当前位置 博文首页 > 梦梦~~的博客:Spring Boot整合MyBatis(详解)

    梦梦~~的博客:Spring Boot整合MyBatis(详解)

    作者:[db:作者] 时间:2021-07-12 18:34

    ????????我们前面讲解了有关springboot的知识点,了解到springboot不是一个框架,而是一个框架的搭建技术,目的是为了简化Spring应用的初始搭建以及开发过程。既然我们要使用springboot做项目,那底层的使用,我们如果还是使用JDBC的话,会比较麻烦,众所周知,MyBatis是替代底层JDBC的一个技术,所以,今天我们介绍springboot如何这个MyBatis。

    第一步:引入依赖

    ????????在pom文件中引入两个依赖,第一个是与MyBatis相关的依赖,第二个是与MySQL数据库相关的依赖。如下图所示:

    <!--不是springboot官方的-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.1</version>
            </dependency>
    <!--下面这些是springBoot官方的-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    

    第二步:编写配置

    ????????此时springboot已经通过自动配置,生成了系统所需的MapperScannerConfigurer组件,并给组件注入了DataSource,开发人员需要在application.yml文件中配置mybatis的一些必要配置即可,例如:数据源、mapper文件和接口的位置、别名等等。如下图所示:

    spring:
      datasource:
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/cvs_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    
    mybatis:
    #  配置别名
      type-aliases-package: com.example.demo.pojo
    #  配置mapper映射文件的位置
      mapper-locations: classpath:mybatis/mapper/*.xml
    

    ????????上面没有说明mapper接口的配置方式,它的配置方式不是在yml文件中配置的,而是通过两个注解完成的,如下:
    ????????1,@Mapper:标注到mapper接口上即可
    ????????2,@MapperScan:标注到启动类上,或者某个配置上(即被@Configuration标注的类)

    第三步:编写mapper接口以及映射文件

    ????????这一步就不详细说了,大家肯定都是非常熟悉了,直接上代码了。

    //这个注解表示了这是一个mybatis的mapper类:dao
    @Mapper
    @Component
    public interface UserMapper {
        public List<User> getUserList();
        public int addUser(User user);
        public int updateUser(User user);
        public int deleteUser(User user);
    }
    
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.example.demo.mapper.UserMapper">
        <insert id="addUser">
            insert into user (id,name,pwd) value (#{id},#{name},#{pwd})
        </insert>
        <update id="updateUser">
            update user set pwd=#{pwd},name=#{name} where id=#{id}
        </update>
        <delete id="deleteUser">
            delete from user where id=#{id}
        </delete>
        <select id="getUserList" resultType="com.example.demo.pojo.User">
            SELECT * from user
        </select>
    </mapper>
    
    
    @RestController
    public class UserController {
        @Autowired
        UserServiceImpl userService;
        @RequestMapping("/getUserList")
        public List<User> getUserList(){
    
            return userService.getUserList();
    
        }
    }
    
    

    ????????注意:mapper映射文件没有写在src目录下,写在了resources目录下。

    ????????最后启动一下启动类,然后再浏览器访问一下即可,我这里访问之后,出来了效果,如下图:
    在这里插入图片描述
    ????????哪一步不太明白了,大家可以在评论下面打出来,我们在一起讨论。

    cs
    下一篇:没有了