全网最详细的SpringBoot学习-day08

SpringBoot整合MyBatis

关于作者

  • 作者介绍

博客主页:作者主页

简介:JAVA领域优质创作者、一名在校大三学生、在校期间参加各种省赛、国赛,斩获一系列荣誉。

关注我:关注我学习资料、文档下载统统都有,每日定时更新文章,励志做一名JAVA资深程序猿‍。

官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

Maven仓库地址:Maven Repository: org.mybatis.spring.boot » mybatis-spring-boot-starter » 2.1.4 (mvnrepository.com)

Mybatis开发文档地址:mybatis – MyBatis 3 | 简介

全网最详细的SpringBoot学习-day08_第1张图片

整合测试

  1. 导入 MyBatis 所需要的依赖

    
    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>2.1.4version>
    dependency>
    
  2. 配置数据库连接信息

    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    #mybatis
    mybatis.type-aliases-package=com.sxau.pojo
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
    
    
  3. 测试数据库是否连接成功!

    image-20210929135221100

  4. 创建实体类,导入 Lombok!

    User.java

    package com.sxau.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int id;
        private String name;
        private String pwd;
    }
    
    
  5. 创建mapper目录以及对应的 Mapper 接口

    UserMapper.java

    package com.sxau.mapper;
    
    import com.sxau.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Mapper //注解表示是一个mybatis的mapper类
    @Repository
    public interface UserMapper {
        List<User> queryUserList();
    
        User quertUserById(int id);
    
        int addUser(User user);
    
        int updateUser(User user);
    
        int deleteUser(int id);
    }
    
    
  6. 对应的Mapper映射文件

    UserMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.sxau.mapper.UserMapper">
        <select id="queryUserList" resultType="User">
            select * from mybatis.user
        select>
    
        <select id="quertUserById" parameterType="int" resultType="User">
            select * from mybatis.user where id=#{id}
        select>
        
        <insert id="addUser" parameterType="User">
            insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
        insert>
    
        <update id="updateUser" parameterType="User">
            update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
        update>
    
        <delete id="deleteUser" parameterType="int">
            delete from mybatis.user where id=#{id}
        delete>
    mapper>
    
  7. maven配置资源过滤问题

    <resources>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.xmlinclude>
            includes>
            <filtering>truefiltering>
        resource>
    resources>
    

    全网最详细的SpringBoot学习-day08_第2张图片

  8. 编写部门的 UserController 进行测试!

    package com.sxau.controller;
    
    import com.sxau.mapper.UserMapper;
    import com.sxau.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    public class UserController {
        @Autowired
        private UserMapper userMapper;
        @GetMapping("/queryUserList")
        public List<User> queryUserList(){
            List<User> users = userMapper.queryUserList();
            return users;
        }
    
        @GetMapping("/quertUserById")
        public User quertUserById(@PathVariable("id")int id){
            User user = userMapper.quertUserById(1);
            return user;
        }
    
        @GetMapping("/addUser")
        public String addUser(){
            userMapper.addUser(new User(10,"阿峰","1234"));
            return "addUser,ok";
        }
    
        @GetMapping("/updateUser")
        public String updateUser(){
            userMapper.updateUser(new User(10,"阿哥哥","1111111"));
            return "updateUser,ok";
        }
    
        @GetMapping("/deleteUser")
        public String deleteUser(@PathVariable("id")int id){
            userMapper.deleteUser(10);
            return "deleteUser,ok";
        }
    }
    

启动项目访问进行测试!

全网最详细的SpringBoot学习-day08_第3张图片

上面测试接口没有问题,说明已经成功读取到数据!

你可能感兴趣的:(Springboot,spring,boot,java,mysql)