MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
mybatis架构
xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wnhz.third</groupId>
<artifactId>springboot-mybatisplus</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.mybatis-plus.version>3.5.3.1</project.mybatis-plus.version>
<project.mysql.version>8.0.32</project.mysql.version>
<project.druid.version>1.2.16</project.druid.version>
<project.lombok.version>1.18.26</project.lombok.version>
<project.junit.version>4.12</project.junit.version>
</properties>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${project.mysql.version}</version>
</dependency>
<!--引入MybatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${project.mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${project.druid.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${project.lombok.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${project.junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
开发mysqlplus程序
application.yml
数据源配置
基本数据源配置: url、username、password、driver-class-name
yml
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/bookshop_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
数据源其它配置 inital-size、max-active、min-idle、max-wait等
yml
datasource:
# 初始化时建立的物理连接数。初始化发生在显式调用init方法,或者第一次getConnection时.
initial-size: 5
# 连接池最大物理连接数量。
max-active: 50
# 连接池最小物理连接数量。
min-idle: 5
# 获取连接时最大等待时间,单位为毫秒。
# 配置之后,缺省启用公平锁,并发效率会有所下降,若需要可以通过配置useUnfairLock属性为true使用非公平锁。
max-wait: 6000
# 是否缓存preparedStatement,也就是PSCache。
# PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
pool-prepared-statements: true
# 要启用PSCache,其值必须大于0,当大于0时,poolPreparedStatements自动触发修改为true。
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100。
max-pool-prepared-statement-per-connection-size: 20
# 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。
# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。
validation-query: select 1 from dual
mybatisplus配置
yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.mapper.xml #xml文件的位置(resources下的mapper文件夹)
实体类
java
package com.wnhz.bm.domain.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("book_tab")
public class Book {
@TableId(value = "book_id", type = IdType.AUTO)
private Long id;
@TableField("book_title")
private String title;
@TableField("book_author")
private String author;
@TableField("book_price")
private BigDecimal price;
@TableField("book_version")
private Integer version;//版次
@JsonFormat(pattern = "yyyy-MM-dd")
@TableField("book_pubDate")
private Date pubDate;
@TableField("book_store")
private Integer store;
@TableField("book_imgurl")
private String imgUrl;
@TableField("book_weight")
private BigDecimal weight;
@TableField("book_introduction")
private String introduction;
@TableField("book_sold")
private Integer ordered;
@TableField("book_pages")
private Integer pages;
@TableField("book_createtime")
private Date createTime;
@TableField("book_updatetime")
private Date updateTime;
@TableField("book_createby")
private String createBy;
@TableField("book_publisher")
private Long publisher;
@TableField("book_type")
private Long type;
}
mapper映射接口
java
package com.wnhz.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnhz.entity.Book;
@Mapper
public interface IBookDao extends BaseMapper<Book> {
}
service服务接口
java
public interface IBookService {
List<Book> findAllBooks();
List<Book> findByAuthor(String author);
}
service的实现类
java
@Service
public class BookServiceImpl implements IBookService {
@Autowired
private IProductDao dao;
@Override
public List<Product> findAllProducts() {
return dao.selectList(null);
}
@Override
public List<Product> findByName(String name) {
QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", name);
return dao.selectList(queryWrapper);
}
}
或者使用map作为条件
java
Map<String,Object> params = new HashMap<>();
params.put("author",author);
return dao.selectByMap(params);
启动类
@MapperScan注解映射接口的包名称。
java
package com.wnhz;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
测试类
java
package com.wnhz.dao;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wnhz.entity.Book;
import com.wnhz.service.IBookService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class IBookDaoTest {
@Autowired
private IBookService ibs;
@Test
public void testFindAll(){
System.out.println(ibs.findAllBooks());
}
@Test
public void testFindByAuthor(){
System.out.println(ibs.findByAuthor("邓墙"));
}
}