一、添加依赖
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
二、基于mybais注解的集成
2.1、配置数据源
##################################mysql数据源配置##################################
spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.2、java代码
User.java
package com.example.demo.pojo;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String name;
private Date createTime;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", createTime=" + createTime +
'}';
}
}
UUserMapper.java
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface UserMapper {
@Insert(value = "insert into user (name,create_time) values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})")
int insert(User record);
@Select(value = "select id, name, create_time from user where id = #{id,jdbcType=INTEGER}")
@Results(value = { @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP) })
User selectByPrimaryKey(Integer id);
}
2.3、测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDemo27ApplicationTests {
@Autowired
private UserMapper mapper;
@Test
public void insert() {
User user = new User();
user.setName("测试");
user.setCreateTime(new Date());
int result = mapper.insert(user);
System.out.println(result);
}
@Test
public void select() {
User result = mapper.selectByPrimaryKey(1);
System.out.println(result);
}
}
运行insert方法:
运行select方法:
三、基于mybatis的xml集成
3.1、配置
##################################mysql数据源配置##################################
spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##################################mybatis基于xml集成##################################
#扫包
mybatis.mapper-locations: classpath:mybatis
@Mapper
public interface UserMapper2 {
int insert(User record);
User selectByPrimaryKey(Integer id);
}
UserMapper2.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.example.demo.mapper.UserMapper2" >
<resultMap id="BaseResultMap" type="com.example.demo.pojo.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, name, create_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.example.demo.pojo.User" >
insert into user (name, create_time)
values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})
</insert>
</mapper>
3.3、测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.UserMapper2;
import com.example.demo.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDemo27ApplicationTests {
@Autowired
private UserMapper2 mapper2;
@Test
public void insert() {
User user = new User();
user.setName("测试2");
user.setCreateTime(new Date());
int result = mapper2.insert(user);
System.out.println(result);
}
@Test
public void select() {
User result = mapper2.selectByPrimaryKey(2);
System.out.println(result);
}
}
运行insert方法:
运行select方法: