Spring Boot集成Mybatis

2016-09-08  本文已影响2278人  郭寻抚

本文讲述Spring Boot如何通过mybatis-spring-boot-starter集成Mybatis,并给出一个简单的示例。

假设数据库中有t_user表,里面有id和username两个字段。

id username
1 zhangsan
2 lisi

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配置
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
#entity扫描的包名
mybatis.type-aliases-package=com.example.entity

application.properties中关于数据源的配置可以参考:http://www.jianshu.com/p/c8a01ae9f779

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);
  List<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="list" 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);
    }

}

3.5 访问验证

我们可以通过地址来验证查询:http://localhost:8080/user/query.do?id=1

也可以使用Swagger来访问Controller验证代码,Spring Boot 集成 Swagger可以参考:http://www.jianshu.com/p/8ee632047496

spring-boot-starter地址:https://github.com/mybatis/spring-boot-starter
mybatis-spring地址:http://www.mybatis.org/spring/zh/index.html

上一篇下一篇

猜你喜欢

热点阅读