[toc]

该文章包括:

  • 使用提供的curd
  • 条件构造器
  • 自定义sql
  • 分页插件
  • sql执行效率插件

配置文件:

application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://10.10.203.10:3306/jplatdvv?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
spring.datasource.username=test
spring.datasource.password=Jht123456


#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource     
spring.datasource.initialSize=5    
spring.datasource.minIdle=5    
spring.datasource.maxActive=20       
spring.datasource.maxWait=60000

#mybatisMapper文件配置地址
#mybatis框架的配置不起作用了
mybatis-plus.mapper-locations=classpath*:mapper/*Mapper.xml 

启动类:

SpringBootTestMybatisPlusApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

@SpringBootApplication
@EnableTransactionManagement
public class SpringBootTestMybatisPlusApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootTestMybatisPlusApplication.class, args);
	}

	 /**
     * SQL执行效率插件
     */
    @Bean
//    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }
    
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

实体类

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

/**
 * 使用帮助实体类
 * 
 * @author xkj
 *
 */
@TableName(value = "jpf_help_info") // 指定表名,默认使用类名(驼峰转下划线)
@Data
public class HelpInfo {

	/** 帮助ID */
	private String id;
	/** 帮助编码 */
	private String helpNo;
	/** 帮助内容 */
	private String content;
	/** 帮助标题 */
	private String title;
	/** 创建人ID */
	private String createId;
	/** 创建人名称 */
	@TableField(exist=false) // 标明该字段不是数据库字段,默认所有的属性都是数据库字段
	private String createName;
	/** 修改人ID */
	private String updateId;
	/** 修改人名称 */
	@TableField(exist=false)
	private String updateName;

控制类

package com.example.demo.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.dao.HelpDao;
import com.example.demo.entity.HelpInfo;

@RequestMapping("test")
@RestController
public class TestController {
	
	private static Logger logger = LoggerFactory.getLogger(TestController.class);
	
	@Autowired
	private HelpDao helpDao;
	
	@RequestMapping("helpPage")
	public IPage<HelpInfo> getHelpPage(String content) {
		IPage<HelpInfo> page=new Page<>(1,4);
		QueryWrapper<HelpInfo> wrapper=new QueryWrapper<>();
		wrapper.select("HELP_NO","TITLE","CONTENT").eq("id", "1");
		wrapper.like(!StringUtils.isEmpty(content), "CONTENT", content); // 默认用 and 连接
		IPage<HelpInfo> helpInfos = helpDao.selectPage(page,wrapper);
		logger.info("返参:{}",helpInfos);
		return helpInfos;
	}
	
	@RequestMapping("helpOne")
	public HelpInfo getHelpOne() {
		QueryWrapper<HelpInfo> wrapper=new QueryWrapper<>() ;
		wrapper.select("HELP_NO","TITLE","CONTENT")
				.eq("id", "1")
				.or()
				.eq("update_Id", "19")
//				.apply("left join jpf_account")  // apply 允许自定义sql
				.last("limit 1");// last : 无视java代码顺序,会直接放到sql最后面
				
		HelpInfo helpInfo = helpDao.selectOne(wrapper);
		logger.info("返参:{}",helpInfo);
		return helpInfo;
	}
	
	@RequestMapping("helpQuery")
	public HelpInfo getHelpQuery() {
		QueryWrapper<HelpInfo> wrapper=new QueryWrapper<>() ;
		wrapper.eq("id", "1").or().eq("update_Id", "19").last("limit 1");
		logger.info("CustomSqlSegment:{}",wrapper.getCustomSqlSegment());// sql条件, 带where
		// CustomSqlSegment:WHERE id = #{ew.paramNameValuePairs.MPGENVAL1} OR update_Id = #{ew.paramNameValuePairs.MPGENVAL2} limit 1
		logger.info("SqlSegment:{}",wrapper.getSqlSegment());// sql条件, 不带where
		// SqlSegment:id = #{ew.paramNameValuePairs.MPGENVAL1} OR update_Id = #{ew.paramNameValuePairs.MPGENVAL2} limit 1
		HelpInfo helpInfo = helpDao.getHelpQuery(wrapper);
		logger.info("返参:{}",helpInfo);
		return helpInfo;
	}
	
	@RequestMapping("helpQueryByMe")
	public HelpInfo getHelpQueryByMe() {
		QueryWrapper<HelpInfo> wrapper=new QueryWrapper<>() ;
		wrapper.eq("id", "1").or().eq("update_Id", "19").last("limit 1").select("HELP_NO","TITLE","CONTENT");
		String id="1";
		HelpInfo helpInfo = helpDao.getHelpQueryByMe(id); // 允许完全自定义sql,和mybatis一样使用(spring boot的配置不一样)
		logger.info("返参:{}",helpInfo);
		return helpInfo;
	}
	
}

业务层

// 可以使用 ServiceImpl类下的方法, 更多的操作数据的操作
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, JbpAccount> implements AccountService {
}

dao层

package com.example.demo.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.HelpInfo;

@Mapper
public interface HelpDao extends BaseMapper<HelpInfo> {

	public HelpInfo getHelpQuery(@Param("ew") Wrapper<HelpInfo> wrapper);

	public HelpInfo getHelpQueryByMe(@Param("id") String id);

}

Mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.HelpDao">

	 <select id="getHelpQuery" resultType="com.example.demo.entity.HelpInfo">
	 	 SELECT * FROM jpf_help_info ${ew.customSqlSegment}
<!-- 	 	 customSqlSegment: mybatis-Plus 框架的一个变量,里面是带where 的 sql, 可看代码 -->
	 </select>
	 
	 <select id="getHelpQueryByMe" resultType="com.example.demo.entity.HelpInfo">
	 	SELECT * FROM jpf_help_info where id = #{id}
	 </select>
</mapper>