您的位置:首页 > 编程语言 > Java开发

Spring boot整合mybatis实现数据的CRUD

2017-06-16 05:42 1171 查看

Spring boot整合mybatis实现数据的CRUD

本文主要参考:https://spring.io/guides/gs/rest-service/  这个是如何搭建Spring boot
本文略过如何搭建spring boot,默认已经搭建好spring boot了,如果想要搭建spring boot可以参考上边的文章。好,下面上货。首先看一下目录结构:


1、添加maven依赖。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

2、在classpath:mybatis文件夹下添加mybatis-conf.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="String" type="java.lang.String"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>

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<Student> getAll();

Student getById(int id);

List<Student> getByNameLike(String name);

Student getByIdAndName(Student student);

Student getByIdAndNameWithParamMap(HashMap<String,Object> params);

List<Student> getByCreateTime(Date date);

int insertStudent(Student student);

int deleteById(Student student);
}

5、添加studentmapper.xml
<?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.xueyou.demo.dao.StudentDao">

<sql id="allField">
id, `name`, age, memo AS attr, createtime, updatetime
</sql>

<select id="getAll" resultType="com.xueyou.demo.pojo.Student">
select
<include refid="allField"/>
from student
</select>

<select id="getById" resultType="com.xueyou.demo.pojo.Student">
SELECT
<include refid="allField"/>
from student WHERE id = #{id}
</select>

<select id="getByNameLike" resultType="com.xueyou.demo.pojo.Student">
SELECT
<include refid="allField"/>
from student WHERE name LIKE CONCAT('%',#{name},'%')
</select>

<select id="getByIdAndName" resultType="com.xueyou.demo.pojo.Student" parameterType="com.xueyou.demo.pojo.Student">
SELECT
<include refid="allField"/>
from student WHERE id = #{id} AND name = #{name}
</select>

<select id="getByIdAndNameWithParamMap" resultType="com.xueyou.demo.pojo.Student" parameterType="HashMap">
SELECT
<include refid="allField"/>
from student WHERE id = #{id} AND name = #{name}
</select>

<select id="getByCreateTime" resultType="com.xueyou.demo.pojo.Student">
SELECT
<include refid="allField"/>
from student where createtime >= #{date}
</select>

<insert id="insertStudent" parameterType="com.xueyou.demo.pojo.Student">
insert INTO student(id, `name`, age, memo)
VALUES (#{id},#{name},#{age},#{memo})
</insert>

<delete id="deleteById">
DELETE FROM student where id = #{id}
</delete>
</mapper>
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<Student> getAll() {
List<Student> studentList = studentDao.getAll();
return studentList;
}

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

@RequestMapping("/getByNameLike")
public List<Student> 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<String, Object> param = new HashMap<>();
param.put("id", id);
param.put("name", name);
return studentDao.getByIdAndNameWithParamMap(param);
}

@RequestMapping("/getByCreatetime")
public List<Student> 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"})
否则会出现找不到接口的实现类的问题。

测试数据:


由于请求很多,这里不一一列出了。update和delete的返回值是sql执行后受影响的行数。如果返回0,那么就意味着更新或者删除失败。
后记:2018年3月23日
当前如果更新了新版本的mybatis starter,那么需要再application.properties里面配置好xml的路径
#mybatis
mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml否则,会出现找不到dao的实现类的异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: