小工具知识网

当前位置: 首页 > 行业知识 > 【SSM进阶学习系列丨整合篇】Spring+SpringMVC+MyBatis 框架配置详解

行业知识

【SSM进阶学习系列丨整合篇】Spring+SpringMVC+MyBatis 框架配置详解

2026-11-05 14:33:26 来源:互联网转载

文章目录

  • 一、环境准备
    • 1.1、创建数据库和表
    • 1.2、导入框架依赖的jar包
    • 1.3、修改Maven的编译版本
    • 1.4、完善Maven目录
    • 1.5、编写项目需要的包
    • 1.6、编写实体、Mapper、Service
    • 二、配置MyBatis环境
      • 2.1、配置mybatis的主配置文件
      • 2.2、编写映射文件
      • 2.3、测试环境是否正确
      • 三、配置Spring环境
        • 3.1、编写主配置文件
        • 3.2、测试Spring环境是否正确
        • 四、Spring整合MyBaits框架
          • 4.1、定义数据库配置
          • 4.2、主配置文件中引入properties配置文件
          • 4.3、注册数据源
          • 4.4、定义事务管理器
          • 4.5、开启事务的注解驱动支持
          • 4.6、配置SqlSessionFactoryBean
          • 4.7、配置自动扫描所有 Mapper 接口和文件
          • 4.8、测试
          • 五、配置SpringMVC环境
            • 5.1、主配置文件
            • 5.2、web.xml配置前端控制器
            • 六、Spring集成SpringMVC环境

              一、环境准备

              1.1、创建数据库和表

              create database ssm;use ssm;create table t_account(id int primary key auto_increment,  name varchar(30),  age int,  balance int);insert into t_account values(default,'HelloWorld',23,100);

              1.2、导入框架依赖的jar包

              ​ 由于我们整合的是SpringMVC、Spring和MyBatis,所以需要将这三个框架所需要的jar包导入到项目中。注意:由于要连接数据库,所以仍需要数据库驱动包。

              ​ 注意:MyBatis框架和Spring框架整合需要一个mybatis-spring的jar包,该jar包的作用是两个框架的转换包。

                           org.mybatis       mybatis       3.5.3               mysql        mysql-connector-java        5.1.6                  org.springframework        spring-context        5.2.1.RELEASE                    org.aspectj        aspectjweaver        1.9.4                    org.springframework        spring-tx        5.2.1.RELEASE                   org.springframework      spring-jdbc      5.2.1.RELEASE                org.springframework      spring-web      5.2.1.RELEASE                  org.springframework      spring-webmvc      5.2.1.RELEASE                  com.fasterxml.jackson.core      jackson-core      2.9.5              com.fasterxml.jackson.core      jackson-annotations      2.9.5              com.fasterxml.jackson.core      jackson-databind      2.9.5                      javax.servlet          javax.servlet-api          3.1.0          provided                javax.servlet      jstl      1.2                  commons-fileupload      commons-fileupload      1.3.1              commons-io      commons-io      2.4                  log4j        log4j        1.2.17                     org.springframework        spring-test        5.2.1.RELEASE        test                    junit        junit        4.12        test                  org.mybatis        mybatis-spring        2.0.3                com.alibaba      druid      1.1.21    

              1.3、修改Maven的编译版本

                  UTF-8    1.8    1.8

              1.4、完善Maven目录

              1.5、编写项目需要的包

              cn.bdqn.domaincn.bdqn.mappercn.bdqn.servicecn.bdqn.controllercn.bdqn.utilscn.bdqn.exception

              1.6、编写实体、Mapper、Service

              public class Account {                                          private int id;                                             private String name;                                        private Integer age;                                        private Integer balance; // 生成get和set方法、toString方法}
              public interface AccountMapper {        // 查询所有的账号    public List selectAll();     // 保存账号    public void insert(Account account);}
              public interface AccountService {    // 查询所有账号    public List queryAll();    // 保存账号    public void save(Account account);}
              public class AccountServiceImpl implements AccountService {    // 查询全部账号    public List queryAll() {        System.out.println("查询全部账号");        return null;    }    // 保存账号    public void save(Account account) {        System.out.println("保存账号");    }}

              二、配置MyBatis环境

              2.1、配置mybatis的主配置文件

              ​ 主配置文件:mybaits-config.xml.

                                                                                                                                                                                                                                      

              2.2、编写映射文件

              ​ 在resources目录下新建与mapper接口同名的目录。即:cn/bdqn/mapper

                              select            id,name,age,balance        from            t_account                        insert into t_account(name,age,balance) values (#{name},#{age},#{balance})        

              2.3、测试环境是否正确

                  @Test    public void testMyBatis() throws Exception{        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);        SqlSession session = ssf.openSession();        AccountMapper accountMapper = session.getMapper(AccountMapper.class);        List accounts = accountMapper.selectAll();        System.out.println(accounts);        is.close();        session.close();    }

              三、配置Spring环境

              3.1、编写主配置文件

              ​ 注意:由于是SSM项目整合,在Spring主配置文件中配置扫描包的时候仅仅针对Service业务层、Mapper持久化层等注解的扫描,对于Web层,交由SpringMVC框架去处理。

                                   

              3.2、测试Spring环境是否正确

                  @Test    public void testSpring() throws Exception{        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");        AccountService accountService = (AccountService) ac.getBean("accountService");        System.out.println(accountService);    }

              四、Spring整合MyBaits框架

              ​ 注意:我们在配置MyBaits环境的时候,数据源配置在了MyBatis上,现在由于Spring整合MyBaits,则对于在MyBatis中配置的数据源要交给Spring去管理。在这里,我们用的阿里巴巴的数据源。

              ​ 官网

              4.1、定义数据库配置

              ​ 定义db.properties数据库配置文件

              jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///ssm?allowMultiQueries=truejdbc.username=rootjdbc.password=1234567

              4.2、主配置文件中引入properties配置文件

               

              4.3、注册数据源

                                              

              4.4、定义事务管理器

                  

              4.5、开启事务的注解驱动支持

               

              4.6、配置SqlSessionFactoryBean

                

              4.7、配置自动扫描所有 Mapper 接口和文件

                  

              4.8、测试

                  @Test    public void testSpringAndMyBatis() throws Exception{        ApplicationContext ac =  new ClassPathXmlApplicationContext("beans.xml");        AccountService accountService = (AccountService) ac.getBean("accountService");        List accounts = accountService.queryAll();        System.out.println(accounts);    }

              五、配置SpringMVC环境

              5.1、主配置文件

                                                                  

              5.2、web.xml配置前端控制器

                    frontDispatcherServlet    org.springframework.web.servlet.DispatcherServlet          contextConfigLocation      classpath:SpringMVC.xml        1        frontDispatcherServlet    /  

              六、Spring集成SpringMVC环境

              ​ 在web.xml文件中配置。

                  org.springframework.web.context.ContextLoaderListener    contextConfigLocation    classpath:beans.xml

上一篇:深度解析Android编程权威源码,掌握核心开发技能

下一篇:服务器和显卡怎么配置?