Spring Boot中使用MyBatis

作者: 0e9fc14f0444 | 来源:发表于2019-04-23 14:00 被阅读2次

    整合MyBatis之前,先搭建一个基本的Spring Boot项目[。然后引入spring-boot-mybatis和数据库连接驱动(这里使用关系型数据库mysql)

    添加mysql和mybaties的依赖

    • 这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问
    • 引入连接mysql的必要依赖mysql-connector-java
    • 引入整合MyBatis的核心依赖mybatis-spring-boot-starter
    • 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖

    在pom文件中添加:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.tcbj</groupId>
        <artifactId>spring-boot-mybatis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-mybatis</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!--Mybatis-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
    
            <!--Mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    不同版本的Spring Boot和MyBatis版本对应不一样,具体可查看官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

    • 在application.properties中配置mysql的连接配置
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    使用MyBatis

    • 在Mysql中创建User表,包含id(BIGINT)、name(INT)、age(VARCHAR)字段。同时,创建映射对象User
    @Getter
    @Setter
    public class User {
        private Long id;
        private String name;
        private Integer age;
    }
    
    • 创建User映射的操作UserMapper,为了后续单元测试验证,实现插入和查询操作
    @Mapper
    public interface UserMapper {
    
        @Select("select * from User Where Name = #{name}")
        User findByName(@Param("name") String name);
    
        @Insert("insert into user(name,age) values(#{name},#{age})")
        int insert(@Param("name") String name, @Param("age") Integer age);
    }
    
    • 创建单元测试
      • 测试逻辑:插入一条name=AAA,age=20的记录,然后根据name=AAA查询,并判断age是否为20
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    public class SpringBootMybatisApplicationTests {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        @Rollback
        public void findByName() throws Exception {
            userMapper.insert("AAA", 20);
            User u = userMapper.findByName("AAA");
            Assert.assertEquals(20, u.getAge().intValue());
        }
    }
    
    

    相关文章

      网友评论

        本文标题:Spring Boot中使用MyBatis

        本文链接:https://www.haomeiwen.com/subject/jqvmgqtx.html