基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【四】【编写基础开发工具】

大家在经过一段时间的开发实际上已经积累的自己的一套的开发框架或者一些自己的jar包,此处所编写的开发工具是基于当前工程的进行扩展的一个开发工具包,仅适合当前框架,若大家需要的话可以自己在当前开发工具包底下进行扩展,有了开发工具包的好处就是我们可以少写很多基础的代码,那么接下来将开始编写我们的开发工具包:

      首先在我们的common包底下先建以下的包结构:

在我们创建我们的开发工具包之前我们需要先添加几个配置文件在我们的config->util包底下我们创建一个json包同时增加一个JsonHelper.java文件如下所示:

 

 
  1. /*

  2. * 类描述:json工具类

  3. * @auther linzf

  4. * @create 2017/8/23 0023

  5. */

  6. public class JsonHelper {

  7.  
  8. private static SimpleDateFormat sdf = new SimpleDateFormat(

  9. "yyyy-MM-dd HH:mm:ss");

  10.  
  11. // 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key

  12. public static Map json2Map(Object[] keyArray, String json, Class valueClz) {

  13. JSONObject jsonObject = JSONObject.fromObject(json);

  14. Map classMap = new HashMap();

  15. for (int i = 0; i < keyArray.length; i++) {

  16. classMap.put(keyArray[i], valueClz);

  17. }

  18. return (Map) JSONObject.toBean(jsonObject, Map.class, classMap);

  19. }

  20.  
  21. /***

  22. * 将对象转换为传入类型的List

  23. *

  24. * @param object

  25. * @param objectClass

  26. * @return

  27. */

  28. public static <T> Collection<T> toList(Object object, Class<T> objectClass) {

  29. JSONArray jsonArray = JSONArray.fromObject(object);

  30. return JSONArray.toCollection(jsonArray, objectClass);

  31. }

  32.  
  33. /**

  34. * 功能描述:实现将一个object对象转换为json的string字符串

  35. * @param obj

  36. * @return

  37. */

  38. public static String object2json(Object obj) {

  39. StringBuilder json = new StringBuilder();

  40. if (obj == null) {

  41. json.append("\"\"");

  42. } else if (obj instanceof String || obj instanceof BigDecimal

  43. || obj instanceof BigInteger) {

  44. json.append("\"").append(string2json(obj.toString())).append("\"");

  45. } else if (obj instanceof Boolean) {

  46. json.append(Boolean.valueOf(obj.toString()));

  47. } else if (obj instanceof Integer || obj instanceof Float

  48. || obj instanceof Short || obj instanceof Double

  49. || obj instanceof Long || obj instanceof Byte) {

  50. json.append(obj.toString());

  51. } else if (obj instanceof Date || obj instanceof java.sql.Date) {

  52. json.append("\"").append(sdf.format(obj)).append("\"");

  53. } else if (obj instanceof Object[]) {

  54. json.append(array2json((Object[]) obj));

  55. } else if (obj instanceof List) {

  56. json.append(list2json((List<?>) obj));

  57. } else if (obj instanceof Map) {

  58. json.append(map2json((Map<?, ?>) obj));

  59. } else if (obj instanceof Set) {

  60. json.append(set2json((Set<?>) obj));

  61. } else {

  62. json.append(bean2json(obj));

  63. }

  64. return json.toString();

  65. }

  66.  
  67. public static String list2json(List<?> list) {

  68. StringBuilder json = new StringBuilder();

  69. json.append("[");

  70. if (list != null && list.size() > 0) {

  71. for (Object obj : list) {

  72. json.append(object2json(obj));

  73. json.append(",");

  74. }

  75. json.setCharAt(json.length() - 1, ']');

  76. } else {

  77. json.append("]");

  78. }

  79. return json.toString();

  80. }

  81.  
  82. public static String array2json(Object[] array) {

  83. StringBuilder json = new StringBuilder();

  84. json.append("[");

  85. if (array != null && array.length > 0) {

  86. for (Object obj : array) {

  87. json.append(object2json(obj));

  88. json.append(",");

  89. }

  90. json.setCharAt(json.length() - 1, ']');

  91. } else {

  92. json.append("]");

  93. }

  94. return json.toString();

  95. }

  96.  
  97. public static String map2json(Map<?, ?> map) {

  98. StringBuilder json = new StringBuilder();

  99. json.append("{");

  100. if (map != null && map.size() > 0) {

  101. for (Object key : map.keySet()) {

  102. json.append(object2json(key));

  103. json.append(":");

  104. json.append(object2json(map.get(key)));

  105. json.append(",");

  106. }

  107. json.setCharAt(json.length() - 1, '}');

  108. } else {

  109. json.append("}");

  110. }

  111. return json.toString();

  112. }

  113.  
  114. public static String set2json(Set<?> set) {

  115. StringBuilder json = new StringBuilder();

  116. json.append("[");

  117. if (set != null && set.size() > 0) {

  118. for (Object obj : set) {

  119. json.append(object2json(obj));

  120. json.append(",");

  121. }

  122. json.setCharAt(json.length() - 1, ']');

  123. } else {

  124. json.append("]");

  125. }

  126. return json.toString();

  127. }

  128.  
  129. public static String string2json(String s) {

  130. if (s == null)

  131. return "";

  132. StringBuilder sb = new StringBuilder();

  133. for (int i = 0; i < s.length(); i++) {

  134. char ch = s.charAt(i);

  135. switch (ch) {

  136. case '"':

  137. sb.append("\\\"");

  138. break;

  139. case '\\':

  140. sb.append("\\\\");

  141. break;

  142. case '\b':

  143. sb.append("\\b");

  144. break;

  145. case '\f':

  146. sb.append("\\f");

  147. break;

  148. case '\n':

  149. sb.append("\\n");

  150. break;

  151. case '\r':

  152. sb.append("\\r");

  153. break;

  154. case '\t':

  155. sb.append("\\t");

  156. break;

  157. case '/':

  158. sb.append("\\/");

  159. break;

  160. default:

  161. if (ch >= '\u0000' && ch <= '\u001F') {

  162. String ss = Integer.toHexString(ch);

  163. sb.append("\\u");

  164. for (int k = 0; k < 4 - ss.length(); k++) {

  165. sb.append('0');

  166. }

  167. sb.append(ss.toUpperCase());

  168. } else {

  169. sb.append(ch);

  170. }

  171. }

  172. }

  173. return sb.toString();

  174. }

  175.  
  176. public static String bean2json(Object bean) {

  177. StringBuilder json = new StringBuilder();

  178. json.append("{");

  179. PropertyDescriptor[] props = null;

  180. try {

  181. props = Introspector.getBeanInfo(bean.getClass(), Object.class)

  182. .getPropertyDescriptors();

  183. } catch (IntrospectionException e) {

  184. }

  185. if (props != null) {

  186. for (int i = 0; i < props.length; i++) {

  187. try {

  188. String name = object2json(props[i].getName());

  189. String value = object2json(props[i].getReadMethod().invoke(

  190. bean));

  191. json.append(name);

  192. json.append(":");

  193. json.append(value);

  194. json.append(",");

  195. } catch (Throwable e) {

  196. }

  197. }

  198. json.setCharAt(json.length() - 1, '}');

  199. } else {

  200. json.append("}");

  201. }

  202. return json.toString();

  203. }

  204.  
  205.  
  206. }


 

 

接着我们在dao,entity,service包底下分别加入以下的类文件:

 

 
  1. /**

  2. * 分页实体类

  3. * */

  4. @SuppressWarnings("rawtypes")

  5. public class Page {

  6. private List rows;

  7. private long total;

  8.  
  9. public Page(){}

  10.  
  11. public Page(List rows, long total) {

  12. super();

  13. this.rows = rows;

  14. this.total = total;

  15. }

  16.  
  17. public List getRows() {

  18. return rows;

  19. }

  20. public void setRows(List rows) {

  21. this.rows = rows;

  22. }

  23. public long getTotal() {

  24. return total;

  25. }

  26. public void setTotal(long total) {

  27. this.total = total;

  28. }

  29.  
  30. @Override

  31. public String toString() {

  32. return "Page [rows=" + rows + ", total=" + total + "]";

  33. }

  34. }

 

 
  1. /*

  2. * 类描述:查询基础类

  3. * @auther linzf

  4. * @create 2017/8/11 0011

  5. */

  6. public class QueryBase {

  7.  
  8. /** 要排序的字段名 */

  9. protected String sort;

  10. /** 排序方式: desc \ asc */

  11. protected String order = "";

  12. /** 获取一页行数 */

  13. protected int limit;

  14. /** 获取的页码 */

  15. protected int page;

  16. /** 起始记录 */

  17. protected int offset;

  18.  
  19. public String getSort() {

  20. return sort;

  21. }

  22.  
  23. public void setSort(String sort) {

  24. this.sort = sort;

  25. }

  26.  
  27. public String getOrder() {

  28. return order;

  29. }

  30.  
  31. public void setOrder(String order) {

  32. this.order = order;

  33. }

  34.  
  35. public int getLimit() {

  36. return limit;

  37. }

  38.  
  39. public void setLimit(int limit) {

  40. this.limit = limit;

  41. }

  42.  
  43. public int getPage() {

  44. return page;

  45. }

  46.  
  47. public void setPage(int page) {

  48. this.page = page;

  49. }

  50.  
  51. public int getOffset() {

  52. return (this.page-1)*limit;

  53. }

  54.  
  55. public void setOffset(int offset) {

  56. this.offset = offset;

  57. }

  58. }

 

 
  1. /**

  2. * Dao通用模版

  3. * */

  4. public interface GenericDao<T, Q extends QueryBase>{

  5. /**

  6. * 根据主键值获取对象

  7. * @param entity

  8. * */

  9. public T get(T entity);

  10.  
  11. /**

  12. * 获取全部实体

  13. * */

  14. public List<T> loadAll();

  15.  
  16. /**

  17. * 查找是否存在

  18. * @param queryModel 查询条件

  19. * @return int

  20. * */

  21. public int isExist(Q queryModel);

  22.  
  23. /**

  24. * 保存

  25. * @param entity 保存对象

  26. * @return int

  27. * @throws Exception

  28. * */

  29. public int save(T entity) throws Exception;

  30.  
  31. /**

  32. * 更新

  33. * @param entity 修改对象

  34. * @return int

  35. * @throws Exception

  36. * */

  37. public int update(T entity) throws Exception;

  38.  
  39. /**

  40. * 删除

  41. * @param entity 删除对象

  42. * @throws Exception

  43. * @return int

  44. * */

  45. public int delete(T entity) throws Exception;

  46.  
  47. /**

  48. * 分页查询

  49. * @param queryModel 查询条件

  50. * */

  51. public List<T> findByPage(Q queryModel);

  52.  
  53. /**

  54. * 统计

  55. * @param queryModel 查询条件

  56. * @return int

  57. * */

  58. public int count(Q queryModel);

  59.  
  60. /**

  61. * 查询

  62. * @param queryModel 查询条件

  63. * */

  64. public List<T> query(Q queryModel);

  65. /**

  66. * 根据id数组删除记录

  67. * @param ids 数组

  68. * @return int

  69. * */

  70. public int deleteByIds(String[] ids) throws Exception;

  71. }

 

 
  1. /**

  2. * 通用Service

  3. * @author linzf

  4. * */

  5. public abstract class GenericService<T, Q extends QueryBase> {

  6. protected abstract GenericDao<T, Q> getDao();

  7.  
  8. /**

  9. * 根据主键值获取对象

  10. * @param entity

  11. * */

  12. public T get(T entity){

  13. return getDao().get(entity);

  14. }

  15.  
  16. /**

  17. * 获取全部实体

  18. * */

  19. public List<T> loadAll(){

  20. return getDao().loadAll();

  21. }

  22.  
  23. /**

  24. * 查找是否存在

  25. * @param queryModel 查询条件

  26. * */

  27. public boolean isExist(Q queryModel){

  28. return getDao().isExist(queryModel)>0;

  29. }

  30.  
  31. /**

  32. * 保存

  33. * @param entity 保存对象

  34. * @return boolean

  35. * @throws Exception

  36. * */

  37. public boolean save(T entity) throws Exception{

  38. return getDao().save(entity)>0;

  39. }

  40.  
  41. /**

  42. * 更新

  43. * @param entity 修改对象

  44. * @return boolean

  45. * @throws Exception

  46. * */

  47. public boolean update(T entity) throws Exception{

  48. return getDao().update(entity)>0;

  49. }

  50.  
  51. /**

  52. * 删除

  53. * @param entity 删除对象

  54. * @return boolean

  55. * @throws Exception

  56. * */

  57. public boolean delete(T entity) throws Exception{

  58. return getDao().delete(entity)>0;

  59. }

  60.  
  61. /**

  62. * 分页查询

  63. * @param queryModel 查询条件

  64. * */

  65. public Page findByPage(Q queryModel){

  66. List<T> list = getDao().findByPage(queryModel);

  67. int count = getDao().count(queryModel);

  68. return new Page(list, count);

  69. }

  70.  
  71. /**

  72. * 统计

  73. * @param queryModel 查询条件

  74. * @return int

  75. * */

  76. public int count(Q queryModel){

  77. return getDao().count(queryModel);

  78. }

  79.  
  80. /**

  81. * 查询

  82. * @param queryModel 查询条件

  83. * */

  84. public List<T> query(Q queryModel){

  85. return getDao().query(queryModel);

  86. }

  87. /**

  88. * 根据id数组删除记录

  89. * @param ids 数组

  90. * @return boolean

  91. * */

  92. public boolean deleteByIds(String[] ids) throws Exception{

  93. return getDao().deleteByIds(ids)>0;

  94. }

  95.  
  96. /**

  97. * 功能描述:批量删除数据

  98. * @param entityList

  99. * @return

  100. */

  101. public boolean removeBath(List<T> entityList) throws Exception{

  102. for(T t:entityList){

  103. if(!delete(t)){

  104. return false;

  105. }

  106. }

  107. return true;

  108. }

  109. }

 

 

 
  1. public abstract class GenericController<T, Q extends QueryBase> {

  2.  
  3. // 抽象方法

  4. protected abstract GenericService<T, Q> getService();

  5.  
  6. /**添加页面路径*/

  7. public final static String ADDPAGE = "/add";

  8. /**修改页面路径*/

  9. public final static String UPDATEPAGE = "/update";

  10.  
  11. /**

  12. * Controller基路径

  13. * */

  14. protected String basePath;

  15.  
  16. /**抽象方法,获取页面基路径

  17. * @throws Exception */

  18. protected String getPageBaseRoot() throws Exception{

  19. if(basePath==null){

  20. basePath = this.getClass().getName();

  21. Pattern p=Pattern.compile(".[a-z|A-z]+.controller.[a-z|A-z]+Controller");

  22. Matcher m=p.matcher(basePath);

  23. if(m.find()){

  24. basePath = m.group();

  25. basePath = basePath.substring(1, basePath.length()-10);

  26. basePath = basePath.replace(".", "/");

  27. basePath = basePath.replace("/controller/", "/");

  28. basePath = basePath.substring(0,basePath.lastIndexOf("/")+1)+ toFirstCharLowerCase(basePath.substring(basePath.lastIndexOf("/")+1));

  29. }

  30. else{

  31. throw new Exception("获取页面基路径失败");

  32. }

  33. }

  34. return basePath;

  35. }

  36.  
  37.  
  38. /**

  39. * 功能描述:直接跳转到更新数据的页面

  40. * @param entity

  41. * @return

  42. */

  43. @RequestMapping(value = "/updatePage",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)

  44. public String updatePage(T entity,Model model) throws Exception{

  45. entity = getService().get(entity);

  46. model.addAttribute("entity",entity);

  47. return getPageBaseRoot()+UPDATEPAGE;

  48. }

  49.  
  50. /** 跳转到添加对象页面

  51. * @throws Exception */

  52. @RequestMapping(value="/addPage")

  53. public String addPage() throws Exception{

  54. return getPageBaseRoot()+ADDPAGE;

  55. }

  56.  
  57. /**

  58. * 功能描述:保存数据字典数据

  59. * @param entity

  60. * @return

  61. */

  62. @RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  63. @ResponseBody

  64. public Map<String,Object> save(T entity) throws Exception{

  65. boolean success = getService().save(entity);

  66. Map<String,Object> result = new HashMap<String, Object>();

  67. if(success==true){

  68. result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);

  69. result.put(SystemStaticConst.MSG,"增加数据成功!");

  70. result.put("entity",entity);

  71. }else{

  72. result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);

  73. result.put(SystemStaticConst.MSG,"增加数据失败!");

  74. }

  75. return result;

  76. }

  77.  
  78. /**

  79. * 功能描述:更新数据字典数据

  80. * @param entity

  81. * @return

  82. */

  83. @RequestMapping(value = "/update",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  84. @ResponseBody

  85. public Map<String,Object> update(T entity) throws Exception{

  86. boolean success = getService().update(entity);

  87. Map<String,Object> result = new HashMap<String, Object>();

  88. if(success==true){

  89. result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);

  90. result.put(SystemStaticConst.MSG,"更新数据成功!");

  91. result.put("entity",entity);

  92. }else{

  93. result.put(SystemStaticConst.RESULT,SystemStaticConst.FAIL);

  94. result.put(SystemStaticConst.MSG,"更新数据失败!");

  95. }

  96. return result;

  97. }

  98.  
  99. /**

  100. * 功能描述:实现批量删除数据字典的记录

  101. * @param entity

  102. * @return

  103. */

  104. @RequestMapping(value = "/remove",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  105. @ResponseBody

  106. public Map<String,Object> remove(T entity) throws Exception{

  107. Map<String,Object> result = new HashMap<String, Object>();

  108. getService().delete(entity);

  109. result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);

  110. result.put(SystemStaticConst.MSG,"删除数据成功!");

  111. return result;

  112. }

  113.  
  114.  
  115. /**

  116. * 功能描述:实现批量删除数据字典的记录

  117. * @param json

  118. * @return

  119. */

  120. @RequestMapping(value = "/removeBath",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  121. @ResponseBody

  122. public Map<String,Object> removeBath(String json) throws Exception{

  123. Map<String,Object> result = new HashMap<String, Object>();

  124. getService().removeBath((List<T>) JsonHelper.toList(json,(Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]));

  125. result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);

  126. result.put(SystemStaticConst.MSG,"删除数据成功!");

  127. return result;

  128. }

  129.  
  130. /**

  131. * 功能描述:获取数据字典的分页的数据

  132. * @param entity

  133. * @return

  134. */

  135. @RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  136. @ResponseBody

  137. public Map<String,Object> list(Q entity){

  138. Map<String,Object> result = new HashMap<String, Object>();

  139. Page page = getService().findByPage(entity);

  140. result.put("totalCount",page.getTotal());

  141. result.put("result",page.getRows());

  142. return result;

  143. }

  144.  
  145. /**

  146. * 功能描述:判断当前的字典元素是否已经存在

  147. * @param entity

  148. * @return

  149. */

  150. @RequestMapping(value = "/isExist",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)

  151. @ResponseBody

  152. public Map<String,Object> isExist(Q entity){

  153. Map<String,Object> result = new HashMap<String, Object>();

  154. if(getService().query(entity).size()>0){

  155. result.put("valid",false);

  156. }else{

  157. result.put("valid",true);

  158. }

  159. return result;

  160. }

  161.  
  162. /**

  163. * 将首字母变小写

  164. * @param str

  165. * @return

  166. */

  167. private static String toFirstCharLowerCase(String str){

  168. char[] columnCharArr = str.toCharArray();

  169. StringBuffer sb = new StringBuffer();

  170. for (int i = 0; i < columnCharArr.length; i++) {

  171. char cur = columnCharArr[i];

  172. if(i==0){

  173. sb.append(Character.toLowerCase(cur));

  174. }else{

  175. sb.append(cur);

  176. }

  177. }

  178. return sb.toString();

  179. }

  180.  
  181. }


添加好上面的实体以后我们就完成了我们开发基础工具类的基本工作,效果如下所示:

 

接着我们改造我们之前的用户模块的代码,先打开我们的mybatis->mapper底下的mybatis_user.xml我们重新改造该文件结果如下:

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  3. <mapper namespace="com.csdn.demo.sys.dao.UserDao">

  4.  
  5. <resultMap type="com.csdn.demo.sys.entity.User" id="UserMap">

  6. <id property="id" column="id"/>

  7. <result property="login" column="login"/>

  8. <result property="password" column="password"/>

  9. <result property="userName" column="user_name"/>

  10. <result property="address" column="address"/>

  11. <result property="job" column="job"/>

  12. <result property="groupId" column="group_id"/>

  13. <result property="birthDate" column="birth_date"/>

  14. <result property="city" column="city"/>

  15. <result property="district" column="district"/>

  16. <result property="province" column="province"/>

  17. <result property="streetAddress" column="street_address"/>

  18. <result property="state" column="state"/>

  19. <result property="type" column="type"/>

  20. <result property="lastLoginDate" column="last_login_date"/>

  21. </resultMap>

  22.  
  23. <!-- 包含角色信息的map -->

  24. <resultMap type="com.csdn.demo.sys.entity.User" id="UserLoginMap">

  25. <id property="id" column="id"/>

  26. <result property="login" column="login"/>

  27. <result property="password" column="password"/>

  28. <result property="userName" column="user_name"/>

  29. <result property="address" column="address"/>

  30. <result property="job" column="job"/>

  31. <result property="groupId" column="group_id"/>

  32. <result property="birthDate" column="birth_date"/>

  33. <result property="city" column="city"/>

  34. <result property="district" column="district"/>

  35. <result property="province" column="province"/>

  36. <result property="streetAddress" column="street_address"/>

  37. <result property="state" column="state"/>

  38. <result property="type" column="type"/>

  39. <result property="lastLoginDate" column="last_login_date"/>

  40. <collection property="roles" ofType="com.csdn.demo.sys.entity.UserRole" javaType="java.util.ArrayList">

  41. <result column="user_role_id" property="id" jdbcType="VARCHAR" />

  42. <result column="name" property="name" jdbcType="VARCHAR" />

  43. <result column="role_name" property="roleName" jdbcType="VARCHAR" />

  44. </collection>

  45. </resultMap>

  46.  
  47. <!-- 根据账号来获取用户信息 -->

  48. <select id="findByLogin" parameterType="java.lang.String" resultMap="UserLoginMap">

  49. select u.*,ur.id as user_role_id,ur.name,ur.role_name from user u inner join user_associate_role uar on u.id = uar.user_id inner join user_role ur on uar.role_id = ur.id where u.login = #{login}

  50. </select>

  51.  
  52. <!--根据主键获取对象-->

  53. <select id="get" parameterType="com.csdn.demo.sys.entity.User" resultMap="UserMap">

  54. select u.* from user u

  55. WHERE id=#{id}

  56. </select>

  57.  
  58. <!--保存-->

  59. <insert id="save" parameterType="com.csdn.demo.sys.entity.User" useGeneratedKeys="true" keyProperty="id">

  60. INSERT INTO user(login,password,user_name,address,job,group_id,birth_date,city,district,province,street_address,state,type,last_login_date)

  61. VALUES(#{login},#{password},#{userName},#{address},#{job},#{orgGroup.groupId},#{birthDate},#{city},#{district},#{province},#{streetAddress},#{state},#{type},#{lastLoginDate})

  62. </insert>

  63.  
  64. <!--修改-->

  65. <update id="update" parameterType="com.csdn.demo.sys.entity.User">

  66. UPDATE user SET user_name=#{userName},address=#{address},job=#{job},group_id=#{orgGroup.groupId},birth_date=#{birthDate},city=#{city},district=#{district},province=#{province},street_address=#{streetAddress}

  67. WHERE id=#{id}

  68. </update>

  69.  
  70. <!--删除-->

  71. <delete id="delete" parameterType="com.csdn.demo.sys.entity.User">

  72. DELETE FROM user WHERE id=#{id}

  73. </delete>

  74.  
  75. <!--分页查询组织架构底下的用户-->

  76. <select id="findGroupUserByPage" parameterType="com.csdn.demo.sys.entity.QueryUser" resultMap="UserMap">

  77. select u.* from user u

  78. WHERE 1=1

  79. <if test="userName!=null and userName!='' ">

  80. AND u.user_name like concat(#{userName},'%')

  81. </if>

  82. <if test="sort!= null and sort!='' ">

  83. order by ${sort} ${order}

  84. </if>

  85. limit #{offset},#{limit}

  86. </select>

  87.  
  88. <!--统计组织架构底下的用户-->

  89. <select id="countGroupUser" parameterType="com.csdn.demo.sys.entity.QueryUser" resultType="int">

  90. select count(1) from user u

  91. WHERE 1=1

  92. <if test="userName!=null and userName!='' ">

  93. AND u.user_name like concat(#{userName},'%')

  94. </if>

  95. </select>

  96.  
  97. <!--分页查询-->

  98. <select id="findByPage" parameterType="com.csdn.demo.sys.entity.QueryUser" resultMap="UserMap">

  99. select u.* from user u

  100. WHERE 1=1

  101. <if test="login!=null and login!='' ">

  102. AND u.login=#{login}

  103. </if>

  104. <if test="password!=null and password!='' ">

  105. AND u.password=#{password}

  106. </if>

  107. <if test="userName!=null and userName!='' ">

  108. AND u.user_name=#{userName}

  109. </if>

  110. <if test="address!=null and address!='' ">

  111. AND u.address=#{address}

  112. </if>

  113. <if test="job!=null and job!='' ">

  114. AND u.job=#{job}

  115. </if>

  116. <if test="groupId!=null and groupId!='' ">

  117. AND u.group_id=#{groupId}

  118. </if>

  119. <if test="birthDate!=null and birthDate!='' ">

  120. AND u.birth_date=#{birthDate}

  121. </if>

  122. <if test="city!=null and city!='' ">

  123. AND u.city=#{city}

  124. </if>

  125. <if test="district!=null and district!='' ">

  126. AND u.district=#{district}

  127. </if>

  128. <if test="province!=null and province!='' ">

  129. AND u.province=#{province}

  130. </if>

  131. <if test="streetAddress!=null and streetAddress!='' ">

  132. AND u.street_address=#{streetAddress}

  133. </if>

  134. <if test="state!=null and state!='' ">

  135. AND u.state=#{state}

  136. </if>

  137. <if test="type!=null and type!='' ">

  138. AND u.type=#{type}

  139. </if>

  140. <if test="lastLoginDate!=null and lastLoginDate!='' ">

  141. AND u.last_login_date=#{lastLoginDate}

  142. </if>

  143. <if test="sort!= null and sort!='' ">

  144. order by ${sort} ${order}

  145. </if>

  146. limit #{offset},#{limit}

  147. </select>

  148.  
  149. <!--统计-->

  150. <select id="count" parameterType="com.csdn.demo.sys.entity.QueryUser" resultType="int">

  151. SELECT count(*) FROM user

  152. WHERE 1=1

  153. <if test="login!=null and login!='' ">

  154. AND login=#{login}

  155. </if>

  156. <if test="password!=null and password!='' ">

  157. AND password=#{password}

  158. </if>

  159. <if test="userName!=null and userName!='' ">

  160. AND user_name=#{userName}

  161. </if>

  162. <if test="address!=null and address!='' ">

  163. AND address=#{address}

  164. </if>

  165. <if test="job!=null and job!='' ">

  166. AND job=#{job}

  167. </if>

  168. <if test="groupId!=null and groupId!='' ">

  169. AND group_id=#{groupId}

  170. </if>

  171. <if test="birthDate!=null and birthDate!='' ">

  172. AND birth_date=#{birthDate}

  173. </if>

  174. <if test="city!=null and city!='' ">

  175. AND city=#{city}

  176. </if>

  177. <if test="district!=null and district!='' ">

  178. AND district=#{district}

  179. </if>

  180. <if test="province!=null and province!='' ">

  181. AND province=#{province}

  182. </if>

  183. <if test="streetAddress!=null and streetAddress!='' ">

  184. AND street_address=#{streetAddress}

  185. </if>

  186. <if test="state!=null and state!='' ">

  187. AND state=#{state}

  188. </if>

  189. <if test="type!=null and type!='' ">

  190. AND type=#{type}

  191. </if>

  192. <if test="lastLoginDate!=null and lastLoginDate!='' ">

  193. AND last_login_date=#{lastLoginDate}

  194. </if>

  195. <if test="sort!= null and sort!='' ">

  196. order by ${sort} ${order}

  197. </if>

  198. </select>

  199.  
  200. <!--查询-->

  201. <select id="query" parameterType="com.csdn.demo.sys.entity.QueryUser" resultMap="UserMap">

  202. SELECT id,login,password,user_name,address,job,group_id,birth_date,city,district,province,street_address,state,type,last_login_date FROM user

  203. WHERE 1=1

  204. <if test="login!=null and login!='' ">

  205. AND login=#{login}

  206. </if>

  207. <if test="password!=null and password!='' ">

  208. AND password=#{password}

  209. </if>

  210. <if test="userName!=null and userName!='' ">

  211. AND user_name=#{userName}

  212. </if>

  213. <if test="address!=null and address!='' ">

  214. AND address=#{address}

  215. </if>

  216. <if test="job!=null and job!='' ">

  217. AND job=#{job}

  218. </if>

  219. <if test="groupId!=null and groupId!='' ">

  220. AND group_id=#{groupId}

  221. </if>

  222. <if test="birthDate!=null and birthDate!='' ">

  223. AND birth_date=#{birthDate}

  224. </if>

  225. <if test="city!=null and city!='' ">

  226. AND city=#{city}

  227. </if>

  228. <if test="district!=null and district!='' ">

  229. AND district=#{district}

  230. </if>

  231. <if test="province!=null and province!='' ">

  232. AND province=#{province}

  233. </if>

  234. <if test="streetAddress!=null and streetAddress!='' ">

  235. AND street_address=#{streetAddress}

  236. </if>

  237. <if test="state!=null and state!='' ">

  238. AND state=#{state}

  239. </if>

  240. <if test="type!=null and type!='' ">

  241. AND type=#{type}

  242. </if>

  243. <if test="lastLoginDate!=null and lastLoginDate!='' ">

  244. AND last_login_date=#{lastLoginDate}

  245. </if>

  246. <if test="sort!= null and sort!='' ">

  247. order by ${sort} ${order}

  248. </if>

  249. </select>

  250.  
  251. </mapper>


 

 


接着改造我们的java层,首先在我们的sys包的entity底下创建一个QueryUser.java文件如下所示:

 

 
  1. /**

  2. *@author linzf

  3. **/

  4. public class QueryUser extends QueryBase {

  5. private String login;

  6. private String password;

  7. private String userName;

  8. private String address;

  9. private String job;

  10. private Long groupId;

  11. private String birthDate;

  12. private String city;

  13. private String district;

  14. private String province;

  15. private String streetAddress;

  16. private String state;

  17. private String type;

  18. private String lastLoginDate;

  19.  
  20.  
  21. public String getLogin() {

  22. return login;

  23. }

  24.  
  25. public void setLogin(String login) {

  26. this.login = login;

  27. }

  28.  
  29. public String getPassword() {

  30. return password;

  31. }

  32.  
  33. public void setPassword(String password) {

  34. this.password = password;

  35. }

  36.  
  37. public String getUserName() {

  38. return userName;

  39. }

  40.  
  41. public void setUserName(String userName) {

  42. this.userName = userName;

  43. }

  44.  
  45. public String getAddress() {

  46. return address;

  47. }

  48.  
  49. public void setAddress(String address) {

  50. this.address = address;

  51. }

  52.  
  53. public String getJob() {

  54. return job;

  55. }

  56.  
  57. public void setJob(String job) {

  58. this.job = job;

  59. }

  60.  
  61. public Long getGroupId() {

  62. return groupId;

  63. }

  64.  
  65. public void setGroupId(Long groupId) {

  66. this.groupId = groupId;

  67. }

  68.  
  69. public String getBirthDate() {

  70. return birthDate;

  71. }

  72.  
  73. public void setBirthDate(String birthDate) {

  74. this.birthDate = birthDate;

  75. }

  76.  
  77. public String getCity() {

  78. return city;

  79. }

  80.  
  81. public void setCity(String city) {

  82. this.city = city;

  83. }

  84.  
  85. public String getDistrict() {

  86. return district;

  87. }

  88.  
  89. public void setDistrict(String district) {

  90. this.district = district;

  91. }

  92.  
  93. public String getProvince() {

  94. return province;

  95. }

  96.  
  97. public void setProvince(String province) {

  98. this.province = province;

  99. }

  100.  
  101. public String getStreetAddress() {

  102. return streetAddress;

  103. }

  104.  
  105. public void setStreetAddress(String streetAddress) {

  106. this.streetAddress = streetAddress;

  107. }

  108.  
  109. public String getState() {

  110. return state;

  111. }

  112.  
  113. public void setState(String state) {

  114. this.state = state;

  115. }

  116.  
  117. public String getType() {

  118. return type;

  119. }

  120.  
  121. public void setType(String type) {

  122. this.type = type;

  123. }

  124.  
  125. public String getLastLoginDate() {

  126. return lastLoginDate;

  127. }

  128.  
  129. public void setLastLoginDate(String lastLoginDate) {

  130. this.lastLoginDate = lastLoginDate;

  131. }

  132.  
  133. }

接着改造我们的UserDao如下所示:

 

 

 
  1. /**

  2. *@author linzf

  3. **/

  4. public interface UserDao extends GenericDao<User, QueryUser> {

  5.  
  6. /**

  7. * 功能描述:根据账号来获取用户信息

  8. * @param login

  9. * @return

  10. */

  11. User findByLogin(String login);

  12.  
  13.  
  14. }

接着在sys包底下创建一个service包,并创建UserService.java文件如下所示:

 

 

 
  1. /*

  2. * 类描述:

  3. * @auther linzf

  4. * @create 2017/12/8 0008

  5. */

  6. @Service("userService")

  7. @Transactional(rollbackFor={IllegalArgumentException.class})

  8. public class UserService extends GenericService<User, QueryUser> {

  9.  
  10.  
  11. @Autowired

  12. @SuppressWarnings("SpringJavaAutowiringInspection")

  13. private UserDao userDao;

  14.  
  15. @Override

  16. protected GenericDao<User, QueryUser> getDao() {

  17. return userDao;

  18. }

  19. }


接着改造我们的controller层代码如下:

 

 

 
  1. /*

  2. * 类描述:用户维护controller

  3. * @auther linzf

  4. * @create 2017/9/7 0007

  5. */

  6. @Controller

  7. @RequestMapping("/user")

  8. public class UserController extends GenericController<User,QueryUser> {

  9.  
  10. @Inject

  11. private UserService userService;

  12.  
  13. @Override

  14. protected GenericService<User, QueryUser> getService() {

  15. return userService;

  16. }

  17. }

 

配置好以后我们的整个的效果如下:

接着我们重新加载我们的代码并把整个项目运行起来,然后去访问我们的swagger2的目录,你会发现我们已经多了很多的功能了,以后我们有了新的模块的时候我们只需要继承我们的类就可以实现快速的开发了,效果如下:



        到此处我们完成了我们的基础开发工具的百分30的开发工作了,因为到这里我们还需要自己去编写我们的dao、service、controller以及mybatis配置文件,这个过程完全是一个干苦力的过程,因此在下一章我们将编写一个工具类来帮我们快速生成以上的代码,该项目的GitHub地址:https://github.com/185594-5-27/csdndemo/tree/base-druid-swagger-tool-one

 

上一篇文章地址:基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【三】【整合swagger2和druid】

 

 

下一篇文章地址:基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【五】【编写基础代码快速生成工具】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值