SpringBoot集成Mybatis Plus

Mybatis Plus是Mybatis的升级版本,提供一些默认的功能实现,只要按照其约定编写代码,就可以充分利用其特性。

SpringBoot集成Mybatis Plus步骤:

每个实体类定义一套Mybatis Plus接口与类

EntityBean,MapperInterface,IService,ServiceImpl

例如:
SpringBoot集成Mybatis Plus_第1张图片

SpringBoot集成Mybatis Plus_第2张图片

1、修改POM,添加Mybatis Plus依赖

SpringBoot集成Mybatis Plus_第3张图片


  4.0.0
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
        
    
  
  com.test
  EC5_Proj
  war
  0.0.1-SNAPSHOT
  EC5_Proj Maven Webapp
  http://maven.apache.org
    
        
            org.springframework.boot
            spring-boot-starter-web
        
		
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            javax.servlet
            jstl
        
        
            taglibs
            standard
            1.1.2
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
        
            com.baomidou
            mybatisplus-spring-boot-starter
            1.0.5
        
        
            com.baomidou
            mybatis-plus
            2.1.8
        
        
            tk.mybatis
            mapper
            3.4.5
        

        
        
            com.alibaba
            druid
            1.1.6
            
                
                    com.alibaba
                    jconsole
                
                
                    com.alibaba
                    tools
                
            
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.1.2
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        
        
            org.springframework.data
            spring-data-solr
        
        
            commons-beanutils
            commons-beanutils-core
            1.8.3
        

        
            org.springframework.boot
            spring-boot-devtools
            true
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
          junit
          junit
          4.12
          compile
        

        
    
  
    EC4_Proj
	
		
			org.springframework.boot
			spring-boot-maven-plugin
			
				true
			
		
	
  


2、修改application.properties配置文件

SpringBoot集成Mybatis Plus_第4张图片

#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{
	public List findGood(@Param("name") String name);
	public GoodInfo findGoodById(@Param("id") Integer id);
	public List 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{
	public List 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 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 
	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 findType() {
		try
		{
			return mapper.findType();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List findGood(String name) {
		return mapper.findGood(name);
	}

}

7.Controller层自定义查询
SpringBoot集成Mybatis Plus_第5张图片

代码:https://pan.baidu.com/s/1eTNkOZtrgWA1aw8OXsxIBw

你可能感兴趣的:(SpringBoot)