摘要

最近将微人事这个开源项目进行了复现,这篇文章记录mybaits访问数据库这一块。

其中MyBatis是一个流行的持久层框架,支持自定义SQL、存储过程和高级映射。MyBatis消除了几乎所有的JDBC代码、手动设置参数和检索结果。MyBatis可以使用简单的XML或注释进行配置,实现对数据库的访问。

项目结构

  • 其中mapper是持久层,model是实体类,service是逻辑层,web是表现层。

model和mapper

首先需要定义实体类:



具体如下:

public class Department implements Serializable {
private Integer id; private String name; private Integer parentId; private String depPath; private Boolean enabled; private Boolean isParent;
//注意
private List<Department> children = new ArrayList<>(); private Integer result; //getter and setter()
//...
}

再来看数据库中的数据:



从表中,我们看到表中的列并没有完全包含Department类中的所有成员变量,那是怎样实现的了?这就要看看mapper是怎样实现的了。



从图中可以看到,在mapper层中采用了接口和xml文件的方式访问数据库,没有使用注解的方式。

其中DepartmentMapper接口中定义了诸如删除、插入和更新等操作数据库的方法。

public interface DepartmentMapper {
int deleteByPrimaryKey(Integer id); int insert(Department record); int insertSelective(Department record); Department selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Department record); int updateByPrimaryKey(Department record); List<Department> getAllDepartmentsByParentId(Integer pid); void addDep(Department dep); void deleteDepById(Department dep); List<Department> getAllDepartmentsWithOutChildren();
}

接着来看看xml文件是怎样实现的:

//前两句应该就是模板了。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
//这儿定义命名空间,常见就是用Mapper的路径。
<mapper namespace="com.codexwj.vhr03.mapper.DepartmentMapper">
//这里注意,在上面的数据表中不完全包含实体类Department中的成员变量,那在这里就需要定义数据表中需要有哪些列了。
<resultMap id="BaseResultMap" type="com.codexwj.vhr03.model.Department">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="parentId" property="parentId" jdbcType="INTEGER"/>
<result column="depPath" property="depPath" jdbcType="VARCHAR"/>
<result column="enabled" property="enabled" jdbcType="BIT"/>
<result column="isParent" property="isParent" jdbcType="BIT"/>
</resultMap>
//这里把children这个属性添加到DepartmentWithChildren中,它是继承于BaseResultMap的。
<resultMap id="DepartmentWithChildren" type="com.*.model.Department" extends="BaseResultMap">
<collection property="children" ofType="com.*.model.Department" select="com.*.mapper.DepartmentMapper.getAllDepartmentsByParentId" column="id"/>
</resultMap>
//表明数据表的列有哪些
<sql id="Base_Column_List">
id, name, parentId, depPath, enabled, isParent
</sql>
// 这里进行数据输出的时候只访问包含于Base_Column_List的数据。
<select id="getAllDepartmentsWithOutChildren" resultMap="BaseResultMap">
select
<include
refid="Base_Column_List">
</include>
from department;
</select>
// 对传入的值进行非空判断,并且只对非空的值进行赋值。
<insert id="insertSelective" parameterType="com.codexwj.vhr03.model.Department">
insert into department
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="parentId != null">
parentId,
</if>
<if test="depPath != null">
depPath,
</if>
<if test="enabled != null">
enabled,
</if>
<if test="isParent != null">
isParent,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="parentId != null">
#{parentId,jdbcType=INTEGER},
</if>
<if test="depPath != null">
#{depPath,jdbcType=VARCHAR},
</if>
<if test="enabled != null">
#{enabled,jdbcType=BIT},
</if>
<if test="isParent != null">
#{isParent,jdbcType=BIT},
</if>
</trim>
</insert> //同理insertSelective
<update id="updateByPrimaryKeySelective" parameterType="com.codexwj.vhr03.model.Department">
update department
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="parentId != null">
parentId = #{parentId,jdbcType=INTEGER},
</if>
<if test="depPath != null">
depPath = #{depPath,jdbcType=VARCHAR},
</if>
<if test="enabled != null">
enabled = #{enabled,jdbcType=BIT},
</if>
<if test="isParent != null">
isParent = #{isParent,jdbcType=BIT},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>

思路: 先定义实体类,建立mapper接口,利用xml文件进行配置实现对数据库的增删查改。

如有错误,欢迎指正

微人事项目-mybatis-持久层的更多相关文章

  1. MyBatis持久层框架学习之01 MyBatis的起源和发展

    一.MyBatis的简介  MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.    MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. MyB ...

  2. MyBatis持久层框架使用总结 转载

    MyBatis持久层框架使用总结   MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google co ...

  3. MyBatis持久层框架使用总结

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 2 ...

  4. 预研报告——MyBatis持久层的demo

    一.预研任务介绍和预研目标 任务介绍: 与 Hibernate 相比, MyBatis 是一个半自动化的持久层框架,以轻量级.效率高.原生代而好评如潮.虽然有在分享会上大致讲解,但是还是重新梳理成文字 ...

  5. Spring集成MyBatis持久层框架

    一.MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的JDBC代码和手动设置参数以及获取结果集,可以使用简单的XML ...

  6. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  7. Mybatis(一):手写一套持久层框架

    作者 : 潘潘 未来半年,有幸与导师们一起学习交流,趁这个机会,把所学所感记录下来. 「封面图」 自毕业以后,自己先创业后上班,浮沉了近8年,内心着实焦躁,虽一直是走科班路线,但在技术道路上却始终没静 ...

  8. 微人事 star 数超 10k,如何打造一个 star 数超 10k 的开源项目

    看了下,微人事(https://github.com/lenve/vhr)项目 star 数超 10k 啦,松哥第一个 star 数过万的开源项目就这样诞生了. 两年前差不多就是现在这个时候,松哥所在 ...

  9. spring boot V部落 V人事项目

    公司倒闭 1 年多了,而我在公司倒闭时候做的开源项目,最近却上了 GitHub Trending,看着这个数据,真是不胜唏嘘. 缘起 2017 年 11 月份的时候,松哥所在的公司因为经营不善要关门了 ...

随机推荐

  1. react第十六单元(redux的认识,redux相关api的掌握)

    第十六单元(redux的认识,redux相关api的掌握) #课程目标 掌握组件化框架实现组件之间传参的几种方式,并了解两个没有任何关系组件之间通信的通点 了解为了解决上述通点诞生的flux架构 了解 ...

  2. js第二次作业——2019.10.16

    第一题:完成省城市的三级联动(包括湖南省),附代码和效果图. 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 </head> ...

  3. 你真的了解Python自动化吗?这篇文章可以让你了解90%

    人们为什么使用Python? 之所以选择Python的主要因素有以下几个方面: 软件质量:在很大程度上,Python更注重可读性.一致性和软件质量,从而与脚本语言世界中的其他工具区别开发.此外,Pyt ...

  4. 【Python】自动化测试的7个步骤

    我们对自动化测试充满了希望,然而,自动化测试却经常带给我们沮丧和失望.虽然,自动化测试可以把我们从困难的环境中解放出来,在实施自动化测试解决问题的同时,又带来同样多的问题.在开展自动化测试的工作中,关 ...

  5. 容器编排系统K8s之Volume的基础使用

    前文我们聊到了k8s上的ingress资源相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14167581.html:今天们来聊一下k8s上volum ...

  6. Json转换值类型字段为空字符串时报错问题

    问题 在写Webservices时,碰到的问题. 定义的类 public class User { public string sID { get; set; } public int? iAge { ...

  7. python初学者-判断一个数是否为素数

    while True: #判断为真 num = int(input('请输入一个数:')) for i in range(2,num):#判断在num之前的数能不能把num整除 if(num%i == ...

  8. 深入理解MySQL系列之redo log、undo log和binlog

    事务的实现 redo log保证事务的持久性,undo log用来帮助事务回滚及MVCC的功能. InnoDB存储引擎体系结构 redo log Write Ahead Log策略 事务提交时,先写重 ...

  9. 编程漫谈(二十):如何自学编程及Java、上手真实开发及转行程序员的建议

    前路漫漫,吾将上下而求索! 最近有时在知乎上逛逛,发现很多人对自学编程及转行程序员有困惑.我是在25岁读研时转程序员,正赶上好时候(中国云计算刚刚起步及移动互联网正红的阶段),同时又走了不少弯路,因此 ...

  10. 拥抱 C/C++ : Android JNI 的使用

    编译工具 CMake 以及 Android 上 JNI 的使用介绍. 编译工具 CMake 在Android Studio 2.2 之后,工具中增加了 CMake 的支持,于是我们有两种选择来编译 c ...