SpringBoot(四):整合Mybatis

SpringBoot(四):整合Mybatis

Scroll Down
小提示,本文编写于  2,102  天前,最后编辑于  1,872  天前,某些信息可能有些出入,仅供参考。

ORM框架有很多,比如Mybatis、hibernate、JPA、JDBCTemplate等,各自有各自的优点。Mybatis作为一个半自动的框架,灵活易上手等特点,收到了很多人的青睐。

Mybatis介绍

基本概念

 什么是 Mybatis?

官方给的解释是:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

优缺点

Mybatis 可能是用的最广泛的一个 ORM 框架了,下面简单列举其优缺点。

优点:

易上手;
sql 灵活,与代码解耦;
支持对象与数据库映射;
半自动框架,灵活性较高;

缺点:

需要维护 sql ;
绑定了sql,移植性差;
二级缓存机制不佳;

开发模式

准备工作

在Mybatis 中,有两种方式开发方式:配置文件开发和注解开发,以下分别介绍两种模式。

我们先来做一下准备工作。不管是哪一种开发模式,下面几步都是相同的:

都需要对 Mybatis 代理接口进行扫描。在 SpringBoot 项目中,扫描方式有两种:

1) 在启动类上加 @MapperScan(value = {"com.sunwin.db.*","com.yanfei1819.mybatisdemo.db"}) 注解;

2) 分别在接口 mapper 上添加 @Mapper 注解;

上面扫描Mybatis 代理接口的两种方式的效果一样,只不过第一种方式是一次性扫描整个包,第二种方式是单独扫描每个接口。

1.初始化数据库:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `age` int(3) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 50 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (14, 'admin', 21);
INSERT INTO `user` VALUES (48, 'teacher', 20);
INSERT INTO `user` VALUES (49, 'student', 22);
 
SET FOREIGN_KEY_CHECKS = 1;

2.引入maven 依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

3.配置数据库信息:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/icefiredb?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

注解开发

开发之前,先大体了解一下项目的结构(这个结构都是自定义的,这里只是为了演示方便):
1557755905903
首先,创建一个实体类:

package com.yanfei1819.mybatisdemo.db.dto;
 
/**
 * Created by icefire on 2018-07-11.
 */
public class UserDto {
    private Long id;
    private String name;
    private int age;
    //  set/get 省略
}

创建以下实体类是为了试返回的值展示更加友好。

package com.yanfei1819.mybatisdemo.entity;
import com.yanfei1819.mybatisdemo.db.dto.UserDto;
import java.util.List;
 
/**
 * Created by icefire on 2018-07-11.
 */
public class UserListResponse {
    private int code;
    private String msg;
    private List<UserDto> users;
    // set/get 省略
}

其次,创建代理接口:

package com.yanfei1819.mybatisdemo.db.dao;
import com.yanfei1819.mybatisdemo.db.dto.UserDto;
import org.apache.ibatis.annotations.Select;
 
import java.util.List;
/**
 * Created by icefire on 2018-07-11.
 */
public interface UserDao {
    @Select("select * from user ")
    List<UserDto> queryList();
}

再者,创建service层:

package com.yanfei1819.mybatisdemo.service;
import com.yanfei1819.mybatisdemo.entity.UserListResponse;
 
/**
 * Created by icefire on 2018-07-11.
 */
public interface UserService {
    UserListResponse queryUsers();
}
package com.yanfei1819.mybatisdemo.service.impl;
import com.yanfei1819.mybatisdemo.db.dao.UserDao;
import com.yanfei1819.mybatisdemo.db.dto.UserDto;
import com.yanfei1819.mybatisdemo.entity.UserListResponse;
import com.yanfei1819.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
 
/**
 * Created by icefire on 2018-07-11.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public UserListResponse queryUsers() {
        List<UserDto> userDtos = userDao.queryList();
        UserListResponse response = new UserListResponse();
        response.setUsers(userDtos);
        response.setCode(0);
        response.setMsg("success");
        return response;
    }
}

然后,创建controller层:

package com.yanfei1819.mybatisdemo.controller;
import com.yanfei1819.mybatisdemo.entity.UserListResponse;
import com.yanfei1819.mybatisdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
/**
 * Created by icefire on 2018-07-11.
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @ResponseBody
    @GetMapping("/queryUsers")
    public UserListResponse queryUsers(){
        return userService.queryUsers();
    }
}

最后,启动main 方法:

package com.yanfei1819.mybatisdemo;
 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@MapperScan("com.yanfei1819.mybatisdemo.db") // 注意这个注解
public class MybatisDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisDemoApplication.class, args);
    }
 
}

用postman测试:
17755905903

配置文件开发

配置文件模式开发比注解开发稍稍复杂一点。因为这种模式多了维护 sql 的 mapper.xml 文件。我将其归结为下面三步:

1.创建代理接口:

2.创建接口映射的 xxxMapper.xml 文件

3.在主配置文件 application.properties 中指定 xxxMapper.xml 的位置: mybatis.mapper-locations=classpath:mapper/*.xml;

在以上项目的基础上,添加以下代码。

首先,新建实体类:

package com.yanfei1819.mybatisdemo.entity;
import com.yanfei1819.mybatisdemo.db.dto.UserDto;
 
/**
 * Created by icefire on 2018-07-11.
 */
public class UserResponse {
    private int code;
    private String msg;
    private UserDto user;
}

其次,创建mapper接口:

package com.yanfei1819.mybatisdemo.db.mapper;
import com.yanfei1819.mybatisdemo.db.dto.UserDto;
import org.apache.ibatis.annotations.Param;
/**
 * Created by icefire on 2018-07-11.
 */
public interface UserMapper {
    UserDto queryUserByName(@Param("name") String name);
}

 然后,创建UserMapper文件:

<?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.yanfei1819.mybatisdemo.db.mapper.UserMapper">
  <select id="queryUserByName" resultType="com.yanfei1819.mybatisdemo.db.dto.UserDto" parameterType="java.lang.String">
    select * from user where `name` = #{name}
  </select>
</mapper>

 下面,UserService 接口添加方法:

UserResponse queryUserByName(String name);

 UserServiceImpl 类实现:

    @Override
    public UserResponse queryUserByName(String name){
        UserDto userDto = userMapper.queryUserByName(name);
        UserResponse response = new UserResponse();
        response.setUser(userDto);
        response.setCode(0);
        response.setMsg("success");
        return response;
    }

 最后,在 controller 层添加方法:

    @ResponseBody
    @GetMapping("/queryUserByName")
    public UserResponse queryUserByName(String name){
        return userService.queryUserByName(name);
    }

测试结果
17755905903

总结

针对上面两种方式,各有优势。
注解开发基本上只要

@Insert 、@Select、@Update、 @Delete 

四个注解就可以搞定。配置文件开发只需要在 xxxMapper.xml 维护 sql 即可。

我个人的喜好是,如果是单表操作,或者是工具包,就选择注解方式,因为比较简洁,没有配置文件;如果是多表操作,则选择配置文件的方式,对sql的操作更灵活,扩展性更好。

教程学自此处