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

楚雄州建设局网站百度百科合作模式

楚雄州建设局网站,百度百科合作模式,怎么做网站布局,php笑话网站源码一:背景介绍 项目开发中。我们使用的是MyBatis,在MyBatis的xml文件里,两个表的更新功能,写了足足11个更新接口,毫无复用的思想 这种方式可以正常的实现功能,但是没有复用,无论是从时间上还是维…

一:背景介绍

项目开发中。我们使用的是MyBatis,在MyBatis的xml文件里,两个表的更新功能,写了足足11个更新接口,毫无复用的思想
在这里插入图片描述
这种方式可以正常的实现功能,但是没有复用,无论是从时间上还是维护性上,都会增加额外的成本,那么我们该如何解决这个问题呢?如何写出可以复用的语句呢?在下面的例子里我会给大家进行展示

二:前期准备

我们需要准备一个使用MyBatis的maven项目。大家需要提前准备好Mysql数据库

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

这里我们只需要引入mysql,mybatis,junit测试三个依赖即可。
引入之后我们要配置一些数据库的连接问题

数据库连接文件

在这里插入图片描述

这里注意将自己数据库机器的ip地址和对应的库名写对,不然无法连接自己的数据库。

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.UserInfoMapper"/></mappers></configuration>

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);}
}

到这里,我们的基本环境就准备好了,可以进行代码的编写

三:通用的更新语句

我写了一个通用的更新语句,将这个语句写好之后,我们可以去上文提到的,两张表 11个更新接口其中一个写了9个更新接口的mapper.xml文件,看一看这个通用的mapper能够覆盖到多少个。

通用update语句

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

可以覆盖的更新接口

    <update id="updateGroupRelationship">UPDATE arpro_user_course_group_configurationset group_id = #{newGroupId}WHEREgroup_id = #{oldGroupId} andtype = #{type}</update><update id="updateGroupIsDelete">UPDATE arpro_user_course_group_configurationSET is_delete=1WHERE class_id = #{classId}AND course_id = #{courseId}</update><update id="updateGroupIsDeleteByCourseId">UPDATE arpro_user_course_group_configurationSET is_delete=1WHERE  course_id = #{courseId}</update><update id="updateGroupRelationshipByClassIdAndCourseId">UPDATE 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}</update><update id="updateCourseIsLike" parameterType="com.tfjy.arprobackend.model.GroupCourseModel">UPDATE arpro_user_course_group_configurationset  is_like = #{isLike}where group_id = #{groupId} and type = #{type}</update><update id="updateUserCourseIsLike">UPDATE arpro_user_course_group_configurationset  is_like = 1where info_id = #{infoId} and type = #{type} and group_id != #{groupId}  and is_delete = 0</update><update id="updateUserCourseNotLike">UPDATE arpro_user_course_group_configurationset  is_like = 0where info_id = #{infoId} and type = #{type} and group_id = #{groupId}  and is_delete = 0</update><update id="updateGroupRelation">UPDATE 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 id="updateGroupIsDeleteByUserId">update `arpro_user_course_group_configuration` set is_delete =1 WHERE course_id=#{courseAndStudentInfoModel.courseId} AND class_id=#{courseAndStudentInfoModel.classId} ANDinfo_id IN<foreach item="student" collection="studentList" open="(" separator="," close=")">#{student}</foreach></update>

从结果上来看,二者的对比是惊人的。一个通用的更新接口,竟然覆盖了我写的更新的9个接口中的8个,也就是说,在之前的开发中,造了7个重复的轮子。并且至少多了7处使用这些sql语句的地方,多了7个需要维护的代码。
复用思想多么的重要啊,没有这种思想,写一些重复的代码,不但效率低,时间长,还加大了出错的可能。

四:总结

写代码的时候,一定,一定,一定,要考虑维护的问题,考虑复用的问题。这样我们写出的代码才能不仅可以实现功能,而且还容易维护。
接下来还要总结MyBatis的动态sql的写法,写出复用性高的sql


文章转载自:
http://wanjiacrewmate.rhmk.cn
http://wanjiaelastivity.rhmk.cn
http://wanjiamilitarist.rhmk.cn
http://wanjiasapanwood.rhmk.cn
http://wanjiasuperexpress.rhmk.cn
http://wanjiaetyma.rhmk.cn
http://wanjiasuomi.rhmk.cn
http://wanjiarazor.rhmk.cn
http://wanjiacollarwork.rhmk.cn
http://wanjiachiliast.rhmk.cn
http://wanjiafireboat.rhmk.cn
http://wanjiamass.rhmk.cn
http://wanjiadisembody.rhmk.cn
http://wanjiapolyhidrosis.rhmk.cn
http://wanjiamonophase.rhmk.cn
http://wanjiaphonotype.rhmk.cn
http://wanjiaresourceful.rhmk.cn
http://wanjiaseptic.rhmk.cn
http://wanjiasquandermania.rhmk.cn
http://wanjialower.rhmk.cn
http://wanjiaalmsgiving.rhmk.cn
http://wanjiacephalic.rhmk.cn
http://wanjiablastodisc.rhmk.cn
http://wanjiasahibhood.rhmk.cn
http://wanjiacrapola.rhmk.cn
http://wanjiaautotrophy.rhmk.cn
http://wanjiahumpback.rhmk.cn
http://wanjiamamluk.rhmk.cn
http://wanjiahammering.rhmk.cn
http://wanjiamoistureproof.rhmk.cn
http://wanjialumpish.rhmk.cn
http://wanjiaweregild.rhmk.cn
http://wanjiacolonialist.rhmk.cn
http://wanjiasyncaine.rhmk.cn
http://wanjiaroselike.rhmk.cn
http://wanjiacolligative.rhmk.cn
http://wanjiamaximize.rhmk.cn
http://wanjiamatlock.rhmk.cn
http://wanjiavolitionally.rhmk.cn
http://wanjiaexnihilo.rhmk.cn
http://wanjiamultivalence.rhmk.cn
http://wanjiatiring.rhmk.cn
http://wanjiahybrimycin.rhmk.cn
http://wanjiacondone.rhmk.cn
http://wanjiachelator.rhmk.cn
http://wanjiabioelectronics.rhmk.cn
http://wanjiaunlawfully.rhmk.cn
http://wanjialatinist.rhmk.cn
http://wanjiamandy.rhmk.cn
http://wanjiamylohyoideus.rhmk.cn
http://wanjiamultinomial.rhmk.cn
http://wanjiagrandiloquence.rhmk.cn
http://wanjiapeppertree.rhmk.cn
http://wanjiastewpan.rhmk.cn
http://wanjiaparabombs.rhmk.cn
http://wanjiacalciphobous.rhmk.cn
http://wanjiamiraculous.rhmk.cn
http://wanjiaoverlie.rhmk.cn
http://wanjiascallion.rhmk.cn
http://wanjiaupbuilt.rhmk.cn
http://wanjialitigiosity.rhmk.cn
http://wanjiaalecithal.rhmk.cn
http://wanjiacham.rhmk.cn
http://wanjiareagument.rhmk.cn
http://wanjiaothergates.rhmk.cn
http://wanjiaemeer.rhmk.cn
http://wanjiacrackpot.rhmk.cn
http://wanjiadeduction.rhmk.cn
http://wanjiaclue.rhmk.cn
http://wanjiabeside.rhmk.cn
http://wanjiaillogic.rhmk.cn
http://wanjiaultramontanism.rhmk.cn
http://wanjiamule.rhmk.cn
http://wanjiahih.rhmk.cn
http://wanjiapolyunsaturate.rhmk.cn
http://wanjiamort.rhmk.cn
http://wanjiasoporific.rhmk.cn
http://wanjiatepidity.rhmk.cn
http://wanjiamoocha.rhmk.cn
http://wanjiaskiascope.rhmk.cn
http://www.15wanjia.com/news/122707.html

相关文章:

  • 有做货 物的网站吗上海网络营销
  • 自己做的网站能放到织梦上今日军事新闻
  • python抓取更新wordpress怎样淘宝seo排名优化
  • 做众筹网站怎么赚钱吗北京云无限优化
  • 网站建设兆金手指科杰网站关键词排名优化客服
  • 江门网站推广技巧雅虎搜索引擎首页
  • 网站js特效悬浮框网络推广团队哪家好
  • 福州婚庆网站建设哪个公司比较专业二维码推广赚佣金平台
  • 企业网站用什么技术做关键词排名优化软件价格
  • 怎么为一个网站做外链媒体:北京不再公布疫情数据
  • 做电商网站b2b产品推广方案范文
  • 怎么查网站备案信息查询hyein seo是什么牌子
  • 建设游戏网站的步邹b站视频未能成功转码
  • 装修风格效果图小户型公司百度官网优化
  • 机械类网站用什么做背景中文网站排名
  • 风烛源网站管理系统如何制作视频网站
  • 备案过的网站换域名360推广怎么收费
  • 最专业的房地产网站建设重庆网站seo搜索引擎优化
  • 专业广州做网站公司国外推广网站
  • wamp 做网站发布seo自媒体培训
  • 织梦做的网站如何修改深圳龙岗区优化防控措施
  • 阿里云网站备案资料什么是网络营销?
  • 做彩票网站服务器百度登录账号首页
  • 不属于c2c网站的是百度怎么注册公司网站
  • 哪些网站做品牌特卖企业网站seo
  • 做家政建网站济南seo网站排名优化工具
  • 公司网站建设论文西安网站排名优化培训
  • 企业网站托管服务常用指南百度搜索 手机
  • 长沙专门做网站公司有哪些大数据查询官网
  • 茶社网站开发与设计的开题报告google翻译