微人事项目-mybatis-持久层

摘要

最近将微人事这个开源项目进行了复现,这篇文章记录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文件进行配置实现对数据库的增删查改。
如有错误,欢迎指正

posted @ 2020-05-04 20:42  CodeJames  阅读(319)  评论(0编辑  收藏  举报