ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot秘籍七、集成mybatisplus实战

2019-07-18 10:43:52  阅读:188  来源: 互联网

标签:mybatisplus SpringBoot 秘籍 import commonfile com public String


在SpringBoot秘籍六基础上做如下修改:

pom.xml
我们删除mybatis-spring-boot-starter和pagehelper-spring-boot-starter依赖。
新增依赖:

<!-- mybatis plus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>

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
新增分页插件:

<plugins>  
    <!-- mybatisplus分页拦截器 -->
   	<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
    </plugin>  
</plugins> 

好了,开始实战

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<Commonfile> {
	
	public void updateAllColumnById(Commonfile commonfile);
}

src/main/resources创建xml配置sql
mybatis/common/CommonfilePlusMapper.xml

<?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.javasgj.springboot.mybatisplus.dao.CommonfileMapper">
 	
    <update id="updateAllColumnById" parameterType="Commonfile">
        update t_c_file
        <trim prefix="set" suffixOverrides=",">
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="name == null or name == ''">
                name = null,
            </if>
            <if test="size != null and size != ''">
                size = #{size},
            </if>
            <if test="size == null or size == ''">
                size = null,
            </if>
            <if test="path != null and path != ''">
                path = #{path},
            </if>
            <if test="path == null or path == ''">
                path = null,
            </if>
            <if test="type != null and type != ''">
                type = #{type},
            </if>
            <if test="type == null or type == ''">
                type = null,
            </if>
            <if test="ywId != null and ywId != ''">
                yw_id = #{ywId},
            </if>
            <if test="ywId == null or ywId == ''">
                yw_id = null,
            </if>
        </trim>
        where id = #{id}
    </update>
</mapper>

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<Commonfile> {

	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<CommonfileMapper, Commonfile> 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<Commonfile> findByPage(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
		
		Page<Commonfile> page = new Page<Commonfile>();
		page.setCurrent(commonfile.getPageIndex());
		page.setSize(commonfile.getPageSize());
		
		QueryWrapper<Commonfile> queryWrapper = new QueryWrapper<Commonfile>();
		if(!StringUtil.isEmpty(commonfile.getName())) {
			queryWrapper.like("name", commonfile.getName());
		}
		return commonfileService.page(page, queryWrapper);
	}
	
	@RequestMapping(value="findAll", method=RequestMethod.POST)
	public List<Commonfile> findAll(@RequestBody Commonfile commonfile, HttpServletRequest request, HttpServletResponse response) {
		
		QueryWrapper<Commonfile> queryWrapper = new QueryWrapper<Commonfile>();
		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<MediaType> 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,整合完了!

标签:mybatisplus,SpringBoot,秘籍,import,commonfile,com,public,String
来源: https://blog.csdn.net/ETTTTTSS/article/details/96337563

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有