额外:
现在写博客就是逼着自己多去总结,在以后的项目中少走弯路。
- 公共类
1.BaseDao.java BaseService.java两个里面的内容都是一样的,都是对公共的方法用接口进行封装。一个用在持久化层,一个用在业务层,这里只写一个。
package org.niugang.dao;
import java.util.List;
/**
*
* @Description:基础持久化类
* @Project:boot-base
* @File:BaseDao.java
* @Package:org.niugang.dao
* @Date:2018年7月5日下午7:36:11
* @author:niugang
* @Copyright (c) 2018, 863263957@qq.com All Rights Reserved.
*
*/
public interface BaseDao<T, S> {
List<T> getList(S s);
List<T> getListByPage(S s);
void insert(S s);
void delete(int id);
T getById(int id);
T get(S s);
int count(S s);
void update(S s);
}
- 演示代码
针对 部门信息表 进行封装。
1.实体类
package org.niugang.dept.entity;
import java.util.Date;
/**
*
* @Description:部门实体类
* @Project:boot-sis
* @File:DeptEntity.java
* @Package:org.niugang.dept.entity
* @Date:2018年7月5日下午3:09:32
* @author:niugang
* @Copyright (c) 2018, 863263957@qq.com All Rights Reserved.
*
*/
public class DeptEntity {
/**
* id
*/
private Integer id;
/**
* 部门名称
*/
private String deptName;
/**
* 父id
*/
private Integer parentId;
/**
* 层级编码
*/
private String code;
/**
* 删除标识
*/
private String deleteFlag;
/**
* 全拼
*/
private String quanPin;
/**
* 简拼
*/
private String jianPin;
private Date createTime;
private String creator;
private Date updateTime;
private String updator;
/**
* 版本号
*/
private Long version;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(String deleteFlag) {
this.deleteFlag = deleteFlag;
}
public String getQuanPin() {
return quanPin;
}
public void setQuanPin(String quanPin) {
this.quanPin = quanPin;
}
public String getJianPin() {
return jianPin;
}
public void setJianPin(String jianPin) {
this.jianPin = jianPin;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getUpdator() {
return updator;
}
public void setUpdator(String updator) {
this.updator = updator;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
2.Dao类
package org.niugang.dept.dao;
import org.niugang.dao.BaseDao;
import org.niugang.dept.bean.DeptQueryBean;
import org.niugang.dept.entity.DeptEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DeptDao extends BaseDao<DeptEntity, DeptQueryBean> {
}
3.查询Bean
解释:查询bean主要是在DeptEntity上进行再次封装,用于查询参数传递,扩展其他字段。
package org.niugang.dept.bean;
import org.niugang.constant.CodeConstant;
import org.niugang.dept.entity.DeptEntity;
/**
*
* @Description:部门查询bean
* @Project:boot-sis
* @File:DeptQueryBean.java
* @Package:org.niugang.dept.bean
* @Date:2018年7月5日下午5:41:49
* @author:niugang
* @Copyright (c) 2018, 863263957@qq.com All Rights Reserved.
*
*/
public class DeptQueryBean extends DeptEntity {
/**
* 页面
*/
private Integer pageNo = CodeConstant.DEFAULT_PAGE_NUMBER;
/**
* 分页大小
*/
private Integer pageSize = CodeConstant.DEFAULT_PAGE_SIZE;
/**
* 开始查询行
*/
private Integer startRecord;
/**
* 定义查询字段,在有些情况下并非所有的字段都查询,只查询给定字段
*/
private String[] queryField;
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getStartRecord() {
return (pageNo - 1) * pageSize;
}
public void setStartRecord(Integer startRecord) {
this.startRecord = startRecord;
}
public String[] getQueryField() {
return queryField;
}
public void setQueryField(String ...queryField) {
this.queryField = queryField;
}
}
高度整合,细细研究 3.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="org.niugang.dept.dao.DeptDao">
<resultMap id="BaseResultMap"
type="org.niugang.dept.entity.DeptEntity">
<result column="c_id" property="id" />
<result column="c_dept_name" property="deptName" />
<result column="c_parent_id" property="parentId" />
<result column="c_code" property="code" />
<result column="c_delete_flag" property="deleteFlag" />
<result column="c_quanpin" property="quanPin" />
<result column="c_jianpin" property="jianPin" />
<result column="c_createtime" property="createTime" />
<result column="c_creator" property="creator" />
<result column="c_updatetime" property="updateTime" />
<result column="c_updator" property="updator" />
<result column="c_version" property="version" />
</resultMap>
<!--查询字段 -->
<sql id="Base_Column_List">
<choose>
<!--只查询给定字段 -->
<when test="queryField!=null">
<foreach collection="queryField" separator="," item="item">
<if test="item!=null and item!=''">
<!--这里只能用${},因为${}是指传什么就用什么,而#{}是根据传的调用起get..方法 -->
${item}
</if>
</foreach>
</when>
<!--查询全部字段 -->
<otherwise>
c_id,
c_dept_name,
c_parent_id,
c_code,
c_delete_flag,
c_quanpin,
c_jianpin,
c_createtime,
c_creator,
c_updatetime,
c_updator,
c_version
</otherwise>
</choose>
</sql>
<!-- 查询条件 -->
<sql id="queryCondition">
<where>
<if test="id!=null">
and c_id=#{id}
</if>
<if test="deptName!=null and deptName!=''">
and c_dept_name like "%"#{deptName}"%"
</if>
<if test="parentId!=null and parentId!=''">
and c_parent_id =#{parentId}
</if>
<if test="code!=null and code!=''">
and c_code like "%"#{code}"%"
</if>
<if test="deleteFlag!=null and deleteFlag!=''">
and c_delete_flag=#{deleteFlag}
</if>
<if test="quanPin!=null and quanPin!=''">
and c_quanpin like "%"#{quanPin}"%"
</if>
<if test="jianPin!=null and jianPin!=''">
and c_jianpin like "%"#{jianPin}"%"
</if>
<if test="createTime!=null and createTime!=''">
and c_createtime=#{createTime}
</if>
<if test="creator!=null and creator!=''">
and c_creator=#{creator}
</if>
<if test="updateTime!=null and updateTime!=''">
and c_updatetime=#{updateTime}
</if>
<if test="version!=null and version!=''">
and c_version=#{version}
</if>
</where>
</sql>
<!--分页查询条件 -->
<sql id="limitCoddition">
order by c_id desc limit
<choose>
<when test="startRecord!=null and startRecord!=''">
#{startRecord},
</when>
<otherwise>
0,
</otherwise>
</choose>
<choose>
<when test="pageSize!=null and pageSize!=''">
#{pageSize}
</when>
<otherwise>
10
</otherwise>
</choose>
</sql>
<!--查询集合 -->
<select id="getList" resultMap="BaseResultMap"
parameterType="org.niugang.dept.bean.DeptQueryBean">
select
<include refid="Base_Column_List" />
from t_boot_dept
<include refid="queryCondition" />
</select>
<!--分页查询 -->
<select id="getListByPage" resultMap="BaseResultMap"
parameterType="org.niugang.dept.bean.DeptQueryBean">
select
<include refid="Base_Column_List" />
from t_boot_dept
<include refid="queryCondition" />
<include refid="limitCoddition" />
</select>
<!--插入 -->
<insert id="insert"
parameterType="org.niugang.dept.bean.DeptQueryBean">
<!-- mysql数据库自增 -->
<selectKey resultType="java.lang.Integer" order="AFTER"
keyProperty="id" keyColumn="c_id">
SELECT LAST_INSERT_ID() AS id
</selectKey>
INSERT INTO `t_boot_dept` (
<trim suffixOverrides=",">
<!--字符串类型需要判断是否为'' -->
<!--对象只需要判断是否为空 -->
<if test="deptName!=null and deptName!=''">
`c_dept_name`,
</if>
<if test="parentId!=null">
`c_parent_id`,
</if>
<if test="code!=null and code!=''">
`code`,
</if>
<if test="deleteFlag!=null and deleteFlag!=''">
`c_delete_flag`,
</if>
<if test="quanPin!=null and quanPin!=''">
`c_quanpin`,
</if>
<if test="jianPin!=null and jianPin!=''">
`c_jianpin`,
</if>
`c_createtime`,
<if test="creator!=null and creator!=''">
`c_creator`,
</if>
<if test="updateTime!=null">
`c_updatetime`,
</if>
<if test="updator!=null and updator!=''">
`c_updator`,
</if>
<if test="version!=null">
`c_version`
</if>
</trim>
)
VALUES
(
<trim suffixOverrides=",">
<if test="deptName!=null and deptName!=''">
#{deptName},
</if>
<if test="parentId!=null">
#{parentId},
</if>
<if test="code!=null and code!=''">
#{code},
</if>
<if test="deleteFlag!=null and deleteFlag!=''">
#{deleteFlag},
</if>
<if test="quanPin!=null and quanPin!=''">
#{quanPin},
</if>
<if test="jianPin!=null and jianPin!=''">
#{jianPin},
</if>
NOW(),
<if test="creator!=null and creator!=''">
#{creator},
</if>
<if test="updateTime!=null">
#{updateTime},
</if>
<if test="updator!=null and updator!=''">
#{updator},
</if>
<if test="version!=null">
#{version}
</if>
</trim>
)
</insert>
<!--删除 -->
<delete id="delete" parameterType="int">
delete from t_boot_dept where
c_id =#{id}
</delete>
<!--根据主键查询 -->
<select id="getById" parameterType="int"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_boot_dept
where c_id =#{id}
</select>
<!--查询单个对象 -->
<select id="get"
parameterType="org.niugang.dept.bean.DeptQueryBean"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_boot_dept
<include refid="queryCondition" />
</select>
<!--查询数量 -->
<select id="count"
parameterType="org.niugang.dept.bean.DeptQueryBean" resultType="int">
select count(*) from t_boot_dept
<include refid="queryCondition" />
</select>
<!--更新 -->
<update id="update"
parameterType="org.niugang.dept.bean.DeptQueryBean">
<if test="id!=null">
update t_boot_dept
<trim prefix="set" suffixOverrides=",">
<if test="deptName!=null and deptName!=''">
c_dept_name=#{deptName},
</if>
<if test="parentId!=null and parentId!=''">
c_parent_id=#{parentId},
</if>
<if test="code!=null and code!=''">
c_code=#{code},
</if>
<if test="deleteFlag!=null and deleteFlag!=''">
c_delete_flag=#{deleteFlag},
</if>
<if test="quanPin!=null and quanPin!=''">
c_quanpin=#{quanPin},
</if>
<if test="jianPin!=null and jianPin!=''">
c_jianpin=#{jianPin},
</if>
<if test="createTime!=null">
c_createtime=#{createTime},
</if>
<if test="creator!=null and creator!=''">
c_creator=#{creator},
</if>
c_updatetime=now(),
<if test="version!=null and version!=''">
c_version=#{version}
</if>
</trim>
where c_id=#{id}
</if>
</update>
</mapper>
微信公众号