mybatis-plus将 mybatis封装成与hibernate一样的增删改查操作,同时也能自定义SQL
且源码有中文注释,nice
<!-- mybatis-plus start -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<mybatis-plus.version>2.1.9</mybatis-plus.version>
<mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
pom
package com.admin.model;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;
import java.io.Serializable;
/**
* Created by hc on 2018/12/19 0019.
*
*/
@Data
@TableName("t_type")
public class CTypes extends Model<CTypes> {
/**
* 费用类型主键id
*/
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
/**
* 名称
*/
@TableField("type_name")
private String typeName;
@Override
public String toString() {
return "CType{" +
"id=" + id +
", typeName='" + typeName + '\'' +
", fatherId=" + fatherId +
", status=" + status +
", createId=" + createId +
", createTime='" + createTime + '\'' +
", modifyId=" + modifyId +
", modifyTime='" + modifyTime + '\'' +
'}';
}
@Override
protected Serializable pkVal() {
return this.id;
}
}
model:对象继承Model
package com.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.admin.model.CostSubTypeVO;
import com.admin.model.CostTypes;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2018/12/19 0019.
* 类型 Mapper 接口
*/
@Mapper
@Service
public interface CTypesMapper extends BaseMapper<CTypes>{
CostSubTypeVO selectCostSubTypeById(Integer subId);
List<CostSubTypeVO> selectCostSubTypesPage(Map<String,Integer> params);
CostSubTypeVO selectCostSubTypeByFatherId(Integer fatherId);
}
Mapper:继承BaseMapper
<?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.admin.mapper.CTypesMapper">
<!-- mybatisplus所需 的通用查询结果映射 -->
<resultMap id="BaseResultMap" type="com.admin.model.CTypes">
<id column="id" property="id"/>
<result column="type_name" property="typeName"/>
<result column="status" property="status"/>
<result column="create_id" property="createId"/>
<result column="create_time" property="createTime"/>
<result column="modify_id" property="modifyId"/>
<result column="modify_time" property="modifyTime"/>
</resultMap>
<resultMap id="subTypesResultMap" type="com.admin.model.SubTypeVO">
<id column="id" property="id"/>
<result column="type_name" property="typeName"/>
<result column="status" property="status"/>
<collection column="id" property="subTypeStr" ofType="String" select="selectSubs"></collection>
</resultMap>
<select id="selectCostSubTypesPage" resultMap="subTypesResultMap" parameterType="map">
SELECT id,type_name,`status` FROM t_cost_type WHERE father_id=-1 LIMIT #{start},#{size}
</select>
<select id="selectSubs" resultType="String" parameterType="int">
SELECT GROUP_CONCAT(type_name) FROM t_cost_type WHERE father_id=#{id}
</select>
<select id="selectCostSubTypeByFatherId" resultMap="subTypesResultMap" parameterType="int">
SELECT id,type_name,`status` FROM t_cost_type WHERE id=#{fatherId}
</select>
<resultMap id="subTypeOneResult" type="com.admin.model.CostSubTypeVO">
<id column="id" property="id"/>
<result column="type_name" property="typeName"/>
<result column="status" property="status"/>
<collection property="subTypes" ofType="com.admin.model.CostTypes" column="id" select="selectOneSub">
<id column="id" property="id"/>
<result column="type_name" property="typeName"/>
<result column="status" property="status"/>
<result column="create_id" property="createId"/>
<result column="create_time" property="createTime"/>
<result column="modify_id" property="modifyId"/>
<result column="modify_time" property="modifyTime"/>
</collection>
</resultMap>
<select id="selectSubTypeById" resultMap="subTypeOneResult" parameterType="int">
SELECT id,type_name,`status` FROM t_cost_type WHERE id=#{subId}
</select>
<select id="selectOneSub" resultType="com.admin.model.CTypes" parameterType="int">
SELECT * FROM t_type WHERE father_id=#{id}
</select>
</mapper>
Mapper-xml ps:BaseResultMap
package com.admin.service;
import com.baomidou.mybatisplus.service.IService;
import com.admin.model.CostSubTypeVO;
import com.admin.model.CostTypes;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2018/12/19 0019.
* 类型 Service 接口
*/
public interface CostTypeService extends IService<CostTypes>{
CostSubTypeVO selectCostSubTypeById(Integer subId);
List<CostSubTypeVO> selectCostSubTypesPage(Map<String,Integer> params);
CostSubTypeVO selectCostSubTypeByFatherId(Integer fatherId);
}
service:extends IService
package com.admin.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.admin.mapper.CostTypesMapper;
import com.admin.model.CostSubTypeVO;
import com.admin.model.CostTypes;
import com.admin.service.CostTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2018/12/19 0019.
*类型 Service 接口实现类
*/
@Service
public class CostTypeServiceImpl extends ServiceImpl<CTypesMapper, CostTypes> implements CostTypeService {
@Autowired
private CostTypesMapper costTypeMapper;
/**
*
*
* @param subId
* @return
*/
@Override
public CostSubTypeVO selectSubTypeById(Integer subId) {
return costTypeMapper.selectSubTypeById(subId);
}
}
ServiceImpl extends ServiceImpl
package com.admin.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.admin.model.CostSubTypeVO;
import com.admin.model.CostTypes;
import com.admin.service.CostTypeService;
import com.common.util.Query;
import com.common.util.R;
import com.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2018/12/20 0020.
* 类型 前端控制器
*/
@RestController
@RequestMapping("/costType")
public class CostTypeController extends BaseController{
@Autowired
private CostTypeService costTypeService;
}
方法:
/**
* Copyright (c) 2011-2016, hubin ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.service;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
/**
* <p>
* 顶级 Service
* </p>
*
* @author hubin
* @Date 2016-04-20
*/
public interface IService<T> {
/**
* <p>
* 插入一条记录(选择字段,策略插入)
* </p>
*
* @param entity 实体对象
* @return boolean
*/
boolean insert(T entity);
/**
* <p>
* 插入一条记录(全部字段)
* </p>
*
* @param entity 实体对象
* @return boolean
*/
boolean insertAllColumn(T entity);
/**
* <p>
* 插入(批量),该方法不适合 Oracle
* </p>
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertBatch(List<T> entityList);
/**
* <p>
* 插入(批量)
* </p>
*
* @param entityList 实体对象列表
* @param batchSize 插入批次数量
* @return boolean
*/
boolean insertBatch(List<T> entityList, int batchSize);
/**
* <p>
* 批量修改插入
* </p>
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertOrUpdateBatch(List<T> entityList);
/**
* <p>
* 批量修改插入
* </p>
*
* @param entityList 实体对象列表
* @param batchSize
* @return boolean
*/
boolean insertOrUpdateBatch(List<T> entityList, int batchSize);
/**
* <p>
* 批量修改或插入全部字段
* </p>
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean insertOrUpdateAllColumnBatch(List<T> entityList);
/**
* 批量修改或插入全部字段
*
* @param entityList 实体对象列表
* @param batchSize
* @return boolean
*/
boolean insertOrUpdateAllColumnBatch(List<T> entityList, int batchSize);
/**
* <p>
* 根据 ID 删除
* </p>
*
* @param id 主键ID
* @return boolean
*/
boolean deleteById(Serializable id);
/**
* <p>
* 根据 columnMap 条件,删除记录
* </p>
*
* @param columnMap 表字段 map 对象
* @return boolean
*/
boolean deleteByMap(Map<String, Object> columnMap);
/**
* <p>
* 根据 entity 条件,删除记录
* </p>
*
* @param wrapper 实体包装类 {@link Wrapper}
* @return boolean
*/
boolean delete(Wrapper<T> wrapper);
/**
* <p>
* 删除(根据ID 批量删除)
* </p>
*
* @param idList 主键ID列表
* @return boolean
*/
boolean deleteBatchIds(Collection<? extends Serializable> idList);
/**
* <p>
* 根据 ID 选择修改
* </p>
*
* @param entity 实体对象
* @return boolean
*/
boolean updateById(T entity);
/**
* <p>
* 根据 ID 修改全部字段
* </p>
*
* @param entity 实体对象
* @return boolean
*/
boolean updateAllColumnById(T entity);
/**
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param entity 实体对象
* @param wrapper 实体包装类 {@link Wrapper}
* @return boolean
*/
boolean update(T entity, Wrapper<T> wrapper);
/**
* <p>
* 根据ID 批量更新
* </p>
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean updateBatchById(List<T> entityList);
/**
* <p>
* 根据ID 批量更新
* </p>
*
* @param entityList 实体对象列表
* @param batchSize 更新批次数量
* @return boolean
*/
boolean updateBatchById(List<T> entityList, int batchSize);
/**
* <p>
* 根据ID 批量更新全部字段
* </p>
*
* @param entityList 实体对象列表
* @return boolean
*/
boolean updateAllColumnBatchById(List<T> entityList);
/**
* <p>
* 根据ID 批量更新全部字段
* </p>
*
* @param entityList 实体对象列表
* @param batchSize 更新批次数量
* @return boolean
*/
boolean updateAllColumnBatchById(List<T> entityList, int batchSize);
/**
* <p>
* TableId 注解存在更新记录,否插入一条记录
* </p>
*
* @param entity 实体对象
* @return boolean
*/
boolean insertOrUpdate(T entity);
/**
* 插入或修改一条记录的全部字段
*
* @param entity 实体对象
* @return boolean
*/
boolean insertOrUpdateAllColumn(T entity);
/**
* <p>
* 根据 ID 查询
* </p>
*
* @param id 主键ID
* @return T
*/
T selectById(Serializable id);
/**
* <p>
* 查询(根据ID 批量查询)
* </p>
*
* @param idList 主键ID列表
* @return List<T>
*/
List<T> selectBatchIds(Collection<? extends Serializable> idList);
/**
* <p>
* 查询(根据 columnMap 条件)
* </p>
*
* @param columnMap 表字段 map 对象
* @return List<T>
*/
List<T> selectByMap(Map<String, Object> columnMap);
/**
* <p>
* 根据 Wrapper,查询一条记录
* </p>
*
* @param wrapper 实体对象
* @return T
*/
T selectOne(Wrapper<T> wrapper);
/**
* <p>
* 根据 Wrapper,查询一条记录
* </p>
*
* @param wrapper {@link Wrapper}
* @return Map<String,Object>
*/
Map<String, Object> selectMap(Wrapper<T> wrapper);
/**
* <p>
* 根据 Wrapper,查询一条记录
* </p>
*
* @param wrapper {@link Wrapper}
* @return Object
*/
Object selectObj(Wrapper<T> wrapper);
/**
* <p>
* 根据 Wrapper 条件,查询总记录数
* </p>
*
* @param wrapper 实体对象
* @return int
*/
int selectCount(Wrapper<T> wrapper);
/**
* <p>
* 查询列表
* </p>
*
* @param wrapper 实体包装类 {@link Wrapper}
* @return
*/
List<T> selectList(Wrapper<T> wrapper);
/**
* <p>
* 翻页查询
* </p>
*
* @param page 翻页对象
* @return
*/
Page<T> selectPage(Page<T> page);
/**
* <p>
* 查询列表
* </p>
*
* @param wrapper {@link Wrapper}
* @return
*/
List<Map<String, Object>> selectMaps(Wrapper<T> wrapper);
/**
* <p>
* 根据 Wrapper 条件,查询全部记录
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List<Object>
*/
List<Object> selectObjs(Wrapper<T> wrapper);
/**
* <p>
* 翻页查询
* </p>
*
* @param page 翻页对象
* @param wrapper {@link Wrapper}
* @return
*/
@SuppressWarnings("rawtypes")
Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<T> wrapper);
/**
* <p>
* 翻页查询
* </p>
*
* @param page 翻页对象
* @param wrapper 实体包装类 {@link Wrapper}
* @return
*/
Page<T> selectPage(Page<T> page, Wrapper<T> wrapper);
}
标签:教程,return,param,boolean,plus,Wrapper,mybatis,import,com From: https://blog.51cto.com/u_16082902/6213844