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

案例27-单表从9个更新语句调整为2个

目录

一:背景介绍

二:思路&方案

三:过程

       

1.项目结构

        2.准备一个普通的maven项目,部署好mysql数据库

        3.在项目中引入pom依赖

        5.编写MyBitis配置文件

        6.编写Mysql配置类

        7.编写通用Update语句

        8.项目启动类

四:总结


一:背景介绍

        在项目开发的过程中,我们编写sql语句的时候通常不会考虑到我要编写的sql语句之前是不是有人写过或者是不是可以复用别人的sql语句。而是直接我用了什么业务就直接写一个对应的sql语句。甚至有的时候我们自己写了之后都不清楚了。这样就没有使代码进行复用,也会带来代码带以维护的问题。

二:思路&方案

        1.编写通用的sql语句,使其满足百分之九十的sql语句。

        2.让代码的复用性更高。减少后期代码的维护成本。

三:过程

       

1.项目结构

                

                

        2.准备一个普通的maven项目,部署好mysql数据库

        3.在项目中引入pom依赖

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

        4.在db.properties配置文件中添加连接数据库的语句

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:服务器IP:3306/wzill?useSSL=false&;useUnicode=true;CharacterEncoding=UTF-8
username=数据库账号
password=数据库密码

        5.编写MyBitis配置文件

<?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><!--<typeAlias type="com.kuang.pojo.User" alias="User"/>--><!--可以指定一个包名,MyBatis会在包名下面搜索需要的Java Bean--><package name="com.wzl.CommonUse.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="com.wzl.CommonUse.dao.UserMapper"/></mappers>
</configuration>

        6.编写Mysql配置类

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.编写通用Update语句

   <update id="updateUser">update user<trim prefix="SET" suffixOverrides=","><if test="userPojo.uid != null">uid = #{userPojo.uid}</if><if test="userPojo.id != null">id = #{userPojo.id}</if><if test="userPojo.name != null">name = #{userPojo.name}</if><if test="userPojo.age != null">age = #{userPojo.age}</if><if test="userPojo.username != null">username = #{userPojo.username}</if><if test="userPojo.password != null">password = #{userPojo.password}</if><if test="userPojo.email != null">email = #{userPojo.email}</if><if test="userPojo.phone != null">phone = #{userPojo.phone}</if><if test="userPojo.addr != null">addr = #{userPojo.addr}</if><if test="userPojo.state != null">state = #{userPojo.state}</if></trim>where state = 2<if test="conditionParam.addr != null"> and addr = #{conditionParam.addr}</if><if test="conditionParam.phone != null">and phone = #{conditionParam.phone}</if><if test="conditionParam.name != null">and name = #{conditionParam.name}</if><if test="conditionParam.age != null">and age = #{conditionParam.age}</if><if test="conditionParam.username != null">and username = #{conditionParam.username}</if><if test="conditionParam.password != null">and password = #{conditionParam.password}</if></update>

        这样我们在编写对于 user表中的数据进行更新的时候只需要这一个通用的语句就可以了。涉及到批量更新的话,还是需要新的sql语句的。相信通过不断的学习,我们也可以把批量的和单独的进行一个代码的复用。

        8.项目启动类

public class Client{@Testpublic void test(){//获取数据库连接SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);//进行更新操作UserPojo UserPojo1 = new UserPojo();UserPojo UserPojo2 = new UserPojo();//假删除某个课的某个班的所有信息UserPojo1.setUsername("223667994");UserPojo2.setUsername("bbb");//进行调用userMapper.updateUser(UserPojo1,UserPojo2);}
}

四:总结

        在写代码之前先去看一下项目中是否有类似的业务逻辑,是否有可以复用的代码。来减少我们做无用功。代码的复用对于我们后期对于项目维护的成本非常低。

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

相关文章:

  • Wordpress paid-memberships-pro plugins CVE-2023-23488未授权SQLi漏洞分析
  • 【JavaWeb篇】JSTL相关知识点总结
  • 【蓝桥杯刷题】坑爹的负进制转换
  • react+antdpro+ts实现企业级项目二:Strapi及认证登陆模块
  • Android ANR trace日志如何导出
  • Windows SSH 配置和SCP的使用
  • liunx 安装redsi和连接
  • 接口里面可以写实现方法吗【可以】 、接口可以多继承吗【可以】
  • 【YOLOv8/YOLOv7/YOLOv5/YOLOv4/Faster-rcnn系列算法改进NO.57】引入可形变卷积
  • 统计学习--三种常见的相关系数
  • 基于Django4.1.4的入门学习记录
  • C++ Butterworth N阶滤波器设计
  • UXP下不用任何框架创建自己的插件并试运行
  • mac修改国内源快速安装brew
  • Me-and-My-Girlfriend-1靶场通关
  • 2.6 棋盘覆盖
  • JMU软件20 大数据技术复习(只写了对比18提纲的变动部分)
  • MySQL底层存储B-Tree和B+Tree原理分析
  • 基于Vue+Vue-cli+webpack搭建渐进式高可维护性前端实战项目
  • 第十三章:Java反射机制
  • iLok USB不识别怎么办?
  • 【LeetCode与《代码随想录》】二叉树篇:做题笔记与总结-JavaScript版
  • 机器人运动|浅谈Time Elastic Band算法
  • 【Linux】网络基础(1)
  • 限流算法详解
  • Spark/Hive
  • HashMap底层的实现原理(JDK8)
  • 操作系统-整理
  • 系统换行符的思考
  • Wwise集成到unreal