Java单体应用 - 项目实战(后台) - 03.后台账户管理 - 14.重构 Dao 层

原文地址:http://www.work100.net/training/monolithic-project-iot-cloud-admin-manager-rebuild-dao.html
更多教程:光束云 - 免费课程

重构 Dao 层

序号 文内章节 视频
1 概述 -
2 定义BaseDao通用接口 -
3 重构AuthManagerDao接口 -
4 修改AuthManagerMapper.xml映射文件 -
5 实例源码 -

请参照如上章节导航进行阅读

1.概述

前面的课程已经把 后台账户 功能完成,后续的 租户管理租户账户管理前台的相关功能也都将以 为基础进行展开完成。

为了提高代码的重用率,我们将 Dao 层进行重构,将一些通用功能进行提取。

2.定义BaseDao通用接口

iot-cloud-commons 项目下新增 BaseDao 接口,代码如下:

package net.work100.training.stage2.iot.cloud.commons.dao;

import net.work100.training.stage2.iot.cloud.commons.dto.AbstractBaseDomain;

import java.util.List;
import java.util.Map;

/**
 * <p>Title: BaseDao</p>
 * <p>Description: 通用 DAO 接口</p>
 *
 * @author liuxiaojun
 * @date 2020-03-18 09:11
 * ------------------- History -------------------
 * <date>      <author>       <desc>
 * 2020-03-18   liuxiaojun     初始创建
 * -----------------------------------------------
 */
public interface BaseDao<T extends AbstractBaseDomain> {

    /**
     * 查询全部表记录
     *
     * @return
     */
    List<T> selectAll();


    /**
     * 新增
     *
     * @param entity
     */
    void insert(T entity);

    /**
     * 删除
     *
     * @param entityKey
     */
    void delete(String entityKey);

    /**
     * 批量删除
     *
     * @param entityKeys
     */
    void multiDelete(String[] entityKeys);

    /**
     * 获取单个对象
     *
     * @param id
     * @return
     */
    T getById(Long id);

    /**
     * 获取单个对象
     *
     * @param entityKey 实体对象Key
     * @return
     */
    T getByKey(String entityKey);


    /**
     * 更新
     *
     * @param entity
     */
    void update(T entity);

    /**
     * 搜索
     *
     * @param entity 查询条件
     * @return
     */
    List<T> search(T entity);

    /**
     * 分页查询
     *
     * @param params 查询条件及分页参数
     * @return
     */
    List<T> pageSearch(Map<String, Object> params);

    /**
     * 计数统计
     *
     * @param entity 查询条件
     * @return
     */
    int count(T entity);
}

3.重构AuthManagerDao接口

接下来重构 AuthManagerDao 层代码:

package net.work100.training.stage2.iot.cloud.web.admin.dao;

import net.work100.training.stage2.iot.cloud.commons.dao.BaseDao;
import net.work100.training.stage2.iot.cloud.domain.AuthManager;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>Title: AuthManagerDao</p>
 * <p>Description: </p>
 * <p>Url: http://www.work100.net/training/monolithic-project-iot-cloud-admin.html</p>
 *
 * @author liuxiaojun
 * @date 2020-02-23 22:54
 * ------------------- History -------------------
 * <date>      <author>       <desc>
 * 2020-02-23   liuxiaojun     初始创建
 * -----------------------------------------------
 */
@Repository
public interface AuthManagerDao extends BaseDao<AuthManager> {

    /**
     * 获取账户对象
     *
     * @param userName 用户名
     * @return
     */
    AuthManager getByUserName(String userName);
}

4.修改AuthManagerMapper.xml映射文件

接下来修改 AuthManagerMapper.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 namespace="net.work100.training.stage2.iot.cloud.web.admin.dao.AuthManagerDao">

    <sql id="authManagerColumns">
          a.id,
          a.user_key,
          a.user_name,
          a.password,
          a.status,
          a.superuser,
          a.roles,
          a.modify_password_time,
          a.created,
          a.updated
    </sql>

    <select id="selectAll" resultType="AuthManager">
        SELECT
          <include refid="authManagerColumns" />
        FROM
          auth_manager AS a
        ORDER BY a.id DESC
    </select>

    <insert id="insert" >
        INSERT INTO auth_manager (
            `user_key`,
            `user_name`,
            `password`,
            `status`,
            `superuser`,
            `roles`,
            `created`,
            `updated`
        )
        VALUES (
            #{userKey},
            #{userName},
            #{password},
            #{status},
            #{superuser},
            #{roles},
            #{created},
            #{updated}
        )
    </insert>

    <update id="update">
        UPDATE
          auth_manager
        SET
          status = #{status},
          superuser = #{superuser},
          roles = #{roles},
          updated = #{updated}
        WHERE
          user_key = #{userKey}
    </update>

    <delete id="delete">
        DELETE FROM auth_manager WHERE user_key = #{userKey}
    </delete>

    <delete id="multiDelete">
        DELETE FROM auth_manager
        <if test="array != null and array.length > 0">
            WHERE user_key IN
            <foreach collection="array" open="(" close=")" item="userKey" separator=",">
                #{userKey}
            </foreach>
        </if>
        <if test="array == null or array.length == 0">
            WHERE 1 = 2
        </if>
    </delete>

    <select id="getById" resultType="AuthManager">
        SELECT
          <include refid="authManagerColumns" />
        FROM
          auth_manager AS a
        WHERE
          a.id = #{id}
    </select>

    <select id="getByKey" resultType="AuthManager">
        SELECT
        <include refid="authManagerColumns" />
        FROM
        auth_manager AS a
        WHERE
        a.user_key = #{userKey}
    </select>

    <select id="getByUserName" resultType="AuthManager">
        SELECT
          <include refid="authManagerColumns" />
        FROM
          auth_manager AS a
        WHERE
          a.user_name = #{userName}
    </select>

    <select id="search" resultType="AuthManager">
        SELECT
          <include refid="authManagerColumns" />
        FROM
          auth_manager AS a
        <where>
            <if test="userName != null and userName != ''">
                AND a.user_name LIKE CONCAT('%', #{userName}, '%')
            </if>
            <if test="roles != null and roles != ''">
                AND a.roles LIKE CONCAT('%', #{roles}, '%')
            </if>
            <if test="status != -1">
                AND a.status = #{status}
            </if>
        </where>
        ORDER BY a.id DESC
    </select>

    <select id="pageSearch" resultType="AuthManager" parameterType="java.util.Map">
        SELECT
          <include refid="authManagerColumns" />
        FROM
          auth_manager AS a
        <where>
            <if test="userName != null and userName != ''">
                AND a.user_name LIKE CONCAT('%', #{userName}, '%')
            </if>
            <if test="roles != null and roles != ''">
                AND a.roles LIKE CONCAT('%', #{roles}, '%')
            </if>
            <if test="status != -1">
                AND a.status = #{status}
            </if>
        </where>
        ORDER BY a.id DESC
        LIMIT #{start}, #{length}
    </select>

    <select id="count" resultType="int">
        SELECT
          COUNT(*)
        FROM
          auth_manager AS a
        <where>
            <if test="userName != null and userName != ''">
                AND a.user_name LIKE CONCAT('%', #{userName}, '%')
            </if>
            <if test="roles != null and roles != ''">
                AND a.roles LIKE CONCAT('%', #{roles}, '%')
            </if>
            <if test="status != -1">
                AND a.status = #{status}
            </if>
        </where>
    </select>
</mapper>

5.实例源码

实例源码已经托管到如下地址:


上一篇:Spring Validation

下一篇:重构Service层


如果对课程内容感兴趣,可以扫码关注我们的 公众号QQ群,及时关注我们的课程更新

posted @ 2020-04-01 15:32  光束云  阅读(129)  评论(0编辑  收藏  举报