33.SpringBoot中Mybatis高度整合提高应用

54 篇文章 6 订阅
3 篇文章 0 订阅

额外:

现在写博客就是逼着自己多去总结,在以后的项目中少走弯路。

 

  • 公共类

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>

 

微信公众号               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 和 MyBatis 并不是不匹配,实际上它们经常被结合使用,形成一套非常流行的全栈技术解决方案,尤其适用于快速构建大型应用的后端服务。 ### Spring Boot 的作用: Spring Boot 是由 Pivotal 团队提供的一个轻量级框架,它用于简化基于 Spring 的项目的启动过程。它的核心目标之一就是让开发者可以迅速搭建和部署应用,同时保持对微服务架构的强大支持能力。通过内置的自动配置、依赖注入等功能,以及便捷的开发工具和文档生成,使得开发者能够更专注于业务逻辑的实现而非基础配置和环境设置上。 ### MyBatis 的作用: MyBatis 是一个持久层框架,它提供了一种简单的 SQL 映射语言来操作数据库表。它允许程序员编写简单且易于理解的 SQL 语句,并将其映射到对应的 Java 对象。MyBatis 特别适合于需要高度定制化查询的应用场景,如复杂的 JOIN、动态 SQL 等。 ### 配合使用的好处: 将 Spring Boot 和 MyBatis 结合起来使用的原因有很多,主要包括但不限于以下几个方面: 1. **模块化和解耦**:Spring Boot 可以轻松地集成各种组件和服务,而 MyBatis 则专注于数据访问层的高效处理。这种分离可以提高系统的灵活性和可维护性。 2. **自动配置和管理**:Spring Boot 自动配置特性可以帮助开发者避免大量的 XML 配置文件,使得应用程序的启动更为简便快捷。 3. **灵活的数据访问**:MyBatis 提供了强大的 SQL 映射功能,这使得与数据库的交互变得既强大又灵活。结合 Spring Data JPA 或其他数据访问层框架,还可以进一步增强数据访问的能力。 4. **易于测试**:Spring Boot 和 MyBatis 一起工作,使得单元测试变得更加容易和高效,尤其是对于复杂的数据库操作而言。 5. **性能优化**:两者都针对效率进行了优化设计,在实际应用通常能展现出不错的性能。 ### 相关问题: 1. **如何在项目整合 Spring Boot 和 MyBatis?** 2. **如何利用 MyBatis 实现分页查询?** 3. **如何优化 Spring Boot 应用程序的性能?** 总结来说,Spring Boot 和 MyBatis 是互补的,共同构成了高性能、易维护的 Web 开发框架组合。虽然它们各自的特性和使用场景有所不同,但在实际应用,二者的结合却能够极大地提升开发效率和系统质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值