Mybatis Plus是Mybatis的升级版本,提供一些默认的功能实现,只要按照其约定编写代码,就可以充分利用其特性。
SpringBoot集成Mybatis Plus步骤:
1、修改POM,添加Mybatis Plus依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 定义公共资源版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.test</groupId>
<artifactId>EC5_Proj</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>EC5_Proj Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JSP支持的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 数据源依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>jconsole</artifactId>
</exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>tools</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!--
<dependency>
<groupId>com.test</groupId>
<artifactId>gfdev</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/lib/gfdev-2.0.jar</systemPath>
</dependency>
-->
</dependencies>
<build>
<finalName>EC4_Proj</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、修改application.properties配置文件
#SpringBoot服务端口配置
server.port=6060
server.context-path=/
#spring.resources.static-locations=/css,/images,/img,/js
#SpringMVC JSP目录配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
#Http编码配置
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
#Rabbitmq配置
spring.rabbitmq.host=192.168.25.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin2
spring.rabbitmq.password=admin2
spring.rabbitmq.virtual-host=/
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=redis
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
#数据源配置
spring.datasource.name=w1
spring.datasource.url=jdbc:mysql://localhost:3306/w1?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
#Mybatis实体类配置
#mybatis.mapper-locations=classpath:mapper/*.xml
#Mybatis实体类配置
mybatis-plus.mapper-locations: classpath:mapper/*.xml
#Solr配置
spring.data.solr.host=http://localhost:8984/solr/new_core
#日志配置
logging.file=d:/springboot.log
logging.level.com.test.mapper=DEBUG
logging.level.com.test.servlet=DEBUG
logging.level.com.test.service.impl=DEBUG
3、定义实体类
package com.test.model;
import java.io.Serializable;
import java.sql.Date;
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("t_good")
public class GoodInfo implements Serializable{
@TableId(type=IdType.AUTO)
private Integer id = null;
private Integer no = null;
private String name = null;
private String brand = null;
private Integer tid = null;
private Date dt = null;
@TableField(exist=false)
private String tname = null;
private Integer imgId = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNo() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public Date getDt() {
return dt;
}
public void setDt(Date dt) {
this.dt = dt;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Integer getImgId() {
return imgId;
}
public void setImgId(Integer imgId) {
this.imgId = imgId;
}
}
package com.test.model;
import java.io.Serializable;
import java.sql.Date;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
//实体类
@TableName("t_type")
public class GoodTypeInfo implements Serializable{
@TableId
private Integer id = null;
private String name = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4、定义Mapper
package com.test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.test.model.GoodInfo;
import com.test.model.GoodTypeInfo;
@Mapper
public interface GoodMapper extends BaseMapper<GoodInfo>{
public List<GoodInfo> findGood(@Param("name") String name);
public GoodInfo findGoodById(@Param("id") Integer id);
public List<GoodTypeInfo> findType();
}
5、定义服务接口
package com.test.service;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.service.IService;
import com.test.model.GoodInfo;
import com.test.model.GoodTypeInfo;
public interface IGoodService extends IService<GoodInfo>{
public List<GoodInfo> findGood(String name);
public Boolean saveGood(GoodInfo gi);
public Boolean updateGood(GoodInfo gi);
public Boolean deleteGood(Integer id);
public GoodInfo findGoodById(Integer id);
public List<GoodTypeInfo> findType();
}
6、服务实现类
package com.test.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.test.mapper.GoodMapper;
import com.test.model.GoodInfo;
import com.test.model.GoodTypeInfo;
import com.test.service.IGoodService;
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper,GoodInfo>
implements IGoodService{
@Autowired
private GoodMapper mapper;
@Override
public Boolean saveGood(GoodInfo gi) {
try
{
mapper.insert(gi);
return true;
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
@Override
public Boolean updateGood(GoodInfo gi) {
try
{
mapper.updateById(gi);
return true;
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
@Override
public Boolean deleteGood(Integer id) {
try
{
mapper.deleteById(id);
return true;
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
@Override
public GoodInfo findGoodById(Integer id) {
try
{
return mapper.findGoodById(id);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
public List<GoodTypeInfo> findType() {
try
{
return mapper.findType();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
public List<GoodInfo> findGood(String name) {
return mapper.findGood(name);
}
}