1. 配置pom.xml


引入mybatis-spring-boot-starter。


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>


2. 在application.properties中增加mybatis配置



#mybatis
#entity扫描的包名
mybatis.type-aliases-package=com.xiaolyuh.domain.model
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:/META-INF/mybaits/*Mapper.xml


3. 代码

3.1 User实体

根据application.properties中的定义,包名为com.example.entity

public class User {
private Integer id;
private String username;
//省略了setters & getters
}

3.2 UserMapper接口

注意不要忘记写@Mapper。

@Mapper
public interface UserMapper {
User selectUser(Integer id);
User selectAllUsers();
void insertUser(User user);
void updateUser(User user);
void deleteUser(User user);
}

3.3 UserMapper.xml

根据application.properties中的定义,UserMapper.xml位于src/main/resources/mappers目录下。

<?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.dao.UserMapper">

<resultMap type="User" id="userResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
</resultMap>

<select id="selectUser" resultType="User" resultMap="userResultMap">
select id, username from t_user where id = #{id}
</select>

<select id="selectAllUsers" resultType="User" resultMap="userResultMap">
select id, username from t_user
</select>

<insert id="insertUser" parameterType="User">
insert into t_user (id, username ) values (#{id},#{username})
</insert>

<update id="updateUser" parameterType="User">
update t_user
<set>
<if test="username != null ">
username = #{username},
</if>
</set>
where id = #{id}
</update>

<delete id="deleteUser" parameterType="User">
delete from t_user where id = #{id}
</delete>

</mapper>

3.4 UserController

@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
UserMapper userMapper;

@RequestMapping(value = "/add.do", method = RequestMethod.POST)
public User add(@RequestParam(value = "id", required = true) Integer id,
@RequestParam(value = "username", required = true) String username) {
User user = new User();
user.setId(id);
user.setUsername(username);
userMapper.insertUser(user);
return user;
}

@RequestMapping(value = "/query.do", method = RequestMethod.GET)
public User query(@RequestParam(value = "id", required = true) Integer id) {
return userMapper.selectUser(id);
}

}