Dao层优化

This commit is contained in:
RuoYi 2018-02-23 17:24:12 +08:00
parent c86bf131be
commit 7ab5c9dfb6
23 changed files with 217 additions and 85 deletions

View File

@ -5,6 +5,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* 启动程序
*
* @author yangzz
*/
@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication

View File

@ -10,18 +10,24 @@ import com.ruoyi.common.tools.StringTools;
*/
public class PageUtilEntity
{
private int totalPage; // 总页数
private int page; // 当前记录起始索引
private int size; // 每页显示记录数
private String orderByColumn; // 排序列
private String isAsc; // 排序的方向 "desc" 或者 "asc".
protected String orderCond; // 排序
private boolean entityOrField; // true:需要分页的地方传入的参数就是Page实体false:需要分页的地方传入的参数所代表的实体拥有Page属性
private int totalResult; // 总记录数
protected Map<String, String> relationMap; // 请求参数
// 总页数
private int totalPage;
// 当前记录起始索引
private int page;
// 每页显示记录数
private int size;
// 排序列
private String orderByColumn;
// 排序的方向 "desc" 或者 "asc".
private String isAsc;
// 排序
protected String orderCond;
// true:需要分页的地方传入的参数就是Page实体false:需要分页的地方传入的参数所代表的实体拥有Page属性
private boolean entityOrField;
// 总记录数
private int totalResult;
// 请求参数
protected Map<String, String> relationMap;
public int getTotalPage()
{

View File

@ -14,8 +14,10 @@ import org.springframework.stereotype.Component;
@Component
public final class SpringUtils implements BeanFactoryPostProcessor
{
private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境
// Spring应用上下文环境
private static ConfigurableListableBeanFactory beanFactory;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
SpringUtils.beanFactory = beanFactory;

View File

@ -5,6 +5,11 @@ import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 通用配置
*
* @author yangzz
*/
@Configuration
public class BaseConfig extends WebMvcConfigurerAdapter
{

View File

@ -3,6 +3,11 @@ package com.ruoyi.framework.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 配置信息
*
* @author yangzz
*/
@Component
@ConfigurationProperties(prefix = "ruoyi")
public class Configuration

View File

@ -119,9 +119,11 @@ public class DruidDBConfig
{
ServletRegistrationBean reg = new ServletRegistrationBean();
reg.setServlet(new StatViewServlet());
// 白名单
reg.addUrlMappings("/druid/*");
reg.addInitParameter("allow", ""); // 白名单
// reg.addInitParameter("deny",""); //黑名单
reg.addInitParameter("allow", "");
// 黑名单
// reg.addInitParameter("deny","");
// reg.addInitParameter("loginUsername", "admin");
// reg.addInitParameter("loginPassword", "admin");
return reg;

View File

@ -16,7 +16,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.project.shiro.realm.UserRealm;
import com.ruoyi.project.system.menu.service.MenuService;
import com.ruoyi.project.system.menu.service.MenuServiceImpl;
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
/**
@ -103,7 +103,7 @@ public class ShiroConfig
// 退出 logout地址shiro去清除session
filterChainDefinitionMap.put("/logout", "logout");
// 系统权限列表
MenuService menuService = SpringUtils.getBean(MenuService.class);
MenuServiceImpl menuService = SpringUtils.getBean(MenuServiceImpl.class);
filterChainDefinitionMap.putAll(menuService.findAllPerms());
// 所有请求需要认证
filterChainDefinitionMap.put("/**", "authc");

View File

@ -1,6 +0,0 @@
package com.ruoyi.framework.core;
public class Test
{
}

View File

@ -1,11 +1,18 @@
package com.ruoyi.framework.core.dao;
import java.util.List;
import javax.annotation.Resource;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.project.shiro.exception.base.DaoException;
import com.ruoyi.project.shiro.realm.UserRealm;
/**
* 数据DAO层通用数据处理
@ -14,6 +21,7 @@ import org.mybatis.spring.SqlSessionTemplate;
*/
public class DynamicObjectBaseDao
{
private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
@Resource(name = "sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
@ -28,7 +36,17 @@ public class DynamicObjectBaseDao
*/
public int save(String str, Object obj)
{
return sqlSessionTemplate.insert(str, obj);
int rows;
try
{
rows = sqlSessionTemplate.insert(str, obj);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("插入失败");
}
return rows;
}
/**
@ -41,7 +59,17 @@ public class DynamicObjectBaseDao
*/
public int batchSave(String str, List<?> objs)
{
return sqlSessionTemplate.insert(str, objs);
int rows;
try
{
rows = sqlSessionTemplate.insert(str, objs);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("批量更新失败");
}
return rows;
}
/**
@ -54,7 +82,17 @@ public class DynamicObjectBaseDao
*/
public int update(String str, Object obj)
{
return sqlSessionTemplate.update(str, obj);
int rows;
try
{
rows = sqlSessionTemplate.update(str, obj);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("修改失败");
}
return rows;
}
/**
@ -97,9 +135,19 @@ public class DynamicObjectBaseDao
* @return
* @throws Exception
*/
public Object batchDelete(String str, List<?> objs) throws Exception
public int batchDelete(String str, List<?> objs) throws Exception
{
return sqlSessionTemplate.delete(str, objs);
int rows;
try
{
rows = sqlSessionTemplate.update(str, objs);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("批量删除失败");
}
return rows;
}
/**
@ -110,22 +158,42 @@ public class DynamicObjectBaseDao
* @return
* @throws Exception
*/
public Object delete(String str, Object obj)
public int delete(String str, Object obj)
{
return sqlSessionTemplate.delete(str, obj);
int rows;
try
{
rows = sqlSessionTemplate.delete(str, obj);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("删除失败");
}
return rows;
}
/**
* 查找对象
* 查找单条对象
*
* @param str
* @param obj
* @return
* @throws Exception
*/
public Object findForObject(String str, Object obj)
public <T> T findForObject(String str, Object obj)
{
return sqlSessionTemplate.selectOne(str, obj);
T t;
try
{
t = sqlSessionTemplate.selectOne(str, obj);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("查找单条对象失败");
}
return t;
}
/**
@ -136,9 +204,19 @@ public class DynamicObjectBaseDao
* @return
* @throws Exception
*/
public Object findForList(String str) throws Exception
public <E> List<E> findForList(String str) throws Exception
{
return sqlSessionTemplate.selectList(str);
List<E> list;
try
{
list = sqlSessionTemplate.selectList(str);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("查找所有对象-无条件。失败");
}
return list;
}
/**
@ -149,9 +227,19 @@ public class DynamicObjectBaseDao
* @return
* @throws Exception
*/
public Object findForList(String str, Object obj) throws Exception
public <E> List<E> findForList(String str, Object obj)
{
return sqlSessionTemplate.selectList(str, obj);
List<E> list;
try
{
list = sqlSessionTemplate.selectList(str, obj);
}
catch (Exception e)
{
log.error(e.getMessage());
throw new DaoException("查找所有对象-有条件。失败");
}
return list;
}
public Object findForMap(String str, Object obj, String key, String value) throws Exception

View File

@ -8,13 +8,13 @@ package com.ruoyi.project.shiro.common;
public class UserConstants
{
/** 正常状态 */
public static final String normal = "0";
public static final String NORMAL = "0";
/** 封禁状态 */
public static final String blocked = "1";
public static final String BLOCKED = "1";
/** 异常状态 */
public static final String exception = "1";
public static final String EXCEPTION = "1";
/** 名称是否唯一的返回结果码 */
public final static String NAME_UNIQUE = "0";

View File

@ -62,6 +62,7 @@ public class BaseException extends RuntimeException
this(null, null, null, defaultMessage);
}
@Override
public String getMessage()
{
String message = null;
@ -96,6 +97,7 @@ public class BaseException extends RuntimeException
return defaultMessage;
}
@Override
public String toString()
{
return this.getClass() + "{" + "module='" + module + '\'' + ", message='" + getMessage() + '\'' + '}';

View File

@ -0,0 +1,32 @@
package com.ruoyi.project.shiro.exception.base;
/**
* Dao异常
*
* @author yangzz
*/
public class DaoException extends RuntimeException
{
private static final long serialVersionUID = 1L;
/**
* 错误消息
*/
private String defaultMessage;
public DaoException(String defaultMessage)
{
this.defaultMessage = defaultMessage;
}
public String getDefaultMessage()
{
return defaultMessage;
}
@Override
public String toString()
{
return this.getClass() + "{" + "message='" + getMessage() + '\'' + '}';
}
}

View File

@ -59,7 +59,7 @@ public class LoginService
passwordService.validate(user, password);
if (UserConstants.blocked.equals(user.getStatus()))
if (UserConstants.BLOCKED.equals(user.getStatus()))
{
throw new UserBlockedException(user.getRefuseDes());
}

View File

@ -36,7 +36,7 @@ import com.ruoyi.project.system.user.domain.User;
public class UserRealm extends AuthorizingRealm
{
private static final Logger log = LoggerFactory.getLogger(LoginService.class);
private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
@Autowired
private IMenuService menuService;

View File

@ -1,6 +0,0 @@
package com.ruoyi.project.system;
public class Test
{
}

View File

@ -11,7 +11,7 @@ import com.ruoyi.project.system.menu.domain.Menu;
* @author yangzz
*/
@Repository("menuDao")
public class MenuDao extends DynamicObjectBaseDao implements IMenuDao
public class MenuDaoImpl extends DynamicObjectBaseDao implements IMenuDao
{
/**
@ -20,13 +20,13 @@ public class MenuDao extends DynamicObjectBaseDao implements IMenuDao
* @param userId 用户ID
* @return 菜单列表
*/
@SuppressWarnings("unchecked")
@Override
public List<Menu> findMenusByUserId(Long userId)
{
List<Menu> permsList = null;
try
{
permsList = (List<Menu>) this.findForList("SystemMenuMapper.findMenusByUserId", userId);
permsList = this.findForList("SystemMenuMapper.findMenusByUserId", userId);
}
catch (Exception e)
{
@ -41,13 +41,13 @@ public class MenuDao extends DynamicObjectBaseDao implements IMenuDao
* @param userId 用户ID
* @return 菜单列表
*/
@SuppressWarnings("unchecked")
@Override
public List<String> findPermsByUserId(Long userId)
{
List<String> permsList = null;
try
{
permsList = (List<String>) this.findForList("SystemMenuMapper.findPermsByUserId", userId);
permsList = this.findForList("SystemMenuMapper.findPermsByUserId", userId);
}
catch (Exception e)
{
@ -61,13 +61,13 @@ public class MenuDao extends DynamicObjectBaseDao implements IMenuDao
*
* @return 权限列表
*/
@SuppressWarnings("unchecked")
@Override
public List<Menu> findAllPerms()
{
List<Menu> permsList = null;
try
{
permsList = (List<Menu>) this.findForList("SystemMenuMapper.findAllPerms");
permsList = this.findForList("SystemMenuMapper.findAllPerms");
}
catch (Exception e)
{

View File

@ -22,7 +22,7 @@ import com.ruoyi.project.util.TreeUtil;
* @author yangzz
*/
@Service("menuService")
public class MenuService implements IMenuService
public class MenuServiceImpl implements IMenuService
{
public static final String PREMISSION_STRING = "perms[\"{0}\"]";

View File

@ -15,7 +15,7 @@ public interface IUserDao
/**
* 根据条件分页查询用户对象
*
* @param page 分页对象
* @param pageUtilEntity 分页对象
* @return 用户信息集合信息
*/
public List<User> pageInfoQuery(PageUtilEntity pageUtilEntity);

View File

@ -14,7 +14,7 @@ import com.ruoyi.project.system.user.domain.User;
* @author yangzz
*/
@Repository("userDao")
public class UserDao extends DynamicObjectBaseDao implements IUserDao
public class UserDaoImpl extends DynamicObjectBaseDao implements IUserDao
{
/**
@ -23,21 +23,19 @@ public class UserDao extends DynamicObjectBaseDao implements IUserDao
* @param page 分页对象
* @return 用户对象信息
*/
@SuppressWarnings("unchecked")
@Override
public List<User> pageInfoQuery(PageUtilEntity pageUtilEntity)
{
List<User> userPageInfo = null;
try
{
userPageInfo = (List<User>) this.findForList("SystemUserMapper.queryUserListByCond", pageUtilEntity);
userPageInfo = this.findForList("SystemUserMapper.queryUserListByCond", pageUtilEntity);
}
catch (Exception e)
{
e.printStackTrace();
}
return userPageInfo;
}
/**
@ -46,9 +44,10 @@ public class UserDao extends DynamicObjectBaseDao implements IUserDao
* @param userName 用户名
* @return 用户对象信息
*/
@Override
public User findByUserName(String username)
{
return (User) this.findForObject("SystemUserMapper.findByUserName", username);
return this.findForObject("SystemUserMapper.findByUserName", username);
}
}

View File

@ -32,8 +32,8 @@ public class User implements Serializable
private String refuseDes;
// 创建时间
private String createTime;
private String roleName; // 角色临时字段
// 角色临时字段
private String roleName;
public User()
{
@ -168,20 +168,11 @@ public class User implements Serializable
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", deptId=" + deptId +
", loginName='" + loginName + '\'' +
", userName='" + userName + '\'' +
", email='" + email + '\'' +
", phonenumber=" + phonenumber +
", password='" + password + '\'' +
", salt=" + salt +
", status=" + status +
", refuseDes=" + refuseDes +
", createTime=" + createTime +
", roleName=" + roleName +
'}';
public String toString()
{
return "User{" + "userId=" + userId + ", deptId=" + deptId + ", loginName='" + loginName + '\'' + ", userName='"
+ userName + '\'' + ", email='" + email + '\'' + ", phonenumber=" + phonenumber + ", password='"
+ password + '\'' + ", salt=" + salt + ", status=" + status + ", refuseDes=" + refuseDes
+ ", createTime=" + createTime + ", roleName=" + roleName + '}';
}
}

View File

@ -15,7 +15,7 @@ public interface IUserService
/**
* 根据条件分页查询用户对象
*
* @param page 分页对象
* @param pageUtilEntity 分页对象
* @return 用户信息集合信息
*/
public List<User> pageInfoQuery(PageUtilEntity pageUtilEntity);

View File

@ -13,7 +13,7 @@ import com.ruoyi.project.system.user.domain.User;
* @author yangzz
*/
@Service("userService")
public class UserService implements IUserService
public class UserServiceImpl implements IUserService
{
@Autowired
@ -26,6 +26,7 @@ public class UserService implements IUserService
*
* @return 用户信息集合信息
*/
@Override
public List<User> pageInfoQuery(PageUtilEntity pageUtilEntity)
{
return userDao.pageInfoQuery(pageUtilEntity);
@ -37,6 +38,7 @@ public class UserService implements IUserService
* @param userName 用户名
* @return 用户对象信息
*/
@Override
public User findByUserName(String userName)
{
return userDao.findByUserName(userName);

View File

@ -45,7 +45,8 @@ public class TreeUtil
*/
private static void recursionFn(List<Menu> list, Menu t)
{
List<Menu> childList = getChildList(list, t);// 得到子节点列表
// 得到子节点列表
List<Menu> childList = getChildList(list, t);
t.setChildren(childList);
for (Menu tChild : childList)
{
@ -91,7 +92,9 @@ public class TreeUtil
public List<Menu> getChildPerms(List<Menu> list, int typeId, String prefix)
{
if (list == null)
{
return null;
}
for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
{
Menu node = (Menu) iterator.next();
@ -110,9 +113,11 @@ public class TreeUtil
private void recursionFn(List<Menu> list, Menu node, String p)
{
List<Menu> childList = getChildList(list, node);// 得到子节点列表
// 得到子节点列表
List<Menu> childList = getChildList(list, node);
if (hasChild(list, node))
{// 判断是否有子节点
{
// 判断是否有子节点
returnList.add(node);
Iterator<Menu> it = childList.iterator();
while (it.hasNext())