在【SpringBoot攻略六、集成mybatis实战】基础上做如下修改:
pom.xml
我们删除mybatis-spring-boot-starter和pagehelper-spring-boot-starter依赖。
新增依赖:
com.baomidou
mybatis-plus-boot-starter
3.0.5
application.properties
去掉mybatis和pagehelper的配置项
新增配置项:
#mybatis plus
mybatis-plus.configLocation=classpath:mybatis/mybatis-config.xml
mybatis-plus.mapperLocations=classpath*:/mybatis/**/*Mapper.xml
mybatis-plus.typeAliasesPackage=com.javasgj.springboot.mybatisplus.domain
mybatis-plus.typeAliasesSuperType=com.javasgj.springboot.mybatisplus.domain.BaseEntity
mybatis-config.xml
新增分页插件:
mybatisplus开发指南:https://mp.baomidou.com/
1、创建实体类
Pagination:
package com.javasgj.springboot.mybatisplus.domain;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
/**
* 分页工具类
*/
public class Pagination {
/**
* 当前页码
*/
@TableField(exist = false)
@JSONField(serialize = false)
protected int pageIndex;
/**
* 每页大小
*/
@TableField(exist = false)
@JSONField(serialize = false)
protected int pageSize;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
BaseEntity:
package com.javasgj.springboot.mybatisplus.domain;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
/**
* 基础实体类
*/
public class BaseEntity extends Pagination {
// 主键
@TableId(type=IdType.UUID)
protected String id;
// 创建人
protected String cjrId;
protected String cjr;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
protected Date cjSj;
// 更新人
protected String gxrId;
protected String gxr;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
protected Date gxSj;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCjrId() {
return cjrId;
}
public void setCjrId(String cjrId) {
this.cjrId = cjrId;
}
public String getCjr() {
return cjr;
}
public void setCjr(String cjr) {
this.cjr = cjr;
}
public Date getCjSj() {
return cjSj;
}
public void setCjSj(Date cjSj) {
this.cjSj = cjSj;
}
public String getGxrId() {
return gxrId;
}
public void setGxrId(String gxrId) {
this.gxrId = gxrId;
}
public String getGxr() {
return gxr;
}
public void setGxr(String gxr) {
this.gxr = gxr;
}
public Date getGxSj() {
return gxSj;
}
public void setGxSj(Date gxSj) {
this.gxSj = gxSj;
}
}
Commonfile:
package com.javasgj.springboot.mybatisplus.domain;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("t_c_file")
public class Commonfile extends BaseEntity {
// 以下为实体类对应表的所有字段
private String name;
private Long size;
private String path;
private String type;
private String ywId;
// 额外辅助字段
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getYwId() {
return ywId;
}
public void setYwId(String ywId) {
this.ywId = ywId;
}
}
2、创建mapper
CommonfileMapper:
package com.javasgj.springboot.mybatisplus.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasgj.springboot.mybatisplus.domain.Commonfile;
public interface CommonfileMapper extends BaseMapper {
public void updateAllColumnById(Commonfile commonfile);
}
src/main/resources创建xml配置sql
mybatis/common/CommonfilePlusMapper.xml
update t_c_file
name = #{name},
name = null,
size = #{size},
size = null,
path = #{path},
path = null,
type = #{type},
type = null,
yw_id = #{ywId},
yw_id = null,
where id = #{id}
3、创建service
CommonfileService:
package com.javasgj.springboot.mybatisplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.javasgj.springboot.mybatisplus.domain.Commonfile;
public interface CommonfileService extends IService {
public void updateAllColumnById(Commonfile commonfile);
}
CommonfileServiceImpl:
package com.javasgj.springboot.mybatisplus.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasgj.springboot.mybatisplus.dao.CommonfileMapper;
import com.javasgj.springboot.mybatisplus.domain.Commonfile;
@Service
public class CommonfileServiceImpl extends ServiceImpl implements CommonfileService {
@Override
@Transactional
public void updateAllColumnById(Commonfile commonfile) {
this.baseMapper.updateAllColumnById(commonfile);
}
}
4、创建controller
CommonfileController:
package com.javasgj.springboot.mybatisplus.controller;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
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.javasgj.springboot.mybatisplus.domain.Commonfile;
import com.javasgj.springboot.mybatisplus.service.CommonfileService;
import com.javasgj.springboot.mybatisplus.utils.StringUtil;
/**
* 公共附件管理模块
*/
@RestController
@RequestMapping("/commonfile")
public class CommonfileController {
@Autowired
private CommonfileService commonfileService;
@RequestMapping(value="findById", method=RequestMethod.GET)
public Commonfile findById(String id, HttpServletRequest request, HttpServletResponse response) {
return commonfileService.getById(id);
}
@RequestMapping(value="findByPage", method=RequestMethod.POST)
public IPage findByPage(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
Page page = new Page();
page.setCurrent(commonfile.getPageIndex());
page.setSize(commonfile.getPageSize());
QueryWrapper queryWrapper = new QueryWrapper();
if(!StringUtil.isEmpty(commonfile.getName())) {
queryWrapper.like("name", commonfile.getName());
}
return commonfileService.page(page, queryWrapper);
}
@RequestMapping(value="findAll", method=RequestMethod.POST)
public List findAll(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
QueryWrapper queryWrapper = new QueryWrapper();
if(!StringUtil.isEmpty(commonfile.getName())) {
queryWrapper.like("name", commonfile.getName());
}
return commonfileService.list(queryWrapper);
}
@RequestMapping(value="save", method=RequestMethod.POST)
public void save(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
commonfileService.save(commonfile);
}
@RequestMapping(value="update", method=RequestMethod.POST)
public void update(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
commonfileService.updateAllColumnById(commonfile);
}
@RequestMapping(value="deleteById", method=RequestMethod.POST)
public void deleteById(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
commonfileService.removeByIds(Arrays.asList(commonfile.getId().split(",")));
}
}
5、应用启动类
package com.javasgj.springboot.mybatisplus;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
/**
* spring boot应用启动类
* @MapperScan:mybatis Mapper接口扫描,如果不加此注解,那么必须在Mapper接口上加@Mapper
*/
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.javasgj.springboot.mybatisplus.**.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* fastjson替换默认的jackjson(springmvc)
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 处理中文乱码问题
List fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
HttpMessageConverter> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);
}
}
6、测试,使用postman
findById:
get方式:http://127.0.0.1:8080/springboot/commonfile/findById?id=6ff2a63a45da4f27bdd8f292c3b24c27
findByPage:
post方式:http://127.0.0.1:8080/springboot/commonfile/findByPage
Headers头添加:Content-Type:application/json
Body中选择raw:{“pageIndex”: “1”, “pageSize”: “2”}
findAll:
post方式:http://127.0.0.1:8080/springboot/commonfile/findAll
Headers头添加:Content-Type:application/json
Body中选择raw:{“name”: “86”}
save:
post方式:http://127.0.0.1:8080/springboot/commonfile/save
Headers头添加:Content-Type:application/json
Body中选择raw:{“name”: “86”}
update:
post方式:http://127.0.0.1:8080/springboot/commonfile/update
Headers头添加:Content-Type:application/json
Body中选择raw:{“id”:“0e821cf5423a4877899a1649a6de650e”, “name”: “86111”}
deleteById:
post方式:http://127.0.0.1:8080/springboot/commonfile/deleteById
Headers头添加:Content-Type:application/json
Body中选择raw:{“id”:“0e821cf5423a4877899a1649a6de650e,57bb99851d9947208569540d28c1c453”}
OK,整合完了!