链接 🔗

  • MVNRepository

    资源

  • JdbcType 类型配置参考

    安装

    添加依赖:

    1. <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    2. <dependency>
    3. <groupId>org.mybatis.spring.boot</groupId>
    4. <artifactId>mybatis-spring-boot-starter</artifactId>
    5. <version>2.1.1</version>
    6. </dependency>

配置:resources/application.properties :

  1. # mybatis
  2. mybatis.type-aliases-package=com.anydong.example.springboot.mapper
  3. mybatis.mapper-locations=classpath:mapper/*.xml

使用

新建文件:src/main/java/com/example/api/mapper/UserMapper.java

  1. package com.example.api.mapper;
  2. import com.example.api.model.domain.UserDO;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import org.springframework.stereotype.Component;
  6. import java.util.List;
  7. /**
  8. * UserMapper
  9. *
  10. * @author Where
  11. * @date 2019-01-31
  12. */
  13. @Component
  14. @Mapper
  15. public interface UserMapper {
  16. /**
  17. * 获取用户列表
  18. *
  19. * @return
  20. */
  21. List<UserDO> list();
  22. /**
  23. * 统计当前用户数量
  24. *
  25. * @return
  26. */
  27. @Select("SELECT COUNT(*) FROM USER")
  28. Integer count();
  29. }

新建文件:src/main/resources/mapper/UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.example.api.mapper.UserMapper">
  4. <resultMap id="UserDO" type="com.example.api.model.domain.UserDO">
  5. <id column="id" property="id" jdbcType="INTEGER"/>
  6. <result column="username" property="username" jdbcType="VARCHAR"/>
  7. <result column="password" property="password" jdbcType="VARCHAR"/>
  8. <result column="created_at" property="createdAt" jdbcType="TIMESTAMP"/>
  9. <result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP"/>
  10. <result column="deleted_at" property="deletedAt" jdbcType="TIMESTAMP"/>
  11. </resultMap>
  12. <select id="list" resultMap="UserDO" resultType="list">
  13. SELECT * FROM USER
  14. </select>
  15. </mapper>

然后,可以在controllerservice等代码中使用

  1. package com.example.api.controller;
  2. import com.example.api.mapper.UserMapper;
  3. import com.example.api.model.domain.UserDO;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. /**
  10. * UserController
  11. *
  12. * @author Where
  13. * @date 2019-01-31
  14. */
  15. @RestController
  16. @RequestMapping(value = "/user", produces = "application/json; charset=utf-8")
  17. public class UserController {
  18. @Autowired
  19. UserMapper userMapper;
  20. @RequestMapping(value = "/list", method = RequestMethod.GET)
  21. public List<UserDO> list() {
  22. return userMapper.list();
  23. }
  24. }