Mybatis和Mybaits-plus在springboot项目中使用的区别


1、概念
MyBatis 是一款优秀的持久层框架。它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
 

2、Mybatis和Mybaits-plus依赖

Mybatis和Mybaits-plus在springboot项目中使用,需要先引入依赖

 

Mybatis和Mybaits-plus的sprongboot项目依赖在中央仓库链接:
Mybatis依赖:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter

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


 Mybaits-plus依赖:https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
还需要mysql依赖:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
 https://mvnrepository.com/artifact/mysql/mysql-connector-java

3、springboot项目中其它依赖

3.1 依赖:

要想使用mybatis和mybatis-plus,还需要其它必备依赖:
先引入parent:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> 

spring-boot-starter-parent提供了springboot统一的依赖管理和插件管理;主要的依赖其实是继承了spring-boot-dependencies(通过标签dependencyManagement管理依赖声明),本质是继承了它然后扩展了插件配置。

其它依赖:

mysql依赖
lombok依赖
spring-boot-starter:这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。
spring-boot-starter-web:支持全栈式Web开发,包括Tomcat和spring-webmvc。(controller层@RequestMapper等注解需要用到)
3.2 mysql数据源配置
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tzq?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

4、Mybatis与Mybatis-Plus项目结构

4.1 Mybatis:

 

 

 

4.1.1 Mybatis注意事项:
注意事项一:mapper映射的xml文件如果写在java文件夹下,在编译的时候是不会被编译的(编译器只会编译java文件夹下的.java文件)。否则运行会出现Invalid bound statement (not found)错误(找不到映射xml文件)。
解决办法(两种):
①:xml文件放到resource文件夹下
②:(推荐)在pom文件的`< build >``标签下,添加:(将java文件下的xml文件也打包)
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

注意事项二:需要在启动类或者配置类上加上@MapperScanner( ),否则在启动的时候报错(找不到mapper):

 

注意事项三:需要在配置文件application.properties中添加mybatis.mapper-locations

mybatis.mapper-locations=classpath*:com/example/test002/mapper/**/*.xml

否则 在运行时也会报错Invalid bound statement (not found):(与注意事项第一个一样的错误)

 

 

 

4.2 Mybatis-Plus

 

 

 

 

 

注意:

mb是增强型通用接口基本实现了单表查询的所有方法,可以直接使用,单表查询时无需配置xml文件(自己非要手写sql也可以配置xml映射文件(手动狗头));
mb下的多表查询与mybatis一模一样,还是需要配置xml文件;
mb配置mybatis-plus.mapper-locations,必须加plus,不可以mybatis.mapper-locations,否则配置无效!!!!!

 

 

 

 

5、附录(所有可复用粘贴的文件)
5.1 xml文件格式(可以粘贴复制)
<?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.example.testmybatisplus.mapper.StudentsMapper">

<resultMap id="BaseResultMap" type="com.example.testmybatisplus.entity.Students">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="VARCHAR" property="age" />
</resultMap>

<select id="findTzq" resultType="com.example.testmybatisplus.entity.Students">
select * from students where id = #{id}
</select>

</mapper> 

 

5.2 配置文件
server.port=8004
spring.application.name=testApplication

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tzq?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

# 配置mybatis-plus的mapper位置
mybatis-plus.mapper-locations=classpath*:com/example/testmybatisplus/mapper/**/*.xml

# 配置mybatis的mapper位置
mybatis.mapper-locations=classpath*:com/example/testmybatisplus/mapper/**/*.xml

5.3 打包xml资源文件
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM