Java培训_Spring整合MyBatisPlus
一:简介
MyBatisPlus 简称MP 是MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发,提高效率而生。MP有着众多特性,主要变现在:无侵入 依赖少 损耗小 预防sql注入 通用mapper操作 支持多种主键策略 支持热加载 支持ActiveRecord 支持代码生成 内置分页插等件;
二:MP快速入门
2.1:新建数据库
-- 创建数据库
CREATE DATABASE mybatisplus;
-- 使用数据库
USE mybatisplus;
-- 创建表
CREATE TABLE t_user(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(50),
age int );
INSERT INTO t_user(name,email ,age) VALUES('李白','libai@togogo.net',17);
INSERT INTO t_user(name,email ,age) VALUES('亚瑟','yase@togogo.net',18);
INSERT INTO t_user(name,email ,age) VALUES('妲己','daji@togogo.net',19);
2.2:构建Maven聚合工程
2.2.1:构建Maven父工程
<!—集中定义版本号-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<spring.version>5.1.2.RELEASE</spring.version>
<mysql.version>8.0.13</mysql.version>
<druid.version>1.1.10</druid.version>
<lombok.version>1.16.12</lombok.version>
<log4j.version>1.2.17</log4j.version>
<mybatis-plus.version>2.3</mybatis-plus.version>
</properties>
<dependencyManagement>
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--spring ioc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2.2:构建Maven子工程并导入必须的依赖
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencys>
注意:不需要加入mybatis与spring的依赖,MyBatisPlus会自行维护
2.2.3:编写配置文件
1.编写db.properties配置文件
jdbc.user=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.108.129.95:3306/mybatisplus?characterEncoding=utf8
2.编写log4j.properties配置文件
#Global logging configuration
log4j.rootLogger=DEBUG, stdout
#Console output
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
3.编写Spring-context.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!--配置数据源-->
<context:property-placeholder location="db.properties"/>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.user}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 事务管理器 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 基于注解的事务管理 -->
<tx:annotation-driven
transaction-manager="dataSourceTransactionManager"/>
<!--配置SqlSessionFactory-->
<!--注意:整合mybatisplus框架时只需要调整SqlSessionFactory为MyBatis-plus的SqlSessionFactory即可-->
<bean name="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--mybatis原生配置 配置日志输出-->
<property name="configuration">
<bean class="com.baomidou.mybatisplus.MybatisConfiguration">
<property name="logImpl" value="org.apache.ibatis.logging.log4j.Log4jImpl"/>
</bean>
</property>
<!--注入MybatisPlus的全局策略配置-->
<property name="globalConfig" ref="globalConfiguration"></property>
</bean>
<!-- 定义MybatisPlus的全局策略配置-->
<bean id ="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 在2.3版本以后,dbColumnUnderline 默认值就是true -->
<!--<property name="dbColumnUnderline" value="true"></property>-->
<!-- Mysql 全局的主键策略 -->
<!--<property name="idType" value="0"></property>-->
<!--全局表前缀策略配置-->
<!--<property name="tablePrefix" value="t_"></property>-->
</bean>
<!--配置mapper接口扫描器-->
<bean name="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.togogo.mapper"/>
</bean>
</beans>
2.3:编写测试代码
2.3.1:编写与数据库对应的实体类
@Data
@TableName("t_user")
public class User {
private Integer id;
private String name;
private String email;
private Integer age;
}
2.3.2:编写mapper接口
public interface UserMapper extends BaseMapper<User> {
}
注意:BaseMapper<T>:范型当前mapper接口所操作的实体类类型
2.3.3:编写测试类
public class HelloWordTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
//数据源测试
@Test
public void DataSourceTest() throws Exception{
DataSource ds = context.getBean("dataSource", DataSource.class);
System.out.println("获取到的数据源是:" + ds);
Connection connection = ds.getConnection();
System.out.println("获取到的连接信息是:" + connection);
}
//insert操作
@Test
public void mpInsert(){
UserMapper userMapper = context.getBean(UserMapper.class);
User user = new User();
//user.setId(15);//mysql已设置为id自增长 mp主键策略选择
user.setLastname("孙策");//@TableField 标记了带有下划线的字段
user.setMoney(200.0);//数据库中不存在此字段
user.setEmail("sunce@qq.com");
user.setAge(20);
int result = userMapper.insert(user);
System.out.println("result = " + result);
//获取当前插入数据在数据库中的主键值
Integer key = user.getId();
System.out.println("key = " + key);
}
}
三:MP入门开发流程与总结
MP操作数据库只需要创建*Mapper接口,继承BaseMapper接口即可,不需要创建对应的sql映射文件,BaseMapper默认提供了一系列的增删查改方法,开发者对于这些简单的操作只需要直接调用对应的方法即可;
总结:MP在MyBatis的基础上大大简化了xml配置文件,使得MP更加简洁,并且提供了丰富的crud操作。