玖零定制问题修复的gravatar头像
玖零定制问题修复 2023-07-21 00:05:06
手撕博客0到1 -> 004集成mybatis

1.mybatis是什么

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

2.集成mybatis

2.1. 引入相关依赖

2.1.1. mysql

数据库采用mysql8.x版本,在springboot2.x版本后默认对应的mysql数据库版本为8.x,如果希望降低数据库版本,可以自己去指定版本信息,在mysql依赖中增加<version>标签并指定版本即可,如果采用低版本的情况下在数据库驱动选择时候需特别注意,不同版本的数据库驱动选择不同,例如5.x为com.mysql.jdbc.Driver,8.x版本为com.mysql.cj.jdbc.Driver

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
</dependency>

2.1.2. mybatis

mybatis直接通过mybatis-spring-boot-starter进行快速集成,采用版本为1.3.2对应的mybatis版本为3.4.6。不需要单独去引入mybatis的依赖包在mybatis-spring-boot-starter直接帮我们引入的相关的依赖包

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
</dependency>

手撕博客0到1 -> 004集成mybatis

2.1.3. lombok

引入lombok简化代码量提高可阅读性,Lombok项目是一个java库,它可以自动插入到编辑器和构建工具中,增强java的性能。不需要再写getter、setter或equals方法,只要有一个注解,就有一个功能齐全的构建器、自动记录变量等等

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.22</version>
  <scope>provided</scope>
</dependency>

 

2.2. 创建测试数据库表

创建数据库表demo字段如下

CREATE TABLE `demo` (
 `id` bigint NOT NULL COMMENT '主键id',
 `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '姓名',
 `age` int DEFAULT NULL COMMENT '年龄',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

 

2.3. 配置相关属性

2.3.1.数据源配置

spring:
datasource:
  usernameroot
  password12345678
  urljdbc:mysql://localhost:3306/zhuhuo-blog?useUnicode=true&characterEncoding=utf-8&seSSL=false&useTimezone=true&serverTimezone=GMT%2B8
  driver-class-namecom.mysql.cj.jdbc.Driver
  hikari:
    connection-test-querySELECT 1
    connection-timeout60000
    maximum-pool-size50
    max-lifetime1200000
    validation-timeout5000
    idle-timeout600000

2.3.2. mybatis配置

mybatis:
 #实体对象包的地址
typeAliasesPackagecom.zhuhuo.demo.entity
 #mapper.xml文件地址
mapperLocationsclasspath*:mapper/*Mapper.xml

2.4. 编写测试程序

2.4.1. 创建实体对象

在com.zhuhuo.demo.entity下创建文件DemoEntity

@Data
public class DemoEntity {
​
   private Long id;
​
   private String name;
​
   private Integer age;
}

2.4.2. 创建控制层

在DemoController类下创建方法findDemoList,并设置返回内容为json格式

@ResponseBody
@GetMapping(value = "/findDemoList")
public List<DemoEntity> findDemoList(){
 return demoService.findDemoList();
}

 

2.4.3. 创建mapper接口

在com.zhuhuo.demo.mapper下创建文件DemoMapper并定义方法findDemoList()

public interface DemoMapper {
​
   List<DemoEntity> findDemoList();
}

2.4.4. 创建mapper.xml

在resource文件夹下创建mapper文件夹并创建DemoMapper.xml文件.实现findDemoList方法

<?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.zhuhuo.demo.mapper.DemoMapper">
   <resultMap id="BaseResultMap" type="com.zhuhuo.demo.entity.DemoEntity">
       <id column="id" property="id" jdbcType="BIGINT"/>
       <id column="name" property="name" jdbcType="VARCHAR"/>
       <id column="age" property="age" jdbcType="INTEGER"/>
   </resultMap>
​
   <select id="findDemoList" resultMap="BaseResultMap">
      select id,name,age from demo
   </select>
</mapper>

2.4.5. 创建service

在com.zhuhuo.demo.service下创建demo接口

public interface DemoService {
   List<DemoEntity> findDemoList();
}

在com.zhuhuo.demo.service.impl下创建demo接口的实现类

@Service("DemoService")
public class DemoServiceImpl implements DemoService {
​
   @Autowired
   private DemoMapper demoMapper;
​
   @Override
   public List<DemoEntity> findDemoList() {
       List<DemoEntity> demoEntityList = demoMapper.findDemoList();
       return demoEntityList;
  }
}

2.4.6. 添加注解MapperScan

在启动类中添加注解 @MapperScan并设置扫描包为com.zhuhuo.demo.mapper

@MapperScan(basePackages = "com.zhuhuo.demo.mapper")
@SpringBootApplication
public class ZhuhuoBlogApplication {
​
   public static void main(String[] args) {
       SpringApplication.run(ZhuhuoBlogApplication.class,args);
  }
}

2.4.7.测试验证

测试结果:待补充截图

 

3.替换tkmybatis

上面呢 我们对mybatis进行了基础的集成,如果说习惯写sql的到这一个步骤其实也算集成完毕了,但是为了简化我们的代码量,提高效率希望减少基本crud的sql编写,为此我们对mybatis进行拓展一下,集成tkmybatis.当然了除了tk,其实还有mybatis-plus可供大家选择,有兴趣的可以私底下研究研究

TKMybatis 是基于 Mybatis框架开发的一个工具,内部实现了对单表的基本数据操作,只需要简单继承 TKMybatis 提供的接口,就能够实现无需编写任何 sql 即能完成单表操作。

3.1. 引入相关依赖

3.1.1. 移除原生mybatis

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
</dependency>

3.1.2. 引入tkmybatis

<dependency>
 <groupId>tk.mybatis</groupId>
 <artifactId>mapper-spring-boot-starter</artifactId>
 <version>2.1.5</version>
</dependency>

 

3.2. 修改测试程序

3.2.1. 修改实体对象

修改实体对象增加注解@Table ,@Id

@Data
@Table(name = "demo")
public class DemoEntity {
​
   @Id
   private Long id;
​
   private String name;
​
   private Integer age;
}

3.2.2. 修改Mapper

修改mapper 继承Mapper<?>

public interface DemoMapper extends Mapper<DemoEntity> {
   List<DemoEntity> findDemoList();
}

 

3.2.3. 修改service

@Override
public List<DemoEntity> findDemoList() {
 List<DemoEntity> demoEntityList = demoMapper.selectAll();
 return demoEntityList;
}

3.2.4. 修改注解MapperScan

ZhuhuoBlogApplication下@MapperScan注解依赖包变更为import tk.mybatis.spring.annotation.MapperScan;

4.总结


打赏
最近浏览
玖零定制问题修复  LV34 2024年3月10日
xiongfan  LV6 2023年10月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友