一、添加依賴
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
二、基於 mybatis 註解的集成
配置:
#mysql
spring.datasource.url=jdbc:mysql://localhost/spring_boot_demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
package com.roncoo.example.bean;
import java.io.Serializable;
import java.util.Date;
public class RoncooUser implements Serializable {
private static final long serialVersionUID = 1L;
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 == null ? null : name.trim();
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", createTime=").append(createTime);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
package com.roncoo.example.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.roncoo.example.bean.RoncooUser;
@Mapper
public interface RoncooUserMapper {
@Insert(value = "insert into roncoo_user (name, create_time) values (#{name,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})")
int insert(RoncooUser record);
@Select(value = "select id, name, create_time from roncoo_user where id = #{id,jdbcType=INTEGER}")
@Results(value = { @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP) })
RoncooUser selectByPrimaryKey(Integer id);
}
package com.roncoo.example;
import java.util.Date;
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 com.roncoo.example.bean.RoncooUser;
import com.roncoo.example.mapper.RoncooUserMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemo281ApplicationTests {
@Autowired
private RoncooUserMapper mapper;
@Test
public void insert() {
RoncooUser roncooUser = new RoncooUser();
roncooUser.setName("測試");
roncooUser.setCreateTime(new Date());
int result = mapper.insert(roncooUser);
System.out.println(result);
}
@Test
public void select() {
RoncooUser result = mapper.selectByPrimaryKey(2);
System.out.println(result);
}
}
三、基於 mybatis xml 的集成
配置:
#mybatis
mybatis.mapper-locations: classpath:mybatis/*.xml
#mybatis.type-aliases-package: com.roncoo.example.bean
package com.roncoo.example.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.roncoo.example.bean.RoncooUser;
@Mapper
public interface RoncooUserMapper {
int insert(RoncooUser record);
RoncooUser selectByPrimaryKey(Integer id);
}
<?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.roncoo.example.mapper.RoncooUserMapper" >
<resultMap id="BaseResultMap" type="com.roncoo.example.bean.RoncooUser" >
<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 roncoo_user
where id = #{id,jdbcType=INTEGER}
</select>
<insert id="insert" parameterType="com.roncoo.example.bean.RoncooUser" >
insert into roncoo_user (id, name, create_time)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
</insert>
</mapper>
到此結束!