zoukankan      html  css  js  c++  java
  • Java单体应用

    原文地址: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群,及时关注我们的课程更新

  • 相关阅读:
    Make something people want
    POJ 3662 Telephone Lines (二分+Dijkstra)
    POJ 2010 Moo University
    HDU 6627 2017ACM/ICPC亚洲区域赛沈阳站 Rabbits
    HDU 6625 2017ACM/ICPC亚洲区域赛沈阳站 Little Boxes(高精度整数加法)
    Codeforces 877D Olya and Energy Drinks(BFS+剪枝)
    CodeForces 876B Divisiblity of Differences (数学水题)
    POJ 2139 Six Degrees of Cowvin Bacon (Floyd最短路)
    UESTC 1057 秋实大哥与花 (线段树)
    Linux安装Oh-my-zsh
  • 原文地址:https://www.cnblogs.com/liuxiaojun/p/training-monolithic-project-iot-cloud-admin-manager-rebuild-dao.html
Copyright © 2011-2022 走看看