spring boot学习系列之整合Mybatis持久层2

拯救 – 孙楠–:– / 05:32
(*+﹏+*)

MyBatis是一个基于Java的持久层框架,它支持定制化 SQL、存储过程以及高级映射。现将spring boot整合Mybatis。

添加依赖

添加mybatis的依赖

  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>1.3.0</version>
  6. </dependency>

项目会自动加载application.properties,在里面定义mybatis配置路径和数据源

  1. #加载mybatis配置文件
  2. mybatis.mapper-locations =classpath:mapper/*Mapper.xml
  3. mybatis.config-location =classpath:mapper/config/sqlConfig.xml
  4. #定义别名
  5. mybatis.type-aliases-package =com.wenqy.domain
  6. #数据源
  7. spring.datasource.url =jdbc:mysql://localhost:3306/wordpress
  8. spring.datasource.driver-class-name =com.mysql.jdbc.Driver
  9. spring.datasource.username =root
  10. spring.datasource.password =123456
  11. spring.datasource.max-active=10
  12. spring.datasource.max-idle=5
  13. spring.datasource.min-idle=0

添加mysql的jdbc依赖

  1. <!– https://mvnrepository.com/artifact/mysql/mysql-connector-java –>
  2. <dependency>
  3.     <groupId>mysql</groupId>
  4.     <artifactId>mysql-connector-java</artifactId>
  5.     <version>5.1.42</version>
  6. </dependency>

接下来就是mybatis的规范编程

定义domain bean

  1. public class User implements Serializable{
  2.     private int id;
  3.     private String name;
  4.     public int getId() {
  5.         return id;
  6.     }
  7.     public void setId(int id) {
  8.         this.id = id;
  9.     }
  10.     public String getName() {
  11.         return name;
  12.     }
  13.     public void setName(String name) {
  14.         this.name = name;
  15.     }
  16. }

定义mybatis Mapper 接口

  1. @Mapper
  2. public interface UserMapper {
  3.     /**
  4.      * 
  5.     * @Title: findAll 
  6.     * @Description:
  7.     * @param     
  8.     * @return List<User>     
  9.     * @throws
  10.      */
  11.     public List<User> findAll();
  12.     /**
  13.      * 
  14.     * @Title: getUserById 
  15.     * @Description:
  16.     * @param @param id
  17.     * @param 
  18.     * @return User
  19.     * @throws
  20.      */
  21.     public User getUserById(int id);
  22. }

使用Mapper注解进行注入

定义UserMapper.xml映射文件

  1. <!– namespace必须指向Dao接口 –>
  2. <mapper namespace=“com.wenqy.mapper.UserMapper”>
  3.     <!– 所有列 –>
  4.     <sql id=“Column_list”>
  5.         id,
  6.         name
  7.     </sql>
  8.     <resultMap id=“ListUser” type=“com.wenqy.domain.User” >
  9.         <id  column=“id” property=“id” />
  10.         <result column=“name” property=“name” />
  11.     </resultMap>
  12.     <!– 根据ID查询数据 –>
  13.     <select id=“getUserById” parameterType=“int” resultMap=“ListUser”>
  14.         SELECT
  15.         <include refid=“Column_list” />
  16.         FROM user
  17.         WHERE id = #{id}
  18.     </select>
  19.     <!– 查询所有用户 –>
  20.     <select id=“findAll” resultType=“com.wenqy.domain.User”>
  21.         SELECT * FROM user
  22.     </select>
  23. </mapper>

定义service接口

  1. public interface UserService {
  2.     /**
  3.      * 
  4.     * @Title: findAll 
  5.     * @Description:
  6.     * @param     
  7.     * @return List<User>     
  8.     * @throws
  9.      */
  10.     public List<User> findAll();
  11.     /**
  12.      * 
  13.     * @Title: getUserById 
  14.     * @Description:
  15.     * @param @param id
  16.     * @param 
  17.     * @return User
  18.     * @throws
  19.      */
  20.     public User getUserById(int id);
  21. }

定义service实现类

  1. @Service
  2. public class UserServiceImpl implements UserService {
  3.     @Autowired
  4.     private UserMapper userMapper;
  5.     public List<User> findAll() {
  6.         List<User> userList = userMapper.findAll();
  7.         return userList;
  8.     }
  9.     public User getUserById(int id) {
  10.         User userById = userMapper.getUserById(id);
  11.         return userById;
  12.     }
  13. }

定义表现层

  1. @RestController
  2. @RequestMapping(“/user”)
  3. public class UserController {
  4.     @Autowired
  5.     private UserService userService;
  6.     @RequestMapping(“/{id}”)
  7.     public User getUserById(@PathVariable(“id”int id) {
  8.         User userById = userService.getUserById(id);
  9.         return userById;
  10.     }
  11.     @RequestMapping(“/findAll”)
  12.     public List<User> findAll() {
  13.         List<User> findAll = userService.findAll();
  14.         return findAll;
  15.     }
  16. //  public static void main(String[] args) {
  17. //      SpringApplication.run(UserController.class, args);
  18. //  }
  19. }

访问路由,效果图

springboot-mabatis-1

springboot-mabits-2

为mybatis添加分页插件

添加分页依赖

  1. <!–pagehelper–>
  2.     <dependency>
  3.     <groupId>com.github.pagehelper</groupId>
  4.     <artifactId>pagehelper-spring-boot-starter</artifactId>
  5.     <version>1.1.2</version>
  6. </dependency>

application.properties 添加分页配置

  1. #pagehelper
  2. pagehelper.helperDialect=mysql
  3. pagehelper.reasonable=true
  4. pagehelper.supportMethodsArguments=true
  5. pagehelper.params=count=countSql

控制层,在控制层加入PageHelper分页工具类方法。

  1. /**
  2.      * 
  3.     * @Title: findByPage 
  4.     * @Description: 分页查询
  5.     * @param @param pageNo
  6.     * @param 页数 
  7.     * @return List<User>   页数据 
  8.     * @throws
  9.      */
  10.     @RequestMapping(“/find/{pageNo}”)
  11.     public List<User> findByPage(@PathVariable(“pageNo”int pageNo) {
  12.         PageHelper.startPage(pageNo, 5);
  13.         List<User> findAll = userService.findAll();
  14.         System.out.println(“Total: “ + ((Page<User>) findAll).getTotal());
  15.         return findAll;
  16.     }

查询结果

springboot-mabits-3

Springboot快速开发,的确是如此的优雅,如此的简洁。我启动项目的时候却报错了,说无法连接数据库,发现是application.properties配置有前缀空格,而项目不是用utf8编码的,弄了我好久,这让我无比郁闷。。。

参考:

http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html

https://github.com/pagehelper/pagehelper-spring-boot

发表评论

电子邮件地址不会被公开。 必填项已用*标注

24 + 4 = ?