一、简述:
【1】Mybatis-Plus只是在Mybatis的基础上,实现了功能增强,让开发更加简洁高效。
【2】Mybatis-Plus并没有修改Mybatis的任何特性!!!
二、搭建环境(创建项目、导入包)
说明:(1)Mybatis-Plus并没有提供单独的jar包,而是通过Maven(或者gradle)来管理jar依赖。因此需要使用Maven构建项目。
(2)Mybatis-Plus是基于Spring框架实现的,因此使用Mybatis-Plus,必须导入Spring相关依赖。
【1】创建一个maven项目(暂测mybatis-plus用不到web端,所以建一个jar的maven项目即可)
![750dffc1e1008cdbcc4695eba1b578be.png](https://i-blog.csdnimg.cn/blog_migrate/2d5e4df0800dab9bf49e114111b69ed2.jpeg)
【2】 配置pom.xml构建文件( 需要导入依赖,并且配置项目的编码,JDK版本等构建信息)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.jinshan.mybatisplus</groupId>
<artifactId>mybatis-Plus-demo</artifactId>
<!-- <version>0.0.1-SNAPSHOT</version> -->
<version>1.0</version>
<!-- 添加依赖 -->
<dependencies>
<!-- spring 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.20.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.24.RELEASE</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3.1</version>
</dependency>
<!-- mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.24.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 配置了build标签里的内容,配置后都必须更新项目 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- 设置编码 -->
<encoding>UTF-8</encoding>
<!-- jdk版本 -->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- 安装install命令的时候跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、配置Mybaits-Plus(基于Spring实现)
整合MybatisPlus与Spring的配置。创建一个spirng-data.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"
xsi:schemaLocation="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.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--一、 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- url -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis-plus"></property>
<!-- 用户名 -->
<property name="username" value="root"></property>
<!-- 密码 -->
<property name="password" value="gzsxt"></property>
<!--最大活动数 -->
<property name="maxActive" value="10"></property>
<!-- 超时毫秒数 -->
<property name="maxWait" value="30000"></property>
</bean>
<!--二、 获取会话工厂 -->
<bean name="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--三、扫描动态映射对象到spring容器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 指定扫描映射包 -->
<property name="basePackage" value="cn.jinshan.mybatisplus"></property>
</bean>
<!--四、配置事务代理 -->
<bean name="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean >
<!--五、 支持事务注解 -->
<tx:annotation-driven transaction-manager="tx" />
</beans>
四、编写测试代码
创建一个pojo
package cn.jinshan.mybatisplus.pojo;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
@TableName(value="tb_user")
public class User {
@TableId(value="id",type=IdType.AUTO)
private Long id;
@TableField(value="name")
private String name;
@TableField(value="age")
private Integer age;
@TableField(value="email")
private String email;
//补全set/get方法
}
创建一个UserMapper继承于BaseMapper(实现增删改查)
package cn.jinshan.mybatisplus.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import cn.jinshan.mybatisplus.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
创建一个测试类UserMapperTest
package cn.jisnhan.mybatisPlus.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import cn.jinshan.mybatisplus.mapper.UserMapper;
import cn.jinshan.mybatisplus.pojo.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring-data.xml")
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
//插入数据
@Test
public void insert() {
try {
User user = new User();
user.setName("lisi");
user.setAge(24);
user.setEmail("lisi@");
Integer insert = userMapper.insert(user);
System.out.println(insert);
} catch (Exception e) {
e.printStackTrace();
}
}
//根据id删除
@Test
public void delete() {
try {
Integer id = userMapper.deleteById(1L);
System.out.println(id);
} catch (Exception e) {
e.printStackTrace();
}
}
//根据条件删除
@Test
public void deleteByCondition() {
try {
EntityWrapper<User> wrapper =new EntityWrapper<>();
wrapper.like("name", "%li%");
Integer id = userMapper.delete(wrapper );
System.out.println(id);
} catch (Exception e) {
e.printStackTrace();
}
}
//更新
@Test
public void update() {
try {
User user = new User();
user.setName("hahaha");
user.setAge(18);
user.setEmail("lisi@");
user.setId(8L);
Integer id = userMapper.updateById(user);
System.out.println(id);
} catch (Exception e) {
e.printStackTrace();
}
}
//查询所有
@Test
public void findAll() {
List<User> list = userMapper.selectList(null);
for (User user : list) {
System.out.println(user.getEmail());
}
}
}