Spring boot整合mybatis实现数据的CRUD

Spring boot整合mybatis实现数据的CRUD

本文主要参考:
https://spring.io/guides/gs/rest-service/  这个是如何搭建Spring boot

本文略过如何搭建spring boot,默认已经搭建好spring boot了,如果想要搭建spring boot可以参考上边的文章。
好,下面上货。
首先看一下目录结构:
Spring boot整合mybatis实现数据的CRUD_第1张图片
1、添加maven依赖。

            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        

2、在classpath:mybatis文件夹下添加mybatis-conf.xml文件



    
        
        
        
        
        
        
        
    

3、添加实体类Student:
package com.xueyou.demo.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

/**
 * Created by wuxueyou on 2017/5/26.
 */
public class Student {
    private int id;
    private String name;

    private int age;
    private String attr;

    private Date createtime;
    private Date updatetime;


    public Student() {
    }

    public Student(int id) {
        this.id = id;
    }

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student(int id, String name, int age, String attr) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.attr = attr;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAttr() {
        return attr;
    }

    public void setAttr(String attr) {
        this.attr = attr;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Date updatetime) {
        this.updatetime = updatetime;
    }
}

4、添加DAO接口
package com.xueyou.demo.dao;

import com.xueyou.demo.pojo.Student;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wuxueyou on 2017/6/15.
 */
public interface StudentDao {
    List getAll();

    Student getById(int id);

    List getByNameLike(String name);

    Student getByIdAndName(Student student);

    Student getByIdAndNameWithParamMap(HashMap params);

    List getByCreateTime(Date date);

    int insertStudent(Student student);

    int deleteById(Student student);
}

5、添加studentmapper.xml




    
    id, `name`, age, memo AS attr, createtime, updatetime
    

    

    

    

    

    

    

    
        insert INTO student(id, `name`, age, memo)
        VALUES (#{id},#{name},#{age},#{memo})
    

    
        DELETE FROM student where id = #{id}
    
6、在controller层进行处理:
package com.xueyou.demo.controller;

import com.xueyou.demo.dao.StudentDao;
import com.xueyou.demo.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by wuxueyou on 2017/6/15.
 */
@RestController
@RequestMapping("/mybatis")
public class mybatisTestController {
    @Autowired
    public StudentDao studentDao;

    @RequestMapping("/getAll")
    public List getAll() {
        List studentList = studentDao.getAll();
        return studentList;
    }

    @RequestMapping("/getById")
    public Student getById(int id) {
        return studentDao.getById(id);
    }

    @RequestMapping("/getByNameLike")
    public List getByNameLike(String name) {
        return studentDao.getByNameLike(name);
    }

    @RequestMapping("/getByIdAndName")
    public Student getByIdAndName(int id, String name) {
        return studentDao.getByIdAndName(new Student(id, name));
    }

    @RequestMapping("/getByIdAndNameWithParamMap")
    public Student getByIdAndNameWithParamMap(int id, String name) {
        HashMap param = new HashMap<>();
        param.put("id", id);
        param.put("name", name);
        return studentDao.getByIdAndNameWithParamMap(param);
    }

    @RequestMapping("/getByCreatetime")
    public List getByCreatetime(String dateStr) throws Exception {
        Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        return studentDao.getByCreateTime(date);
    }

    @RequestMapping("/insertStudent")
    public int insertStudent(int id, String name, int age, String memo) {
        return studentDao.insertStudent(new Student(id, name, age, memo));
    }

    @RequestMapping("/deleteById")
    public int deleteById(int id) {
        return studentDao.deleteById(new Student(id));
    }
}

7、这里需要注意的是在springboot启动的时候需要添加mapperscan注解
@MapperScan(basePackages = {"com.xueyou.demo.dao"})
否则会出现找不到接口的实现类的问题。

测试数据:
Spring boot整合mybatis实现数据的CRUD_第2张图片
由于请求很多,这里不一一列出了。update和delete的返回值是sql执行后受影响的行数。如果返回0,那么就意味着更新或者删除失败。

后记:2018年3月23日

当前如果更新了新版本的mybatis starter,那么需要再application.properties里面配置好xml的路径

#mybatis
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml
否则,会出现找不到dao的实现类的异常。

你可能感兴趣的:(mybatis,spring,boot)