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

网站建设相关法律法规郑州网站建设怎么样

网站建设相关法律法规,郑州网站建设怎么样,品牌网站升级,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
http://www.hkea.cn/news/14404998/

相关文章:

  • 做一个公司网站一般多少钱希望小学学校网站建设方案
  • 企业做网站 里面都写什么wordpress无法进入admin
  • 不用ftp可以做网站吗wordpress小说站模板
  • 株洲网站开发公司哈尔滨建设网站平台
  • 房产网站的建设计算机网站建设文献综述
  • 昆山设计网站公司腾讯营销平台
  • 郑州做网站hnqfu网站建设代理多少钱
  • 网站常见故障河南做个人网站
  • 上海seo网站推广公司西安seo优化
  • 昆明建设局官方网站网站建设可以用350摸板
  • 怎么用ip做网站wordpress安装后只有英文版
  • 网站策划需求dw网页制作基础知识
  • 能做wordpress的网站花都营销型网站
  • 手机界面设计网站wordpress 评论链接
  • 网站制作技术支持一键设计logo
  • 千灯网站建设简单的静态网站首页
  • 站长工具seo优化系统wordpress聊天室模板
  • 江苏网站建设网络公司网店网页制作
  • 临漳手机网站建设怎么恢复wordpress设定值
  • 佛山优化网站推广做网站优化的协议书
  • 杭州微信网站制作今天邯郸下的紧急通知
  • 网站备案初审wordpress 个人 主题
  • 做网站推广书范法吗wordpress多站点搭建
  • 做婚礼网站的公司酉阳网站制作
  • 网站开发的实践报告安徽理工大学新校区建设网站
  • php 网站 模板为企业制定网络营销方案
  • 企业形象网站建设WordPress中文相册
  • 网站上传大马后怎么做php网站怎么做post订单
  • 怎样做机械租赁的网站网站建设的关键细节
  • 网站建设中 html南宁网站优化推广