萬能BaseMapper,BaseService,BaseController模板(偷懶專用)

1.目錄結構(後期利用工具也能實現,建議前期自己編寫,儲存用作模板)
在這裏插入圖片描述
2.創建BaseMapper接口

package com.liu.base;

import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author root
 * @create 2020-12-21 15:28
 * 封裝一些mapper 的簡單方法
 */
public interface BaseMapper<T> {
    //=================基礎的增刪改查操作========================
    /**
     * 插入一個實體類
     * @param entity
     * @return
     */
    int insert(T entity);

    /**
     * 根據實體類的主鍵刪除一個實體
     * @param id
     */
    void deleteById(Serializable id);

    /**
     * 通過實體類刪除
     * @param entity
     */
    void deleteByEntity(T entity);

    /**
     * 通過map刪除
     * @param map
     */
    void deleteByMap(Map<String,Object> map);

    /**
     * 更新一個實體類
     * @param entity
     */
    void update(T entity);

    /**
     * 通過實體類的id來修改
     * @param entity
     */
    void updateById(T entity);

    /**
     * 根據參數查詢
     * @param map
     * @return
     */
    List<T> getListByMap(Map<String,Object> map);

    /**
     * 查詢所有
     * @return
     */
    List<T> getAll();

    /**
     * 查詢所有的實體,根據實體的屬性值,屬性值作爲條件
     * @param entity
     * @return
     */
    List<T> getAllByEntity(T entity);

    /**
     * 根據主鍵獲取一個實體
     * @param id
     * @return
     */
    T load(Serializable id);
    /**
     * 根據主鍵獲取一個實體
     * @param id
     * @return
     */
    T getById(Serializable id);
    /**
     * 通過map查詢----不分頁
     * @param params
     * @return
     */
    T getByMap(Map<String,Object> params);

    /**
     * 通過對象查詢----不分頁
     * @param entity
     * @return
     */
    T getByEntity(T entity);

    //=========================分頁查詢中的操作=====================================
    //find*方法爲分頁查詢的方法

    /**
     * 通過map進行分頁查詢
     * @param map
     * @return
     */
    public List<T> findByMap(Map<String,Object> map);

    /**
     * 通過對象查詢分頁
     * @param entity
     * @return
     */
    public List<T> findByEntity(T entity);

    /**
     * 需要自己寫count 的分頁
     * @param map
     * @return
     */
    public List<T> findByCount(Map<String,Object> map);

    /**
     * 批量新增
     * @param list
     */
    public void insertBatch(List<T> list);

    /**
     * 批量修改
     * @param list
     */
    public void updateBatch(List<T> list);

    //=============================封裝純sql語句==================================

    /**
     * 通過sql 查詢一個對象返回map集合
     * @param sql
     * @return
     */
    public Map<String,Object> getBySqlReturnMap(@Param("sql") String sql);

    /**
     * 通過sql查詢一個對象返回實體類
     * @param sql
     * @return
     */
    public T getBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 通過sql查詢到的map集合封裝成List集合返回
     * @param sql
     * @return
     */
    public List<Map<String,Object>> listBySqlReturnMap(@Param("sql") String sql);

    /**
     * 通過sql查詢到的結果封裝爲List集合
     * @param sql
     * @return
     */
    public List<T> listBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 查詢分頁
     * @param sql
     * @return
     */
    public List<T> findBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 通過sql查詢到的結果修改
     * @param sql
     */
    public void updateBysql(@Param("sql") String sql);

    /**
     * 通過sql刪除
     * @param sql
     */
    public void deleteBySql(@Param("sql") String sql);
}

2.編寫BaseService

package com.liu.base;

import com.liu.utils.Pager;
import org.apache.ibatis.annotations.Param;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author root
 * @create 2020-12-21 17:41
 */
public interface BaseService<T> {
    //=================基礎的增刪改查操作========================
    /**
     * 插入一個實體類
     * @param entity
     * @return
     */
    int insert(T entity);

    /**
     * 根據實體類的主鍵刪除一個實體
     * @param id
     */
    void deleteById(Serializable id);

    /**
     * 通過實體類刪除
     * @param entity
     */
    void deleteByEntity(T entity);

    /**
     * 通過map刪除
     * @param map
     */
    void deleteByMap(Map<String,Object> map);

    /**
     * 更新一個實體類
     * @param entity
     */
    void update(T entity);

    /**
     * 通過實體類的id來修改
     * @param entity
     */
    void updateById(T entity);

    /**
     * 根據參數查詢
     * @param map
     * @return
     */
    public List<T> getListByMap(Map<String,Object> map);

    /**
     * 查詢所有
     * @return
     */
    List<T> getAll();

    /**
     * 查詢所有的實體,根據實體的屬性值,屬性值作爲條件
     * @param entity
     * @return
     */
    List<T> getAllByEntity(T entity);

    /**
     * 根據主鍵獲取一個實體
     * @param id
     * @return
     */
    T load(Serializable id);
    /**
     * 根據主鍵獲取一個實體
     * @param id
     * @return
     */
    T getById(Serializable id);
    /**
     * 通過map查詢----不分頁
     * @param params
     * @return
     */
    T getByMap(Map<String,Object> params);

    /**
     * 通過對象查詢----不分頁
     * @param entity
     * @return
     */
    T getByEntity(T entity);

    //=========================分頁查詢中的操作=====================================
    //find*方法爲分頁查詢的方法

    /**
     * 通過map進行分頁查詢
     * @param map
     * @return
     */
    public Pager<T> findByMap(Map<String,Object> map);

    /**
     * 通過對象查詢分頁
     * @param entity
     * @return
     */
    public Pager<T> findByEntity(T entity);

    /**
     * 需要自己寫count 的分頁
     * @param map
     * @return
     */
    public List<T> findByCount(Map<String,Object> map);

    /**
     * 批量新增
     * @param list
     */
    public void insertBatch(List<T> list);

    /**
     * 批量修改
     * @param list
     */
    public void updateBatch(List<T> list);

    //=============================封裝純sql語句==================================

    /**
     * 通過sql 查詢一個對象返回map集合
     * @param sql
     * @return
     */
    public Map<String,Object> getBySqlReturnMap(@Param("sql") String sql);

    /**
     * 通過sql查詢一個對象返回實體類
     * @param sql
     * @return
     */
    public T getBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 通過sql查詢到的map集合封裝成List集合返回
     * @param sql
     * @return
     */
    public List<Map<String,Object>> listBySqlReturnMap(@Param("sql") String sql);

    /**
     * 通過sql查詢到的結果封裝爲List集合
     * @param sql
     * @return
     */
    public List<T> listBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 查詢分頁
     * @param sql
     * @return
     */
    public Pager<T> findBySqlReturnEntity(@Param("sql") String sql);

    /**
     * 通過sql查詢到的結果修改
     * @param sql
     */
    public void updateBysql(@Param("sql") String sql);

    /**
     * 通過sql刪除
     * @param sql
     */
    public void deleteBySql(@Param("sql") String sql);
}

3.編寫BaseServiceImpl

package com.liu.base;

import com.github.pagehelper.PageHelper;
import com.liu.utils.Pager;
import com.liu.utils.SystemContext;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author root
 * @create 2020-12-21 17:43
 * 抽象類  ----通過子類來完成實例化
 */
public abstract class BaseServiceImpl<T> implements BaseService<T>{

    public abstract BaseMapper<T> getBaseMapper();

    public int insert(T entity) {
        return this.getBaseMapper().insert(entity);
    }

    public void deleteById(Serializable id) {
            this.getBaseMapper().deleteById(id);
    }

    public void deleteByEntity(T entity) {
           this.getBaseMapper().deleteByEntity(entity);
    }

    public void deleteByMap(Map<String, Object> map) {
           this.getBaseMapper().deleteByMap(map);
    }

    public void update(T entity) {
          this.getBaseMapper().update(entity);
    }

    public void updateById(T entity) {
         this.getBaseMapper().updateById(entity);
    }

    public List<T> getListByMap(Map<String, Object> map) {
        return this.getBaseMapper().getListByMap(map);
    }

    public List<T> getAll() {
        return this.getBaseMapper().getAll();
    }

    public List<T> getAllByEntity(T entity) {
        return this.getBaseMapper().getAllByEntity(entity);
    }

    public T load(Serializable id) {
        return this.getBaseMapper().load(id);
    }

    public T getById(Serializable id) {
        return this.getBaseMapper().getById(id);
    }

    public T getByMap(Map<String, Object> params) {
        return this.getBaseMapper().getByMap(params);
    }

    public T getByEntity(T entity) {
        return this.getBaseMapper().getByEntity(entity);
    }

    public Pager<T> findByMap(Map<String, Object> map) {
        /**
         * 執行分頁
         */
        Integer pageSize = SystemContext.getPageSize();
        Integer pageOffset = SystemContext.getPageOffset();
        if(pageOffset==null||pageOffset<0) pageOffset = 0;
        if(pageSize==null||pageSize<0) pageSize = 15;
        String order = SystemContext.getOrder();
        String sort = SystemContext.getSort();
        Integer pageNum = null;
        if(pageOffset == 0){
            pageNum = 1;
        }else{
            pageNum = pageOffset/pageSize+1;
        }
        PageHelper.startPage(pageNum, pageSize);
        Pager<T> pages = new Pager<T>(this.getBaseMapper().findByMap(map));
        return pages;
    }

    public Pager<T> findByEntity(T entity) {
        /**
         * 執行分頁
         */
        Integer pageSize = SystemContext.getPageSize();
        Integer pageOffset = SystemContext.getPageOffset();
        if(pageOffset==null||pageOffset<0) pageOffset = 0;
        if(pageSize==null||pageSize<0) pageSize = 15;
        String order = SystemContext.getOrder();
        String sort = SystemContext.getSort();
        Integer pageNum = null;
        if(pageOffset == 0){
            pageNum = 1;
        }else{
            pageNum = pageOffset/pageSize+1;
        }
        PageHelper.startPage(pageNum, pageSize);
        Pager<T> pages = new Pager<T>(this.getBaseMapper().findByEntity(entity));
        return pages;
    }

    public List<T> findByCount(Map<String, Object> map) {
        return this.getBaseMapper().findByCount(map);
    }

    public void insertBatch(List<T> list) {
         this.getBaseMapper().insertBatch(list);
    }

    public void updateBatch(List<T> list) {
         this.getBaseMapper().updateBatch(list);
    }

    public Map<String, Object> getBySqlReturnMap(String sql) {
        return this.getBaseMapper().getBySqlReturnMap(sql);
    }

    public T getBySqlReturnEntity(String sql) {
        return this.getBaseMapper().getBySqlReturnEntity(sql);
    }

    public List<Map<String, Object>> listBySqlReturnMap(String sql) {
        return this.getBaseMapper().listBySqlReturnMap(sql);
    }

    public List<T> listBySqlReturnEntity(String sql) {
        return this.getBaseMapper().listBySqlReturnEntity(sql);
    }

    public Pager<T> findBySqlReturnEntity(String sql) {
        /**
         * 執行分頁
         */
        Integer pageSize = SystemContext.getPageSize();
        Integer pageOffset = SystemContext.getPageOffset();
        if(pageOffset==null||pageOffset<0) pageOffset = 0;
        if(pageSize==null||pageSize<0) pageSize = 15;
        String order = SystemContext.getOrder();
        String sort = SystemContext.getSort();
        Integer pageNum = null;
        if(pageOffset == 0){
            pageNum = 1;
        }else{
            pageNum = pageOffset/pageSize+1;
        }
        PageHelper.startPage(pageNum, pageSize);
        Pager<T> pages = new Pager<T>(this.getBaseMapper().findBySqlReturnEntity(sql));
        return pages;
    }

    public void updateBysql(String sql) {
          this.getBaseMapper().updateBysql(sql);
    }

    public void deleteBySql(String sql) {
          this.getBaseMapper().deleteBySql(sql);
    }
    //判斷空
    public boolean isEmpty(String str) {
        return (null == str) || (str.trim().length() <= 0);
    }

    public boolean isEmpty(Character cha) {
        return (null == cha) || cha.equals(' ');
    }

    public boolean isEmpty(Object obj) {
        return (null == obj);
    }

    public boolean isEmpty(Object[] objs) {
        return (null == objs) || (objs.length <= 0);
    }

    public boolean isEmpty(Collection<?> obj) {
        return (null == obj) || obj.isEmpty();
    }

    public boolean isEmpty(Set<?> set) {
        return (null == set) || set.isEmpty();
    }

    public boolean isEmpty(Serializable obj) {
        return null == obj;
    }

    public boolean isEmpty(Map<?, ?> map) {
        return (null == map) || map.isEmpty();
    }
}

4.編寫BaseController

package com.liu.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author root
 * @create 2020-12-23 12:36
 */
public class BaseController {

    protected final static String DATE_FORMATE = "yyyy-MM-dd";

    // 下面是判斷null的操作

    public boolean isEmpty(String str) {
        return (null == str) || (str.trim().length() <= 0);
    }

    public boolean isEmpty(Character cha) {
        return (null == cha) || cha.equals(' ');
    }

    public boolean isEmpty(Object obj) {
        return (null == obj);
    }

    public boolean isEmpty(Object[] objs) {
        return (null == objs) || (objs.length <= 0);
    }

    public boolean isEmpty(Collection<?> obj) {
        return (null == obj) || obj.isEmpty();
    }

    public boolean isEmpty(Set<?> set) {
        return (null == set) || set.isEmpty();
    }

    public boolean isEmpty(Serializable obj) {
        return null == obj;
    }

    public boolean isEmpty(Map<?, ?> map) {
        return (null == map) || map.isEmpty();
    }

    /**
     *
     * 獲得map
     * @return
     */
    public Map<String,Object> getMap(){
        return new HashMap<String,Object>();
    }
}

5.編寫****Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liu.mapper.SuperUserMapper">
         <!--    實體類跟數據庫映射部分-->
    <resultMap id="ResultMapSuperUser" type="com.liu.pojo.SuperUser">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="realname" column="realname" jdbcType="VARCHAR"/>
    </resultMap>
<!--    聲明數據庫的字段-->
    <sql id="superuser_field">
        id,username,password,realname
    </sql>
<!--    聲明實體類的屬性-->
    <sql id="SuperUser_insert">
        #{id},#{username},#{password},#{realname}
    </sql>
<!--    查詢時的條件-->
    <sql id="SuperUser_where">
        <if test="id!=null">
             id=#{id}
        </if>
        <if test="username!=null">
            and username=#{username}
        </if>
        <if test="password!=null">
            and password=#{password}
        </if>
        <if test="realname!=null">
            and realname=#{realname}
        </if>
    </sql>
    <!--    更新時的條件-->
    <sql id="SuperUser_update">
        <if test="username!=null">
             username=#{username},
        </if>
        <if test="password!=null">
             password=#{password},
        </if>
        <if test="realname!=null">
             realname=#{realname}
        </if>
    </sql>
<!--    新增方法-->
    <insert id="insert" parameterType="SuperUser" useGeneratedKeys="true" keyProperty="id">
           insert into pmxt.superuser(<include refid="superuser_field"></include>)
           values (<include refid="SuperUser_insert"></include>)
    </insert>
<!--    根據id刪除一個實體類-->
    <delete id="deleteById" parameterType="Integer">
            delete from pmxt.superuser where id=#{id}
    </delete>
<!--    通過實體刪除-->
    <delete id="deleteByEntity" parameterType="SuperUser">
        delete from pmxt.superuser where 1=1
       <include refid="SuperUser_where"></include>
    </delete>
<!--通過Map刪除-->
    <delete id="deleteByMap" parameterType="java.util.HashMap">
        delete from pmxt.superuser where 1=1
        <include refid="SuperUser_where"></include>
    </delete>
<!--    更新一個實體類-->
    <update id="update" parameterType="SuperUser">
        update pmxt.superuser
        <set>
            <include refid="SuperUser_update"></include>
        </set>
        where 1=1
        <include refid="SuperUser_where"></include>
    </update>
<!--    通過id進行修改-->
    <update id="updateById" parameterType="SuperUser">
        update pmxt.superuser
        <set>
            <include refid="SuperUser_update"></include>
        </set>
        where id = #{id}
    </update>
<!--    根據參數查詢-->
    <select id="getListByMap" resultMap="ResultMapSuperUser" parameterType="java.util.HashMap">
         select <include refid="superuser_field"></include>
         from pmxt.superuser
         where 1=1
         <include refid="SuperUser_where"></include>
    </select>
<!--    查詢所有實體-->
    <select id="getAll" resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
    </select>
<!--    查詢所有的實體,根據實體的屬性值,屬性值作爲條件-->
    <select id="getAllByEntity" parameterType="SuperUser" resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
        where 1=1
        <include refid="SuperUser_where"></include>
    </select>
    <!--    根據主鍵查詢-->
    <select id="load" parameterType="Integer" resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
        where id=#{id}
    </select>
<!--    根據主鍵查詢-->
    <select id="getById" parameterType="Integer" resultMap="ResultMapSuperUser">
         select <include refid="superuser_field"></include>
         from pmxt.superuser
         where id=#{id}
    </select>
<!--    通過map查詢-->
    <select id="getByMap" parameterType="java.util.HashMap" resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
        where 1=1
        <include refid="SuperUser_where"></include>
    </select>
<!--    通過對象查詢-->
    <select id="getByEntity" parameterType="SuperUser"  resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
        where 1=1
        <include refid="SuperUser_where"></include>
    </select>
<!--    通過map查詢分頁-->
<select id="findByMap" parameterType="java.util.HashMap" resultMap="ResultMapSuperUser">
    select <include refid="superuser_field"></include>
    from pmxt.superuser
    where 1=1
    <include refid="SuperUser_where"></include>
</select>
<!-- 通過對象查詢分頁-->
    <select id="findByEntity" parameterType="SuperUser" resultMap="ResultMapSuperUser">
        select <include refid="superuser_field"></include>
        from pmxt.superuser
        where 1=1
        <include refid="SuperUser_where"></include>
    </select>
<!--批量新增-->
    <insert id="insertBatch" parameterType="java.util.List">
        insert into pmxt.superuser(<include refid="superuser_field"></include>)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.username},#{item.password},#{item.realname})
        </foreach>
    </insert>
<!--批量修改-->
    <update id="updateBatch" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            update pmxt.superuser
            <set>
                <if test="item.username!=null">
                    username=#{item.username},
                </if>
                <if test="item.password!=null">
                    password=#{item.password},
                </if>
                <if test="item.realname!=null">
                    realname=#{item.realname}
                </if>
            </set>
            where 1=1
            <if test="item.id!=null">
                and id=#{item.id}
            </if>
        </foreach>
    </update>
<!--    封裝純sql語法-->
<!--    查詢一個對象返回map集合-->
    <select id="getBySqlReturnMap" resultType="java.util.HashMap">
        ${sql}
    </select>
<!--    通過sql查詢一個對象返回實體類-->
    <select id="getBySqlReturnEntity" resultMap="ResultMapSuperUser">
        ${sql}
    </select>
<!--    通過sql查詢到的map集合封裝成List集合返回-->
    <select id="listBySqlReturnMap" resultType="java.util.HashMap">
        ${sql}
    </select>
<!--    通過sql查詢到的結果封裝爲List集合-->
    <select id="listBySqlReturnEntity" resultMap="ResultMapSuperUser">
        ${sql}
    </select>
<!--查詢分頁-->
    <select id="findBySqlReturnEntity" resultMap="ResultMapSuperUser">
        ${sql}
    </select>
<!--    通過sql查詢到的結果修改-->
    <update id="updateBysql">
        ${sql}
    </update>
<!--    通過sql查詢-->
    <delete id="deleteBySql">
         ${sql}
    </delete>
</mapper>

6.修改部分代碼,可以做到通用(以及批量處理那裏修改以下)

<mapper namespace="com.liu.mapper.SuperUserMapper">
         <!--    實體類跟數據庫映射部分-->
    <resultMap id="ResultMapSuperUser" type="com.liu.pojo.SuperUser">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="realname" column="realname" jdbcType="VARCHAR"/>
    </resultMap>
<!--    聲明數據庫的字段-->
    <sql id="superuser_field">
        id,username,password,realname
    </sql>
<!--    聲明實體類的屬性-->
    <sql id="SuperUser_insert">
        #{id},#{username},#{password},#{realname}
    </sql>
<!--    查詢時的條件-->
    <sql id="SuperUser_where">
        <if test="id!=null">
             id=#{id}
        </if>
        <if test="username!=null">
            and username=#{username}
        </if>
        <if test="password!=null">
            and password=#{password}
        </if>
        <if test="realname!=null">
            and realname=#{realname}
        </if>
    </sql>
    <!--    更新時的條件-->
    <sql id="SuperUser_update">
        <if test="username!=null">
             username=#{username},
        </if>
        <if test="password!=null">
             password=#{password},
        </if>
        <if test="realname!=null">
             realname=#{realname}
        </if>
    </sql>

7.正是開始三層架構
7.1編寫dao層

package com.liu.mapper;

import com.liu.base.BaseMapper;
import com.liu.pojo.SuperUser;

/**
 * @author root
 * @create 2020-12-21 16:08
 */
public interface SuperUserMapper extends BaseMapper<SuperUser> {
   
   
}

7.2編寫Service層

package com.liu.service;

import com.liu.base.BaseService;
import com.liu.pojo.SuperUser;

/**
 * @author root
 * @create 2020-12-21 17:42
 */
public interface SuperUserService extends BaseService<SuperUser>{
   
   
}
package com.liu.service.impl;

import com.liu.base.BaseMapper;
import com.liu.base.BaseServiceImpl;
import com.liu.mapper.SuperUserMapper;
import com.liu.pojo.SuperUser;
import com.liu.service.SuperUserService;
import com.liu.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author root
 * @create 2020-12-21 17:43
 */
@Service
public class SuperUserServiceImpl extends BaseServiceImpl<SuperUser> implements SuperUserService{
   
   
    @Autowired
    SuperUserMapper superUserMapper;
    public BaseMapper<SuperUser> getBaseMapper() {
   
   
        return superUserMapper;
    }

}

7.3Controller層

package com.liu.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.base.BaseController;
import com.liu.pojo.User;
import com.liu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author root
 * @create 2020-12-22 19:14
 */
@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {
   
   
   @Autowired
   private UserService userService;

   /**
    * 查詢所有用戶,根據條件查詢用戶
    * @return
    */
   @GetMapping("/getUsers")
   public String getUsers(String tj,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, Model model){
   
   
      String sql = "select * from user where 1=1 ";
      //拼接sql  條件拼接
      if(!isEmpty(tj)){
   
   
         sql += " and (username like '%"+tj+"%' or phone like '%"+tj+"%' or email like '%"+tj+"%')";
      }
      sql += " order by id";
      List<User> userList = userService.listBySqlReturnEntity(sql);
      PageHelper.startPage(pageNum,8);
      PageInfo<User> pageInfo = new PageInfo<User>(userList);
      model.addAttribute("pageInfo",pageInfo);
      return "admin/user_list";
   }

   /**
    * 刪除用戶
    * @return
    */
   @GetMapping("/delete/{id}")
   public String deleteUserById(@PathVariable("id") Integer id){
   
   
      userService.deleteById(id);
      return "redirect:/admin/user/getUsers";
   }

   /**
    * 去添加頁面
    * @return
    */
   @RequestMapping("/toAdd")
   public String toAdd(Model model){
   
   
      User user = new User();
      model.addAttribute("user",user);
      return "admin/user_add";
   }
   @PostMapping("/add")
   public String addUser(@Valid User user){
   
   
      int i = userService.insert(user);
      return "redirect:/admin/user/getUsers";
   }

   /**
    * 去修改頁面
    * @return
    */
   @RequestMapping("/toUpdate/{id}")
   public String toUpdate(Model model,@PathVariable("id") Integer id){
   
   
      User user = userService.getById(id);
      model.addAttribute("user",user);
      return "admin/user_update";
   }
   @PostMapping("/update")
   public String updateUser(@Valid User user){
   
   
        userService.updateById(user);
        return "redirect:/admin/user/getUsers";
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章