【教学典型案例】28.单表的11个Update接口--MyBatis
迪丽瓦拉
2024-05-30 08:43:43
0

目录

  • 一:背景介绍
  • 二:前期准备
    • 引入pom依赖
    • MyBatis配置文件
    • 数据库连接文件
    • MyBatis配置类
  • 三:代码编写
    • Mapper编写
      • 接口
      • 通用mapper
      • 实体pojo
      • junit测试编写
    • 测试结果
  • 四:总结

一:背景介绍

在进行项目开发编写更新接口时,编写了11个Update接口,这样可以实现最后的功能效果,但是后期如果需要维护的话是十分难得。也无法进行复用和扩展。
在这里插入图片描述
下边的例子中,给大家展示了如何编写可复用、可扩展、维护成本低的SQL语句。

二:前期准备

代码环境:Java MyBatis maven项目,Mysql

引入pom依赖

需要引入mysql、mybatis、junit三个依赖即可。

    mysqlmysql-connector-java5.1.47org.mybatismybatis3.5.2junitjunit4.12test

MyBatis配置文件

实体类包的路径和接口mapper的路径需要改成自己的路径
在这里插入图片描述








数据库连接文件

先进行数据库的连接
在这里插入图片描述
在这里插入图片描述

MyBatis配置类

//sqlSessionFactory(获取资源) 必然是构建 sqlSession
//该工具类的作用时读取配置文件 获取sqlSessionFactory工厂
public class MybatisUtils {private  static  SqlSessionFactory sqlSessionFactory; //该代码的作用是提升作用域 可以让getSqlSession方法使用sqlSessionFactorystatic{ //静态代码块:一旦初始化就加载try {//使用Mybatis第一步:获取sqlSessionFactory对象String resource = "mybatis-config.xml"; //获取资源,直接读到mybatis-config.xmlInputStream inputStream = Resources.getResourceAsStream(resource); //需要用到输入流(InputStream) 把resource类加载进来sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//通过build把输入流加载进来} catch (IOException e) {e.printStackTrace();}}//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句public static SqlSession getSqlSession() { //该方法会返回一个SqlSession类
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//        return sqlSession;return sqlSessionFactory.openSession(true);//openSession中有自动commit(提交)事务的方法,加上true就能实现}}

以上就是我们所需的基本环境啦,接下来就可以进行代码的编写啦~!

三:代码编写

Mapper编写

接口

public interface UserCourseGroupConfigurationMapper {void updateCourseGroupConfiguration(@Param("reviseParam")UserCourseGroupConfigurationPojo reviseParam, @Param("conditionParam")UserCourseGroupConfigurationPojo conditionParam);
}

通用mapper

    update arpro_user_course_group_configurationinfo_id = #{reviseParam.infoId}course_id = #{reviseParam.courseId}class_id = #{reviseParam.classId}group_id = #{reviseParam.groupId}type = #{reviseParam.type}is_delete = #{reviseParam.isDelete}remark = #{reviseParam.remark}is_like = #{reviseParam.isLike}where is_delete = 0 and info_id = #{conditionParam.infoId}and course_id = #{conditionParam.courseId}and class_id = #{conditionParam.classId}and group_id = #{conditionParam.groupId}and is_like = #{conditionParam.isLike}and type = #{conditionParam.type}

实体pojo

@Data
public class UserCourseGroupConfigurationPojo {/*** 主键id*/private BigInteger id;/*** 信息id是用户表的关联键*/private BigInteger infoId;/*** 课程id*/private BigInteger courseId;/*** 班级id*/private BigInteger classId;/*** 分组id*/private BigInteger groupId;/*** 我学的课还是我教的课*/private Integer  type;/*** 是否删除 0 否 1是*/private Integer isDelete;/*** 创建时间*/private Date createTime;/*** 更新时间*/private Date updateTime;/*** 备注*/private String remark;/*** 分组名称*/private String groupName;/*** 分组顺序*/private Integer sequence;/*** 是否是默认分组*/private Integer isDefault;/*** 是否为我喜欢分组 0其他分组,1特别关注分组*/private Integer isMostLike;public BigInteger getId() {return id;}public void setId(BigInteger id) {this.id = id;}public BigInteger getInfoId() {return infoId;}public void setInfoId(BigInteger infoId) {this.infoId = infoId;}public BigInteger getCourseId() {return courseId;}public void setCourseId(BigInteger courseId) {this.courseId = courseId;}public BigInteger getClassId() {return classId;}public void setClassId(BigInteger classId) {this.classId = classId;}public BigInteger getGroupId() {return groupId;}public void setGroupId(BigInteger groupId) {this.groupId = groupId;}public Integer getType() {return type;}public void setType(Integer type) {this.type = type;}public Integer getIsDelete() {return isDelete;}public void setIsDelete(Integer isDelete) {this.isDelete = isDelete;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}public String getRemark() {return remark;}public void setRemark(String remark) {this.remark = remark;}public String getGroupName() {return groupName;}public void setGroupName(String groupName) {this.groupName = groupName;}public Integer getSequence() {return sequence;}public void setSequence(Integer sequence) {this.sequence = sequence;}public Integer getIsDefault() {return isDefault;}public void setIsDefault(Integer isDefault) {this.isDefault = isDefault;}public Integer getIsMostLike() {return isMostLike;}public void setIsMostLike(Integer isMostLike) {this.isMostLike = isMostLike;}
}

junit测试编写

public class UserCourseGroupConfigurationTest {@Testpublic void test(){//获取数据库连接SqlSession sqlSession = MybatisUtils.getSqlSession();UserCourseGroupConfigurationMapper userCourseGroupConfigurationMapper = sqlSession.getMapper(UserCourseGroupConfigurationMapper.class);//进行更新操作UserCourseGroupConfigurationPojo reviseParam = new UserCourseGroupConfigurationPojo();UserCourseGroupConfigurationPojo conditionParam = new UserCourseGroupConfigurationPojo();//假删除某个课的某个班的所有信息/*reviseParam.setIsDelete(0);conditionParam.setCourseId(BigInteger.valueOf(223667994));conditionParam.setClassId(BigInteger.valueOf(56496292));*///reviseParam是修改后的数据reviseParam.setCourseId(new BigInteger("315282991842590721"));//conditionParam是修改条件conditionParam.setCourseId(new BigInteger("315282991842590720"));conditionParam.setType(1);//进行调用userCourseGroupConfigurationMapper.updateCourseGroupConfiguration(reviseParam,conditionParam);}
}

测试结果

没执行代码之前的数据库表数据
在这里插入图片描述
在这里插入图片描述
执行成功后,数据库中type=1的数据不见了
在这里插入图片描述
然后使用course_id=315282991842590721修改后的数据进行查询
在这里插入图片描述

四:总结

通过以上分析,更加认识到了面向对象的思想是多么的伟大。我们一直都说做软件设计要使用面向对象的思想:可复用、可扩展、可维护,可是真的做起来没有做到知行合一。

相关内容