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

哇哈哈电子商务网站建设策划书seo外链发布

哇哈哈电子商务网站建设策划书,seo外链发布,固戍做网站的公司,园林景观设计公司需要什么资质一:背景介绍 项目开发中。我们使用的是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://www.tj-hxxt.cn/news/12951.html

相关文章:

  • wordpress内存seo网站管理
  • 江苏网站设计4414站长平台
  • 盗版软件做的网站会被起诉吗重大军事新闻最新消息
  • c 网站开发简单实例网页设计框架图
  • 定制营销型网站制作2023网站seo
  • .net和php那个做网站好推广平台排名
  • 营销型网站建设定制白杨seo课程
  • 商城网站用什么做常用的关键词优化策略有哪些
  • 电影网站怎么做的自己怎么开电商平台
  • 北京网站建设华大app推广接单网
  • 天津网站设计网站制作平台开发
  • 遵义网站建公司网络推广员每天的工作是什么
  • 武汉模板开发建站合肥网
  • 青岛有没有专门做淘宝网站看广告赚钱一天50元
  • wordpress如何把网站语言改成英文在线搜索资源
  • 宿迁 网站制作百度推广按点击收费
  • seo公司彼亿营销深圳seo
  • wordpress代码片段专业网站seo推广
  • 快递物流网站建设开发具备哪些功能一键生成原创文案
  • 阿里巴巴国际贸易网网站页面优化内容包括哪些
  • 做网站北京如何做一个自己的网页
  • 做网站 看什么书网络推广费用大概价格
  • 做网站堵怕犯法吗100个裂变营销案例
  • 东莞建站响应式网站多少钱seo站内优化培训
  • 河南省新闻奖评选通知网站推广关键词排名优化
  • 网销是什么该怎么做宁波seo推广公司排名
  • 国家高新技术企业有哪些seo搜索优化排名
  • 天津定制网站建设公司郑州网络营销公司排名
  • 网页制作3个网页的网站图片谷歌优化师
  • 江津哪个网站可以做顺风车近三天时政热点