1.添加依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.sql
CREATE TABLE `t_student` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主建', `name` varchar(200) DEFAULT NULL COMMENT '姓名', `create_time` datetime DEFAULT NULL COMMENT '时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '学生表';
3.application.properties
mybatis.mapper-locations=classpath:mappers/*.xml mybatis.type-aliases-package=com.example.demo.model spring.datasource.url = jdbc:mysql://db4free.net:3306/dbs_xq spring.datasource.username = root_xq spring.datasource.password = root123456 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
4.类
(1)dao
package com.example.demo.dao; import com.example.demo.model.StudentEbo; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; /** * Package: com.vince.dao * User: 诸葛子房 * Email: xiaoiqu2017wy@163.com * Date: 2019/3/21 * Time: 11:36 * Description: */ @Mapper public interface StudentDao { @Select("select id,name,create_time as createTime from t_student") List<StudentEbo> findAll(); List<StudentEbo> listStu(@Param("name") String name); StudentEbo getStuById(@Param("id") int id); void addStu(StudentEbo stu); void insertByBatch(List<StudentEbo> studentEbos); @Delete("delete from t_student") void delete(); }
(2)model
package com.example.demo.model; import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; /** * Package: com.vince.model * User: 诸葛子房 * Email: xiaoiqu2017wy@163.com * Date: 2019/3/21 * Time: 11:35 * Description: */ public class StudentEbo { private int id; private String name; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date createTime; 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 Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
(3)启动类----@MapperScan("com.example.demo.dao")
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.demo.dao") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
(4)mapper 文件
<?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.dao.StudentDao"> <resultMap type="com.example.demo.model.StudentEbo" id="StudentMap"> <result property="id" column="id" jdbcType="INTEGER" javaType="Integer"/> <result property="name" column="name" jdbcType="VARCHAR" javaType="String"/> <result property="createTime" column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date"/> </resultMap> <sql id="studentEboMap"> SELECT s.id,s.name,s.create_time FROM t_student AS s </sql> <insert id="addStu" parameterType="com.example.demo.model.StudentEbo"> <selectKey resultType="int" keyProperty="id" order="AFTER"> select @@IDENTITY as id </selectKey> insert into t_student(name,create_time) values( #{name}, #{createTime} ) </insert> <select id="getStuById" resultMap="StudentMap"> SELECT s.id,s.name,s.create_time FROM t_student AS s where id=#{id} </select> <select id="listStu" resultMap="StudentMap"> <include refid="studentEboMap"/> <where> 1=1 <if test=" name != null and name != '' "> AND s.name LIKE '%${name}%' </if> </where> </select> <insert id="insertByBatch"> INSERT INTO t_student (id, name, create_time) VALUES <foreach collection ="list" item="user" separator =","> (#{user.id}, #{user.name}, #{user.createTime}) </foreach > </insert> </mapper>
5.使用
package com.example.demo.controller; /** * @author xiaoqiu * @Date 2019-06-14 15:05 **/ import com.example.demo.dao.StudentDao; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Slf4j @Controller @RequestMapping("/") public class TestController { @Autowired private StudentDao studentDao; @RequestMapping("/") @ResponseBody public String index() { log.info("++++"); return studentDao.getStuById(1).getName() + "hello world"; } }
如果需要打印mybatis sql日志,将日志级别改为debug即可。