SSM框架整合

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。SSM框架整合,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

SSM框架整合

原始方式整合

  1. 创建数据库和表

SSM框架整合

  1. 创建Maven工程

  2. 导入maven坐标

     <dependencies>
        <!--        上下文-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.6</version>
        </dependency>
        <!--        AOP-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.19</version>
        </dependency>
        <!--        jdbc模板-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>5.3.6</version>
        </dependency>
        <!--        事务-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>5.3.6</version>
        </dependency>
        <!--        测试-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>5.3.6</version>
        </dependency>
        <!--        SpringMVC-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.6</version>
        </dependency>
        <!--        servlet和jsp-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
        </dependency>
    
        <!--        MyBatis相关-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.28</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.6</version>
        </dependency>
    
        <!--        分页管理插件-->
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>3.7.5</version>
        </dependency>
        <dependency>
          <groupId>com.github.jsqlparser</groupId>
          <artifactId>jsqlparser</artifactId>
          <version>0.9.1</version>
        </dependency>
    
        <!--        测试-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.2</version>
        </dependency>
        <!--        日志-->
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.12</version>
        </dependency>
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>2.0.5</version>
        </dependency>
      </dependencies>
    
    
  3. 编写实体类

    public class Account {
        private Integer id;
        private  String name;
        private  Double money;
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setMoney(Double money) {
            this.money = money;
        }
    }
    
  4. 编写mapper接口

    public interface AccountMapper {
        public  void  save(Account account);
        public List<Account> findAll();
    }
    
  5. 创建service接口

    public interface AccountService {
      public void save(Account account);//保存账户数据
      public  List<Account> findAll();//查询账户数据
    }
    
  6. 创建service接口的实现类

    public class AccountServiceImpl implements AccountService{
    
        @Override
        public void save(Account account) {
            try {
                InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
                SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
                SqlSession sqlSession = build.openSession();
                AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
                mapper.save(account);
                sqlSession.commit();
                sqlSession.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    
        @Override
        public List<Account> findAll() {
            try {
                InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
                SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
                SqlSession sqlSession = build.openSession();
                AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
                List<Account> all = mapper.findAll();
                sqlSession.close();
                return all;
            }catch (IOException e){
                e.printStackTrace();
            }
            return null;
        }
    }
    
  7. 编写controller

    @Controller
    @RequestMapping("/account")
    public class AccountController {
        @Autowired
        private AccountService accountService;
    
        //保存
        @RequestMapping("/save")
        @ResponseBody
        public String save(Account account){
            accountService.save(account);
            return  "保存成功";
        }
    //    查询
        public ModelAndView findAll(){
            List<Account> all = accountService.findAll();
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("accountList",all);
            modelAndView.setViewName("accountList");
            return modelAndView;
        }
    }
    
  8. 编写页面

  9. 编写相关配置文件

    SSM框架整合

    配起来太繁琐了,根据自己的需求和之前学的老老实实配置吧

原始的方式整合弊端很多

mybatis整合spring整合

SSM框架整合

将sqlsessionFactory配置到spring容器中

一个新的配置文件sqlMapConfig-spring.xml

SSM框架整合

声明事务控制

还是上面那个配置文件:

<!--    平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSouce"></property>
    </bean>
<!--    配置事务增强-->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
<!--    事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xxx.service.*(..))"></aop:advisor>
    </aop:config>
        
        
        <!--    扫描mapper所在的包,为mapper创建实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.mapper"></property>
    </bean>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/195238.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!