微人事-持久層

摘要

最近將微人事這個開源項目進行了復現,這篇文章記錄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文件進行配置實現對數據庫的增刪查改。
如有錯誤,歡迎指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章