目录
一、MyBatis+MyBatis-Plus:
第一步:将UserMapper继承BaseMapper
第二步:使用MyBatisPlus的MybatisSqlSessionFactoryBuilder进程构建:
第三步:在实体类上添加@TableName,指定数据库表名:
简单说明:
二、Spring+MyBatis+MyBatisPlus:
创建项目导入依赖:
编写数据库配置文件:
第一步:编写jdbc.properties:
第二步:编写applicationContext.xml:
第三步:编写实体类对象以及UserMappper接口:
第四步:编写测试用例:
三、SpringBoot+MyBatis+MyBatisPlus:
第一步:创建工程,导入依赖:
第二步:编写配置文件:
第三步:编写实体类:
第四步:编写启动类:
第五步:编写测试用例:
传送口:回顾MyBatis
一、MyBatis+MyBatis-Plus:
第一步:将UserMapper继承BaseMapper
继承BaseMapper之后,该接口将拥有BaseMapper中的所有方法:
package com.krian.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.krian.pojo.User;
import java.util.List;
public interface UserMapper extends BaseMapper<User> {
public List<User> findAll();
}
第二步:使用MyBatisPlus的MybatisSqlSessionFactoryBuilder进程构建:
package com.krian;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.krian.mapper.UserMapper;
import com.krian.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class TestMyBatisPlus {
@Test
public void testUserFindAll() throws Exception{
//加载MyBatis核心配置文件:
String resource = "mybatis-config.xml";
//通过文件流的方式加载核心配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
//通过MybatisSqlSessionFactoryBuilder整合MyBatisPlus插件
SqlSessionFactory sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(inputStream);
//通过sqlSessionFactory获取SqlSession会话:
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过sqlSession加载Mapper文件,获取Mapper对象:
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//通过BaseMapper中定义的方法:
List<User> list = mapper.selectList(null); //调用了MyBatisPlus方法,参数查询条件
for (User user:list){
System.out.println(user);
}
}
}
第三步:在实体类上添加@TableName,指定数据库表名:
package com.krian.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings({"all"})
@TableName(value = "tb_user") //MyBatisPlus数据库表映射
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
测试:
[main] [com.krian.mapper.UserMapper.selectList]-[DEBUG] ==> Preparing: SELECT id,user_name,password,name,age,email FROM tb_user
[main] [com.krian.mapper.UserMapper.selectList]-[DEBUG] ==> Parameters:
[main] [com.krian.mapper.UserMapper.selectList]-[DEBUG] <== Total: 5
User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=test1@itcast.cn)
User(id=2, userName=lisi, password=123456, name=李四, age=20, email=test2@itcast.cn)
User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=test3@itcast.cn)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=test4@itcast.cn)
User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=test5@itcast.cn)
简单说明:
由于使用了MybatisSqlSessionFactoryBuilder进行了构建,继承的BaseMapper中的方法就载入到了SqlSession中,所以就可以直接使用相关的方法
二、Spring+MyBatis+MyBatisPlus:
引入了Spring框架,数据源、构建等工作就交给了Spring进行管理!!!
创建项目导入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>MyBatis-Plus</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatisPlus-Spring</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>5.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--MyBatis-Plus依赖-->
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<!--mybatis依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!--MySQL连接依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- 连接池 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<!--简化bean代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
编写数据库配置文件:
第一步:编写jdbc.properties:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_plus? useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
jdbc.username=root
jdbc.password=131452
第二步:编写applicationContext.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"
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.xsd">
<!--配置文件扫描器-->
<context:property-placeholder location="classpath:*.properties"/>
<!-- 定义数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>
<!--这里使用MyBatisPlus提供的sqlSessionFactory,完成了Spring与MyBatisPlus的整合-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--扫描mapper接口,使用的依然是Mybatis原生的扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.krian.mapper"/>
</bean>
</beans>
第三步:编写实体类对象以及UserMappper接口:
package com.krian.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings({"all"})
@TableName(value = "tb_user") //MyBatisPlus数据库表映射
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
package com.krian.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.krian.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
第四步:编写测试用例:
package com.krian.test;
import com.krian.mapper.UserMapper;
import com.krian.pojo.User;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestSpringMyBatisPlus {
@Autowired
UserMapper userMapper;
@Test
public void test(){
List<User> list = userMapper.selectList(null);
for (User user : list){
System.out.println(user);
}
}
}
输出:
User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=test1@itcast.cn)
User(id=2, userName=lisi, password=123456, name=李四, age=20, email=test2@itcast.cn)
User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=test3@itcast.cn)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=test4@itcast.cn)
User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=test5@itcast.cn)
三、SpringBoot+MyBatis+MyBatisPlus:
使用SpringBoot将进一步的简化MP的整合,需要注意的是,由于使用SpringBoot需要继承parent,所以需要重新创建工程,并不是创建子Module。
第一步:创建工程,导入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.krian</groupId>
<artifactId>MyBatisPlus-SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatisPlus-SpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简化代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:编写配置文件:
日志配置文件:log4j.properties
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
数据库配置文件:application.properties
# 项目名称:
spring.application.name = MyBatisPlus-SpringBoot
# 数据库连接配置信息:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_plus? useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=131452
第三步:编写实体类:
package com.krian.mybatisplusspringboot.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
第四步:编写启动类:
package com.krian.mybatisplusspringboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.krian.mybatisplusspringboot.mapper") //设置mapper接口的扫描包
@SpringBootApplication
public class MyBatisPlusSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusSpringBootApplication.class, args);
}
}
第五步:编写测试用例:
package com.krian.mybatisplusspringboot;
import com.krian.mybatisplusspringboot.mapper.UserMapper;
import com.krian.mybatisplusspringboot.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
@SuppressWarnings({"all"})
class MyBatisPlusSpringBootApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
List<User> list = userMapper.selectList(null);
for (User user : list) {
System.out.println(user);
}
}
}
测试:
User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=test1@itcast.cn)
User(id=2, userName=lisi, password=123456, name=李四, age=20, email=test2@itcast.cn)
User(id=3, userName=wangwu, password=123456, name=王五, age=28, email=test3@itcast.cn)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=test4@itcast.cn)
User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=test5@itcast.cn)
|