Spring整合Mybatis/Junit

Spring整合Mybatis/Junit

忘记中二的少年 Lv3

Spring整合Mybatis/Junit

  • 原生Mybatis的XML配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties" />
<typeAliases>
<package name="com.itheima.domain" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接信息-->
<property name="driver" value="${jdbc.Driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载sql的映射文件-->
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

Spring整合MyBatis

  1. 第一步:添加对应的坐标

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    <dependencies>
    <!-- Spring的坐标【Spring技术的核心】 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.10.RELEASE</version>
    </dependency>

    <!-- druid数据连接池 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
    </dependency>

    <!-- MyBatis的坐标 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
    </dependency>

    <!-- mysql的坐标 -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    </dependency>

    <!-- Spring操作数据库专用的包坐标 -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.10.RELEASE</version>
    </dependency>

    <!-- Spring整合Mybatis的包坐标【版本号要与mybatis版本对应】 -->
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
    </dependency>
    </dependencies>
  2. 第二步:创建外部配置文件【jdbc.properties】

    1
    2
    3
    4
    jdbc.driver=com.mysql.jdbcDriver
    jdbc.url=jdbc.mysql://127.0.0.1:3306/spring_db
    jdbc.username=root
    jdbc.password=123456
  3. 第三步:创建数据源配置类【JdbcConfig.java】

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class JdbcConfig{
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource(){
    DruidDataSource ds = new DruidDataSource();
    ds.setDriverClassName(driver);
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    return ds;
    }
    }
  4. 第四步:创建配置类【SpringConfig.java】

    1
    2
    3
    4
    5
    @Configuration						//声明为Spring的配置类
    @ComponentScan("com.itheima") //bean的扫描
    @PropertySource("classpath:jdbc.properties") //扫描外部配置文件
    @Import({JdbcConfig.class,MybatisConfig.class}) //第三方的bean管理
    public class SpringConfig{}
  5. 第五步:创建一个Mybatis的配置类用于管理第三方(Mybatis)的bean【MybatisConfig.java】代替原生Mybatis的xml配置文件

    image-20230802121432063
    image-20230802121512784

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class MybatisConfig{
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    ssfb.setTypeAliasesPackage("com.itheima.domian");//原生XML配置的<typeAliases>标签
    ssfb.setDataSource(dataSource);//原生XML配置的<dataSource>标签
    return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfiguree(){ //原生XML配置的mapper映射标签
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setBasePackage("com.itheima.dao");
    return msc;
    }
    }

Spring整合Junit

  1. 添加对应的坐标

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.10.RELEASE</version>
    </dependency>
  2. 创建测试类【MyTestClass.java】

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @RunWith(SpringJUnit4ClassRunner.class)			//Spring整合JUnit专用的类加载器
    @ContextConfiguration(classes = SpringConfig.class) //指定Spring上下文配置
    public class AccountServiceTest {

    @Autowired
    private MyService myservice;

    @Test
    public void myTestOne(){ /*函数实现*/ };
    }
  • 标题: Spring整合Mybatis/Junit
  • 作者: 忘记中二的少年
  • 创建于 : 2023-09-18 12:00:00
  • 更新于 : 2023-10-07 22:42:57
  • 链接: https://github.com/HandsomeXianc/HandsomeXianc.github.io/2023/09/18/Spring整合MyBatis_Junit/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
Spring整合Mybatis/Junit