Commit 2856ce65 authored by fangzhipeng's avatar fangzhipeng

构建通用dao层

parent 3b329be9
package com.bigsys.auth.project;
package com.bigsys.auth.project.config;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
......@@ -39,18 +39,19 @@ public class ShiroConfig {
// 拦截器.
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
// 配置不会被拦截的链接 顺序判断
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/ajaxLogin", "anon");
// 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
filterChainDefinitionMap.put("/logout", "logout");
filterChainDefinitionMap.put("/add", "perms[权限添加]");
// <!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
// <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
filterChainDefinitionMap.put("/**", "authc");
filterChainDefinitionMap.put("/**", "anon");
// // 配置不会被拦截的链接 顺序判断
// filterChainDefinitionMap.put("/static/**", "anon");
// filterChainDefinitionMap.put("/ajaxLogin", "anon");
//
// // 配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
// filterChainDefinitionMap.put("/logout", "logout");
//
// filterChainDefinitionMap.put("/add", "perms[权限添加]");
//
// // <!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
// // <!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
// filterChainDefinitionMap.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
System.out.println("Shiro拦截器工厂类注入成功");
......
......@@ -2,10 +2,9 @@ package com.bigsys.auth.project.controller;
import com.bigsys.auth.project.db.model.User;
import com.bigsys.auth.project.service.UserService;
import com.bigsys.auth.project.util.BSResponse;
import com.bigsys.auth.project.util.response.BSResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;
import javax.annotation.Resource;
......@@ -22,4 +21,10 @@ public class UserController {
return BSResponse.ok();
}
@RequestMapping(value = "/getById")
public BSResponse getById (String id) {
User user = userService.selectByPrimaryKey(id);
return BSResponse.ok(user);
}
}
package com.bigsys.auth.project.service;
import com.bigsys.auth.project.db.model.User;
import com.github.pagehelper.Page;
import com.bigsys.auth.project.db.model.UserExample;
import com.bigsys.auth.project.util.service.BaseService;
import com.github.pagehelper.PageInfo;
public interface UserService {
public interface UserService extends BaseService<String, User, UserExample, UserExample.Criteria> {
void addOrUpdate(User model);
......
......@@ -4,10 +4,8 @@ import com.bigsys.auth.project.db.dao.UserMapper;
import com.bigsys.auth.project.db.model.User;
import com.bigsys.auth.project.db.model.UserExample;
import com.bigsys.auth.project.service.UserService;
import com.bigsys.auth.project.util.CreateExample;
import com.bigsys.auth.project.util.UUIDGenarator;
import com.bigsys.auth.project.util.dao.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.bigsys.auth.project.util.service.BaseServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;
......
package com.bigsys.auth.project.util.service;
import com.bigsys.auth.project.util.CreateExample;
import com.github.pagehelper.PageInfo;
import java.util.List;
public interface BaseService<key, model, example, criteria> {
long countByExample(CreateExample<example, criteria> createExample);
int deleteByExample(CreateExample<example, criteria> createExample);
int deleteByPrimaryKey(key id);
int insert(model record);
int insertSelective(model record);
List<model> selectByExample(CreateExample<example, criteria> createExample);
model selectByPrimaryKey(key id);
int updateByExampleSelective(model record, CreateExample<example, criteria> createExample);
int updateByExample(model record, CreateExample<example, criteria> createExample);
int updateByPrimaryKeySelective(model record);
int updateByPrimaryKey(model record);
PageInfo<model> selectByExampleAndPage(PageInfo<model> page, CreateExample<example, criteria> createExample);
}
package com.bigsys.auth.project.util.dao;
package com.bigsys.auth.project.util.service;
import com.bigsys.auth.project.util.CreateExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xml.internal.security.Init;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class BaseServiceImpl<key, model, example, criteria> {
public abstract class BaseServiceImpl<key, model, example, criteria> implements BaseService<key, model, example, criteria> {
private Map<String, Method> methodMap = new HashMap<>();
private final String MYBATIS_COUNTBYEXAMPLE = "countByExample";
private final String MYBATIS_DELETEBYEXAMPLE = "deleteByExample";
private final String MYBATIS_DELETEBYPRIMARYKEY = "deleteByPrimaryKey";
private final String MYBATIS_INSERT = "insert";
private final String MYBATIS_INSERTSELECTIVE = "insertSelective";
private final String MYBATIS_SELECTBYEXAMPLE = "selectByExample";
private final String MYBATIS_SELECTBYPRIMARYKEY = "selectByPrimaryKey";
private final String MYBATIS_UPDATEBYEXAMPLESELECTIVE = "updateByExampleSelective";
private final String MYBATIS_UPDATEBYEXAMPLE = "updateByExample";
private final String MYBATIS_UPDATEBYPRIMARYKEYSELECTIVE = "updateByPrimaryKeySelective";
private final String MYBATIS_UPDATEBYPRIMARYKEY = "updateByPrimaryKey";
private final String MYBATIS_CREATECRITERIA = "createCriteria";
public abstract Object getDao();
public abstract Class getExample();
public void init() {
@Override
public long countByExample(CreateExample<example, criteria> createExample) {
return (long) invokeMethod(getDao(), MYBATIS_COUNTBYEXAMPLE, produceExample(createExample));
}
@Override
public int deleteByExample(CreateExample<example, criteria> createExample) {
return (int) invokeMethod(getDao(), MYBATIS_DELETEBYEXAMPLE, produceExample(createExample));
}
@Override
public int deleteByPrimaryKey(key id) {
return (int) invokeMethod(getDao(), MYBATIS_DELETEBYPRIMARYKEY, id);
}
@Override
public int insert(model record) {
return (int) invokeMethod(getDao(), MYBATIS_INSERT, record);
}
@Override
public int insertSelective(model record) {
return (int) invokeMethod(getDao(), MYBATIS_INSERTSELECTIVE, record);
}
@Override
public List<model> selectByExample(CreateExample<example, criteria> createExample) {
return (List<model>) invokeMethod(getDao(), MYBATIS_SELECTBYEXAMPLE, produceExample(createExample));
}
@Override
public model selectByPrimaryKey(key id) {
return (model) invokeMethod(getDao(), MYBATIS_SELECTBYPRIMARYKEY, id);
}
@Override
public int updateByExampleSelective(model record, CreateExample<example, criteria> createExample) {
return (int) invokeMethod(getDao(), MYBATIS_UPDATEBYEXAMPLESELECTIVE, record, produceExample(createExample));
}
@Override
public int updateByExample(model record, CreateExample<example, criteria> createExample) {
return (int) invokeMethod(getDao(), MYBATIS_UPDATEBYEXAMPLE, record, produceExample(createExample));
}
@Override
public int updateByPrimaryKeySelective(model record) {
return (int) invokeMethod(getDao(), MYBATIS_UPDATEBYPRIMARYKEYSELECTIVE, record);
}
@Override
public int updateByPrimaryKey(model record) {
return (int) invokeMethod(getDao(), MYBATIS_UPDATEBYPRIMARYKEY, record);
}
@Override
public PageInfo<model> selectByExampleAndPage(PageInfo<model> page, CreateExample<example, criteria> createExample) {
page = PageHelper.startPage(page).doSelectPageInfo(() -> {
invokeMethod(getDao(), MYBATIS_SELECTBYEXAMPLE, produceExample(createExample));
});
return page;
}
private void init() {
Object dao = getDao();
Class clazz = dao.getClass();
Method[] methods = clazz.getMethods();
......@@ -30,16 +102,14 @@ public abstract class BaseServiceImpl<key, model, example, criteria> {
}
}
public PageInfo<model> selectByExampleAndPage(PageInfo<model> page, CreateExample<example, criteria> createExample) {
private example produceExample(CreateExample<example, criteria> createExample) {
example example = null;
try {
example example = (example) getExample().newInstance();
example = (example) getExample().newInstance();
Class clazz = example.getClass();
criteria criteria = (criteria) clazz.getMethod(MYBATIS_CREATECRITERIA).invoke(example);
createExample.createExample(example, criteria);
page = PageHelper.startPage(page).doSelectPageInfo(() -> {
invokeMethod(getDao(), MYBATIS_SELECTBYEXAMPLE, example);
});
return page;
return example;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
......@@ -52,7 +122,7 @@ public abstract class BaseServiceImpl<key, model, example, criteria> {
return null;
}
Object invokeMethod(Object obj, String method, Object... args) {
private Object invokeMethod(Object obj, String method, Object... args) {
if (methodMap.size() == 0) {
init();
}
......
......@@ -20,10 +20,7 @@ public class BigsysAuthSpringbootApplicationTests {
@Test
public void contextLoads() {
PageInfo<User> page = new PageInfo<User>();
page.setPageNum(11);
page.setPageSize(10);
userService.page(null, page);
}
System.out.println(userService.countByExample((example, criteria) -> {}));
}
}
......@@ -4,7 +4,7 @@
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 配置Run As Maven build : Goals 参数 : mybatis-generator:generate -Dmybatis.generator.overwrite=true -->
<!-- 配置 tableName,使用 Run As Maven build 生成 dao model 层 -->
<!-- 配置 tableName,使用 Run As Maven build 生成 service model 层 -->
<generatorConfiguration>
<!-- 配置文件路径 -->
<properties url="${mybatis.generator.generatorConfig.properties}"/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment