网站建设相关法律法规,郑州网站建设怎么样,品牌网站升级,php访问网站一#xff1a;背景介绍
项目开发中。我们使用的是MyBatis#xff0c;在MyBatis的xml文件里#xff0c;两个表的更新功能#xff0c;写了足足11个更新接口#xff0c;毫无复用的思想 这种方式可以正常的实现功能#xff0c;但是没有复用#xff0c;无论是从时间上还是维…一背景介绍
项目开发中。我们使用的是MyBatis在MyBatis的xml文件里两个表的更新功能写了足足11个更新接口毫无复用的思想 这种方式可以正常的实现功能但是没有复用无论是从时间上还是维护性上都会增加额外的成本那么我们该如何解决这个问题呢如何写出可以复用的语句呢在下面的例子里我会给大家进行展示
二前期准备
我们需要准备一个使用MyBatis的maven项目。大家需要提前准备好Mysql数据库
引入pom依赖 dependenciesdependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.47/version/dependency!--mybatis--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.2/version/dependency!--junit--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency/dependencies这里我们只需要引入mysqlmybatisjunit测试三个依赖即可。 引入之后我们要配置一些数据库的连接问题
数据库连接文件 这里注意将自己数据库机器的ip地址和对应的库名写对不然无法连接自己的数据库。
MyBatis配置文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttps://mybatis.org/dtd/mybatis-3-config.dtd
!--configuration mybatis的核心配置文件--
configuration
!--引入外部配置文件--properties resourcedb.properties/!--配置--settings!--标准日志工厂设置--setting namelogImpl valueSTDOUT_LOGGING/
!--显示的开启全局缓存--setting namecacheEnabled valuetrue//settings!--可以给实体类取别名--typeAliases!--可以指定一个包名MyBatis会在包名下面搜索需要的Java Bean--package nameorg.example.pojo//typeAliases!--environments 后面的s表示这是一个复数可以编写多套环境 default表示默认的环境为development--environments defaultdevelopment!--编写一套环境 名称为configuration--environment iddevelopment!--jdbc的事务管理--transactionManager typeJDBC/!--配置数据库相关数据--dataSource typePOOLEDproperty namedriver value${driver}/!--userSSL是一个按权连接 amp是一个转移符 等同于and CharacterEncodingutf-8可以保证输入数据库的数据不乱码--property nameurl value${url}/property nameusername value${username}/property namepassword value${password}//dataSource/environment/environments!--绑定接口--mappersmapper classorg.example.dao.UserInfoMapper//mappers/configurationMyBatis配置类
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);}
}到这里我们的基本环境就准备好了可以进行代码的编写
三通用的更新语句
我写了一个通用的更新语句将这个语句写好之后我们可以去上文提到的两张表 11个更新接口其中一个写了9个更新接口的mapper.xml文件看一看这个通用的mapper能够覆盖到多少个。
通用update语句 update idupdateCourseGroupConfiguration parameterTypeorg.example.pojo.UserCourseGroupConfigurationPojoupdate arpro_user_course_group_configurationtrim prefixSET suffixOverrides,1if testinfoId ! nullinfo_id #{infoId}/ifif testcourseId ! nullcourse_id #{courseId}/ifif testclassId ! nullclass_id #{classId}/ifif testgroupId ! nullgroup_id #{groupId}/ifif testtype ! nulltype #{type}/ifif testisDelete ! nullis_delete #{isDelete}/ifif testremark ! nullremark #{remark}/ifif testisLike ! nullis_like #{isLike}/if/trimwhere is_delete 0if testinfoId ! null and info_id #{infoId}/ifif testcourseId ! nulland course_id #{courseId}/ifif testclassId ! nulland class_id #{classId}/ifif testgroupId ! nulland group_id #{groupId}/ifif testisLike ! nulland is_like #{isLike}/ifif testtype ! nulland type #{type}/if/update可以覆盖的更新接口 update idupdateGroupRelationshipUPDATE arpro_user_course_group_configurationset group_id #{newGroupId}WHEREgroup_id #{oldGroupId} andtype #{type}/updateupdate idupdateGroupIsDeleteUPDATE arpro_user_course_group_configurationSET is_delete1WHERE class_id #{classId}AND course_id #{courseId}/updateupdate idupdateGroupIsDeleteByCourseIdUPDATE arpro_user_course_group_configurationSET is_delete1WHERE course_id #{courseId}/updateupdate idupdateGroupRelationshipByClassIdAndCourseIdUPDATE arpro_user_course_group_configurationset group_id #{groupCourseModel.newGroupId} ,is_like #{isLike}WHEREtype #{groupCourseModel.type} and class_id #{groupCourseModel.classId} and course_id #{groupCourseModel.courseId} and info_id #{groupCourseModel.infoId}/updateupdate idupdateCourseIsLike parameterTypecom.tfjy.arprobackend.model.GroupCourseModelUPDATE arpro_user_course_group_configurationset is_like #{isLike}where group_id #{groupId} and type #{type}/updateupdate idupdateUserCourseIsLikeUPDATE arpro_user_course_group_configurationset is_like 1where info_id #{infoId} and type #{type} and group_id ! #{groupId} and is_delete 0/updateupdate idupdateUserCourseNotLikeUPDATE arpro_user_course_group_configurationset is_like 0where info_id #{infoId} and type #{type} and group_id #{groupId} and is_delete 0/updateupdate idupdateGroupRelationUPDATE arpro_user_course_group_configurationset group_id #{newGroupId} ,info_id #{newInfoId}WHEREtype 1 and class_id #{classId} and course_id #{courseId} and info_id #{oldInfoId}/update
暂时无法覆盖到的更新接口 update idupdateGroupIsDeleteByUserIdupdate arpro_user_course_group_configuration set is_delete 1 WHERE course_id#{courseAndStudentInfoModel.courseId} AND class_id#{courseAndStudentInfoModel.classId} ANDinfo_id INforeach itemstudent collectionstudentList open( separator, close)#{student}/foreach/update从结果上来看二者的对比是惊人的。一个通用的更新接口竟然覆盖了我写的更新的9个接口中的8个也就是说在之前的开发中造了7个重复的轮子。并且至少多了7处使用这些sql语句的地方多了7个需要维护的代码。 复用思想多么的重要啊没有这种思想写一些重复的代码不但效率低时间长还加大了出错的可能。
四总结
写代码的时候一定一定一定要考虑维护的问题考虑复用的问题。这样我们写出的代码才能不仅可以实现功能而且还容易维护。 接下来还要总结MyBatis的动态sql的写法写出复用性高的sql