美文网首页Spring Boot
SpringBoot2.0数据访问(三)整合MyBatis

SpringBoot2.0数据访问(三)整合MyBatis

作者: 码农StayUp | 来源:发表于2018-06-20 12:02 被阅读0次

    本文作者:Spring_ZYL
    意见反馈:spring_zyl@163.com
    文章来源:https://www.jianshu.com/u/1b5928185b73
    版权声明:本文版权归作者所有,转载请注明出处

    一、引入MyBatis依赖

    在pom文件中引入MyBatis依赖

          <!--引入MyBatis依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
    

    二、整合MyBatis(注解版)

    1.创建Bean类

    package com.demo.springboot.bean;
    
    public class Users {
    
        private Integer id;
        private String username;
        private String password;
        private String phone;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            this.phone = phone;
        }
    }
    

    2.创建Mapper接口

    package com.demo.springboot.mapper;
    ​
    import com.demo.springboot.bean.Users;
    import org.apache.ibatis.annotations.*;
    ​
    /**
     * 表示此接口为操作数据库的Mapper接口
     */
    @Mapper
    public interface UsersMapper {
    ​
     /**
     * 插入一个用户
     * @param user
     * @return
     */
     @Insert("insert into (username, password, phone) values (#{username}, #{password}, #{phone})")
     public int insertUser(Users user);
    ​
     /**
     * 根据ID删除一个用户
     * @param id
     * @return
     */
     @Delete("delete users where id = #{id}")
     public int deleteUserById(Integer id);
    ​
     /**
     * 根据ID修改用户
     * @param user
     * @return
     */
     @Update("update users set username = #{username}, password = #{password}, phone = #{phone} where id = #{id}")
     public int updateUser(Users user);
    ​
     /**
     * 根据ID相询用户
     * @param id
     * @return
     */
     @Select("select * from users where id = #{id}")
     public Users queryUserById(Integer id);
    }
    

    3.创建Controller类(RESTful风格)

    package com.demo.springboot.controller;
    ​
    import com.demo.springboot.bean.Users;
    import com.demo.springboot.mapper.UsersMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    ​
    @RestController
    @RequestMapping("/mybatis")
    public class MybatisController {
    ​
     @Autowired
     UsersMapper usersMapper;
    ​
     /**
     * 新增一个用户
     * @param user
     * @return
     */
     @PostMapping("/user")
     public Users insertUser(Users user) {
     usersMapper.insertUser(user);
     return user;
     }
    ​
     /**
     * 修改一个用户
     * @param user
     * @return
     */
     @PutMapping("/user")
     public int updateUser(Users user){
     return usersMapper.updateUser(user);
     }
    ​
     /**
     * 删除一个用户
     * @param id
     * @return
     */
     @DeleteMapping("/user/{id}")
     public int deleteUserById(@PathVariable Integer id){
     return usersMapper.deleteUserById(id);
     }
    ​
     /**
     * 查询一个用户
     * @param id
     * @return
     */
     @GetMapping("/user/{id}")
     public Users selectUserById(@PathVariable Integer id){
     return usersMapper.queryUserById(id);
     }
    ​
    }
    

    4.使用Postman测试

    (1)新增一个用户

    测试地址:127.0.0.1:8080/mybatis/user

    请求方式:POST

    返回结果见如图:

    1.png

    数据库增加了一条记录,如下图:

    2.png
    (2)删除一个用户

    测试地址:127.0.0.1:8080/mybatis/user/3

    请求方式:DELETE

    返回结果见下图:

    3.png

    数据库中id为3的记录被删除,如下图:

    4.png
    (3)修改一个用户

    测试地址:127.0.0.1:8080/mybatis/user

    请求方式:PUT

    返回结果见下图:

    6.png

    数据已更新,见如下图:

    7.png
    (4)查询一个用户

    测试地址:127.0.0.1:8080/mybatis/user/9

    请求方式:GET

    返回结果见下图:

    5.png

    三、整合MyBatis(配置版)

    1.创建部门表

    11.png

    2.创建对应的JavaBean类

    ​
    public class Dept {
    ​
     private Integer id;
     private String deptName;
    ​
     public Integer getId() {
     return id;
     }
    ​
     public void setId(Integer id) {
     this.id = id;
     }
    ​
     public String getDeptName() {
     return deptName;
     }
    ​
     public void setDeptName(String deptName) {
     this.deptName = deptName;
     }
    }
    

    3.创建Mapper接口

    package com.demo.springboot.mapper;
    ​
    import com.demo.springboot.bean.Dept;
    import org.apache.ibatis.annotations.Options;
    ​
    public interface DeptMapper {
    ​
     public Integer insertDept(Dept dept);
    ​
     public Dept selectDeptById(Integer id);
    ​
    }
    

    4.添加Controller

     /**
     * 插入一个部门
     * @param dept
     * @return
     */
     @PostMapping("/dept")
     public Dept insertDept(Dept dept){
     deptMapper.insertDept(dept);
     return dept;
     }
    ​
     @GetMapping("/dept/{id}")
     public Dept findDeptById(@PathVariable Integer id){
     return deptMapper.selectDeptById(id);
     }
    

    5.在application.yml添加mybatis配置

    #配置mybatis
    mybatis:
     #mapper文件的位置
     mapper-locations: classpath:mapper/**/*.xml
     configuration:
     #开启驼峰命名
     map-underscore-to-camel-case: true
    

    6.在SpringBoot启动类中添加怎么扫描mapper

    @MapperScan("com.demo.springboot.mapper") // 定义扫描的mapper包

    package com.demo.springboot;
    ​
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    ​
    @MapperScan("com.demo.springboot.mapper") // 定义扫描的mapper包
    @SpringBootApplication
    public class SpringbootJdbcApplication {
    ​
     public static void main(String[] args) {
     SpringApplication.run(SpringbootJdbcApplication.class, args);
     }
    }
    

    7.使用Postman测试

    12.png 13.png

    相关文章

      网友评论

        本文标题:SpringBoot2.0数据访问(三)整合MyBatis

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