当前位置: 首页 > news >正文

案例分析之——理由Mybatis动态SQL实现复用

无复用思想的做法:
        在没有复用思想的时候,就只顾着实现功能。比如开发过程中涉及到两个表的更新功能,每需要更新一处,就写一个接口,结果出现了写了11个接口的情况。
这样虽然功能实现了,可是可能自己都忘了自己当时写的是什么。别人看起来维护起来更是困难,写得多还更容易出错。

        那么通过复用,让代码做减法:
有复用思想的做法:
        这里用到了通用SQL这个概念

实践:

1.创建一个Maven项目

2.引入pom依赖,这里引入了三个,mysql,mybatis,junit,如下:

    <dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.2</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies>

同时在pom中添加如下部分

 <!--在build中配置resources,来防止我们资源导出失败的问题--><build><resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource><resource><directory>src/main/java</directory><includes><include>**/*.properties</include><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>

3.项目目录结构,按照这个结构创建

 4.数据库连接文件 db.properties

 

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql?useSSL=false&;useUnicode=true;CharacterEncoding=UTF-8
username=root
password=123456

5.MyBatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration mybatis的核心配置文件-->
<configuration><!--引入外部配置文件--><properties resource="db.properties"/><!--配置--><settings><!--标准日志工厂设置--><setting name="logImpl" value="STDOUT_LOGGING"/><!--显示的开启全局缓存--><setting name="cacheEnabled" value="true"/></settings><!--可以给实体类取别名--><typeAliases><!--可以指定一个包名,MyBatis会在包名下面搜索需要的Java Bean--><package name="org.example.pojo"/></typeAliases><!--environments 后面的s表示这是一个复数,可以编写多套环境  default表示默认的环境为development--><environments default="development"><!--编写一套环境 名称为configuration--><environment id="development"><!--jdbc的事务管理--><transactionManager type="JDBC"/><!--配置数据库相关数据--><dataSource type="POOLED"><property name="driver" value="${driver}"/><!--userSSL是一个按权连接 &amp是一个转移符 等同于and  CharacterEncoding=utf-8可以保证输入数据库的数据不乱码--><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><!--绑定接口--><mappers><mapper class="org.example.dao.UserCourseGroupConfigurationMapper"/></mappers>
</configuration>

6.MyBatis配置类

public class MybatisUtils {private  static SqlSessionFactory sqlSessionFactory;//静态代码块:一旦初始化就加载static{try {//使用Mybatis第一步:获取sqlSessionFactory对象//获取资源,直接读到mybatis-config.xmlString resource = "mybatis-config.xml";//需要用到输入流(InputStream) 把resource类加载进来InputStream inputStream = Resources.getResourceAsStream(resource);//通过build把输入流加载进来sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession() {//openSession中有自动commit(提交)事务的方法,加上true就能实现return sqlSessionFactory.openSession(true);}
}

7.下面就是最重要的部分,通用SQL

通用update语句

    <update id="updateCourseGroupConfiguration">update arpro_user_course_group_configuration<trim prefix="SET" suffixOverrides=","><if test="reviseParam.infoId != null">info_id = #{reviseParam.infoId}</if><if test="reviseParam.courseId != null">course_id = #{reviseParam.courseId}</if><if test="reviseParam.classId != null">class_id = #{reviseParam.classId}</if><if test="reviseParam.groupId != null">group_id = #{reviseParam.groupId}</if><if test="reviseParam.type != null">type = #{reviseParam.type}</if><if test="reviseParam.isDelete != null">is_delete = #{reviseParam.isDelete}</if><if test="reviseParam.remark != null">remark = #{reviseParam.remark}</if><if test="reviseParam.isMostLike != null">is_like = #{reviseParam.isLike}</if></trim>where is_delete = 0<if test="conditionParam.infoId != null"> and info_id = #{conditionParam.infoId}</if><if test="conditionParam.courseId != null">and course_id = #{conditionParam.courseId}</if><if test="conditionParam.classId != null">and class_id = #{conditionParam.classId}</if><if test="conditionParam.groupId != null">and group_id = #{conditionParam.groupId}</if><if test="conditionParam.isMostLike != null">and is_like = #{conditionParam.isLike}</if><if test="conditionParam.type != null">and type = #{conditionParam.type}</if></update>

这样就避免了写多个接口的麻烦和带来的后续麻烦

http://www.lryc.cn/news/32934.html

相关文章:

  • MCM 箱模型建模方法及大气 O3 来源解析实用干货
  • 【独家】华为OD机试 - 最长连续交替方波信号(C 语言解题)
  • 代码随想录算法训练营第二十一天打卡 | 530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先
  • 免费下载丨一看即会,Serverless 技术进阶必读百宝书
  • SQL语句的加锁方式 - Mysql 锁机制
  • C#开发的OpenRA的游戏主界面怎么样创建4
  • 覆盖5大主流开发平台的报表控件,它值得你一看
  • 【冲刺蓝桥杯的最后30天】day4
  • spring boot actuator 动态修改日志级别
  • 兴达易控Modbus转Profinet网关连接1200Profinet转modbus接三菱A800变频器案例
  • 「SAP ABAP」OPEN SQL(四)【FROM语句】
  • 一文吃透 SpringMVC 中的转发和重定向
  • Hbase操作命令
  • 1>LINK : fatal error LNK1104: cannot open file ‘libconvtname.obj‘
  • 数据结构——链表OJ题目讲解(1)
  • LeetCode_二分搜索_困难_154.寻找旋转排序数组中的最小值 II
  • 面向对象设计模式:创建型模式之建造者模式
  • 集成学习boosting、bagging、stacking
  • 数据模型(上):模型分类和模型组成
  • 郑州轻工业大学2022-2023(2) 数据结构题目集 - ZZULI
  • 【Python语言基础】——Python MySQL Drop Table
  • 2023美团面试真题
  • 【微信小程序开发全流程】篇章0:基于JavaScript开发的校园综合类微信小程序的概览
  • 如何分析sql性能
  • 市场营销书籍推荐:《经理人参阅:市场营销》
  • WPF 控件专题 MediaElement控件详解
  • 基于SpringBoot+SpringCloud+Vue前后端分离项目实战 --开篇
  • 循环队列的实现
  • MTK平台开发入门到精通(休眠唤醒篇)休眠唤醒LPM框架
  • ThreadLocal详解