springboot 整合mybatis

作者: freelands | 来源:发表于2017-01-18 21:04 被阅读336次

因为来到这边公司要用到mybatis,之前因为用的是springboot,所以就想在springboot下面去整合mybatis. 因为之前用的是hibernate,所以做起来也很简单,我觉得mybatis定制化很好,就是配置起来有点麻烦,之前hibernate 和 JPA用起来很快,mybatis就是要写一写mapper.xml,但是这可以锻炼你的一个sql编写能力。废话就说这么多,先上结构图吧。

结构图:

structure.png

Model:

@Data
public class User {
   private Integer id;
   private String name;
   private Integer age;
   private String email;
}

其中data 注解是lombok插件里的,自动实现get set 以及hashcode 、euqals 和 toString 还有默认的构造函数

Mapper:

@Mapper
public interface UserMapper {

    User findUserById(@Param("id") Integer id);

    int updateUser(@Param("name") String name, @Param("id") Integer id);

    int insertUser(@Param("id") Integer id, @Param("name") String name,
                   @Param("age") Integer age, @Param("email") String email);

    int deleteUser(@Param("id") Integer id);

}

@Mapper注解会自动的生成UserMapper的代理类,并注入到spring容器中,所以在Service层的话,直接@Autowired的注入就行了。

Mapping:

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <resultMap id="result" type="com.example.model.User">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="email" column="email"/>
    </resultMap>
    <select id="findUserById" resultMap="result">
        SELECT * FROM user where id = #{id};
    </select>

    <update id="updateUser">
        UPDATE user
        SET
        NAME = #{name}
        where id = #{id}
    </update>

    <insert id="insertUser">
        INSERT INTO user(id,name,age,email)
        VALUES (
        #{id,jdbcType=INTEGER},
        #{name,jdbcType=VARCHAR},
        #{age,jdbcType=INTEGER},
        #{email,jdbcType=VARCHAR}
        )
    </insert>

    <delete id="deleteUser">
        delete from user where id = #{id}
    </delete>
</mapper>

DAO:

@Component
public class UserDao {
    @Autowired
    private UserMapper mapper;

    public User findUserById(Integer id) {
        return mapper.findUserById(id);
    }

    public int insert(Integer id, String name, Integer age, String email) {
        return mapper.insertUser(id, name, age, email);
    }

    public int update(String name, Integer id) {
        return mapper.updateUser(name, id);
    }

    public int delete(Integer id) {
        return mapper.deleteUser(id);
    }
}

Controller:

@RestController
public class UserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("/user")
    public User getUserById(@RequestParam(value = "id") Integer id) {
        User user;
        user = userDao.findUserById(id);
        return user;
    }

    @RequestMapping("/update")
    public int updateById(@RequestParam(value = "name") String name,
                          @RequestParam(value = "id") Integer id) {
        return userDao.update(name, id);
    }

    @RequestMapping("/insert")
    public int insert(@RequestParam(value = "name") String name,
                      @RequestParam(value = "id") Integer id,
                      @RequestParam(value = "age") Integer age,
                      @RequestParam(value = "email") String email) {
        return userDao.insert(id, name, age, email);
    }

    @RequestMapping("/delete")
    public int delete(@RequestParam(value = "id") Integer id) {
        return userDao.delete(id);
    }

}

Dependencies:

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.1.1')
    compile('org.projectlombok:lombok:1.16.8')
    compile('mysql:mysql-connector-java:6.0.5')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

配置文件:

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapperLocations=classpath*:Mapper/**/*.xml

Schema:

CREATE TABLE `user` (
  `id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT '自增id',
  `name` varchar(255) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `email` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT '邮件',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



相关文章

网友评论

    本文标题:springboot 整合mybatis

    本文链接:https://www.haomeiwen.com/subject/pwdbbttx.html