package com.cutter.web.account.dao.achieve;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.pureart.persistement.database.easydb.DB;
import org.pureart.persistement.database.easydb.IUStH;
import org.pureart.persistement.database.easydb.ParamReadStH;
import org.pureart.persistement.database.easydb.ReadStH;
import com.cutter.web.account.dao.entity.Role;
import com.cutter.web.account.dao.inter.RoleDao;
import com.cutter.web.account.util.StringUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class RoleDaoAchieve implements RoleDao {
private static final Logger log = Logger.getLogger(RoleDaoAchieve.class);
private static final String ROLE_TABLE = " test.dt_role";
private static final String ROLE_SELECT = " SELECT * FROM " + ROLE_TABLE;
private static final String ROLE_GET_BY_ID = ROLE_SELECT + " WHERE id=? ;";
private static final String ROLE_DELETE_BY_ID = " DELETE FROM " + ROLE_TABLE + " WHERE id=? ;";
private static final String ROLE_LIST_ALL = ROLE_SELECT + " ORDER BY createDate DESC ";
private static final String ROLE_LIST_BY_PAGE = ROLE_LIST_ALL + " limit ?,? ;";
private static final String ROLE_ADD = " INSERT INTO " + ROLE_TABLE
+ " (roleName,roleDesc,flag,pid,order,ext,createDate) VALUES(?,?,?,?,?,?,?);";
private static final String ROLE_UPDATE = " update " + ROLE_TABLE
+ " set roleName=? , roleDesc=? , flag=? , pid=? , order=? , ext=? , createDate=? where id=?";
@Override
public boolean update(final Role entity) {
if (null != entity) {
final boolean flag = 0 < entity.getId();
try {
return DB.insertUpdate(flag ? ROLE_UPDATE : ROLE_ADD, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setString(1, entity.getRoleName());
stmt.setString(2, entity.getRoleDesc());
stmt.setInt(3, entity.getFlag());
stmt.setInt(4, entity.getPid());
stmt.setInt(5, entity.getOrder());
stmt.setString(6, entity.getExt());
stmt.setDate(7, StringUtil.transforFromUtilToSqlDate(entity.getCrateDate()));
if (flag) {
stmt.setInt(8, entity.getId());
}
stmt.executeUpdate();
}
});
} catch (SQLException e) {
log.error("更新角色信息异常!");
e.printStackTrace();
}
}
return false;
}
@Override
public Role get(final int id) {
try {
if (0 < id) {
final Role role = new Role();
boolean getResult = DB.select(ROLE_GET_BY_ID, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
if (rs.next()) {
handResult(role, rs);
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
}
});
if (getResult) {
return role;
}
}
} catch (SQLException e) {
log.error("查询单条角色信息异常!");
e.printStackTrace();
}
return null;
}
@Override
public boolean delete(final int id) {
try {
if (0 < id) {
return DB.insertUpdate(ROLE_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
stmt.executeUpdate();
}
});
}
} catch (SQLException e) {
log.error("删除单条角色信息异常!");
e.printStackTrace();
}
return false;
}
@Override
public boolean batchDelete(final int[] idArray) {
try {
if (null != idArray && 1 < idArray.length) {
return DB.insertUpdate(ROLE_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
for (int i : idArray) {
stmt.setInt(1, i);
stmt.addBatch();
}
stmt.executeBatch();
}
});
}
} catch (SQLException e) {
log.error("批量删除角色信息异常!");
e.printStackTrace();
}
return false;
}
@Override
public ImmutableList<Role> listAll() {
try {
final List<Role> roleList = Lists.newLinkedList();
boolean listResult = DB.select(ROLE_LIST_ALL, new ReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
roleList.add(handResult(new Role(), rs));
}
}
});
if (listResult) {
return ImmutableList.copyOf(roleList);
}
} catch (SQLException e) {
log.error("查询全部角色信息异常!");
e.printStackTrace();
}
return null;
}
@Override
public ImmutableList<Role> list(final int pageSize, final int page) {
try {
final List<Role> roleLists = Lists.newLinkedList();
boolean listPageResult = DB.select(ROLE_LIST_BY_PAGE, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
roleLists.add(handResult(new Role(), rs));
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
int start = pageSize * (page - 1);
int end = start + pageSize;
stmt.setInt(1, start);
stmt.setInt(2, end);
}
});
if (listPageResult) {
return ImmutableList.copyOf(roleLists);
}
} catch (SQLException e) {
log.error("分页查询角色信息异常!");
e.printStackTrace();
}
return null;
}
private Role handResult(final Role role, ResultSet rs) throws SQLException {
role.setId(rs.getInt("id"));
role.setRoleName(rs.getString("roleName"));
role.setRoleDesc(rs.getString("roleDesc"));
role.setPid(rs.getInt("pid"));
role.setFlag(rs.getInt("flag"));
role.setCrateDate(StringUtil.transforFromSqlToUtilDate(rs.getDate("createDate")));
role.setExt(rs.getString("ext"));
role.setOrder(rs.getInt("order"));
return role;
}
}
|
package com.cutter.web.account.dao.achieve;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.pureart.persistement.database.easydb.DB;
import org.pureart.persistement.database.easydb.IUStH;
import org.pureart.persistement.database.easydb.ParamReadStH;
import org.pureart.persistement.database.easydb.ReadStH;
import com.cutter.web.account.dao.entity.Permission;
import com.cutter.web.account.dao.inter.PermissionDao;
import com.cutter.web.account.util.StringUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class PermissionDaoAchieve implements PermissionDao {
private static final Logger log = Logger.getLogger(PermissionDaoAchieve.class);
private static final String PERMISSION_TABLE = " test.dt_permission ";
private static final String PERMISSION_SELECT = " SELECT * FROM " + PERMISSION_TABLE;
private static final String PERMISSION_GET_BY_ID = PERMISSION_SELECT + " where id=? ;";
private static final String PERMISSION_GET_BY_ROLEID = PERMISSION_SELECT + " where roleId=? ;";
private static final String PERMISSION_DELETE_BY_ID = " DELETE FROM " + PERMISSION_TABLE + " WHERE id=? ;";
private static final String PERMISSION_LIST_ALL = PERMISSION_SELECT + " ORDER BY createDate DESC ;";
private static final String PERMISSION_LIST_BY_PAGE = PERMISSION_LIST_ALL + " LIMIT ?,? ; ";
private static final String PERMISSION_ADD = " INSERT INTO " + PERMISSION_TABLE
+ " (pName,pFunctionName,pid,pDesc,pUrl,pLevel,order,ext,createDate) VALUES(?,?,?,?,?,?,?,?,?,?)";
private static final String PERMISSION_UPDATE = " UPDATE "
+ PERMISSION_TABLE
+ " SET pName=? , pFunctionName=? , pid=? , pDesc=? , pUrl=? , pLevel=? , order=? , ext=? , createDate=? where id=? ;";
@Override
public boolean update(final Permission entity) {
if (null != entity) {
try {
final boolean flag = 0 < entity.getId();
DB.insertUpdate(flag ? PERMISSION_UPDATE : PERMISSION_ADD, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setString(1, entity.getpName());
stmt.setString(2, entity.getpFunctionName());
stmt.setInt(3, entity.getPid());
stmt.setString(4, entity.getpDesc());
stmt.setString(5, entity.getpUrl());
stmt.setInt(6, entity.getpLevel());
stmt.setInt(7, entity.getOrder());
stmt.setString(8, entity.getExt());
stmt.setDate(9, StringUtil.transforFromUtilToSqlDate(entity.getCreateDate()));
if (flag) {
stmt.setInt(10, entity.getId());
}
stmt.executeUpdate();
}
});
} catch (SQLException e) {
log.error("更新权限信息异常!");
e.printStackTrace();
}
}
return false;
}
@Override
public Permission get(final int id) {
if (0 < id) {
final Permission permission = new Permission();
try {
boolean getResult = DB.select(PERMISSION_GET_BY_ID, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
if (rs.next()) {
handResult(permission, rs);
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
}
});
if (getResult) {
return permission;
}
} catch (SQLException e) {
log.error("查询单条的权限信息异常!");
e.printStackTrace();
}
}
return null;
}
@Override
public boolean delete(final int id) {
if (0 < id) {
try {
return DB.insertUpdate(PERMISSION_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
stmt.executeUpdate();
}
});
} catch (SQLException e) {
log.error("删除单条的权限信息异常!");
e.printStackTrace();
}
}
return false;
}
@Override
public boolean batchDelete(final int[] idArray) {
if (null != idArray && 1 < idArray.length) {
try {
return DB.insertUpdate(PERMISSION_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
for (int i : idArray) {
stmt.setInt(1, i);
stmt.addBatch();
}
stmt.executeBatch();
}
});
} catch (SQLException e) {
log.error("批量删除权限信息异常!");
e.printStackTrace();
}
}
return false;
}
@Override
public ImmutableList<Permission> listAll() {
final List<Permission> permissionList = Lists.newLinkedList();
boolean listResult;
try {
listResult = DB.select(PERMISSION_LIST_ALL, new ReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
permissionList.add(handResult(new Permission(), rs));
}
}
});
if (listResult) {
return ImmutableList.copyOf(permissionList);
}
} catch (SQLException e) {
log.error("查询所有的权限信息异常!");
e.printStackTrace();
}
return null;
}
@Override
public ImmutableList<Permission> list(final int pageSize, final int page) {
final List<Permission> permissionList = Lists.newLinkedList();
boolean listResult;
try {
listResult = DB.select(PERMISSION_LIST_ALL, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
permissionList.add(handResult(new Permission(), rs));
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
int start = pageSize * (page - 1);
int end = start + pageSize;
stmt.setInt(1, start);
stmt.setInt(2, end);
}
});
if (listResult) {
return ImmutableList.copyOf(permissionList);
}
} catch (SQLException e) {
log.error("分页查询权限信息异常!");
e.printStackTrace();
}
return null;
}
private Permission handResult(final Permission permission, ResultSet rs) throws SQLException {
permission.setId(rs.getInt("id"));
permission.setpName(rs.getString("pName"));
permission.setpDesc(rs.getString("pDesc"));
permission.setOrder(rs.getInt("order"));
permission.setCreateDate(StringUtil.transforFromSqlToUtilDate(rs.getDate("createDate")));
permission.setpFunctionName(rs.getString("pFunctionName"));
permission.setExt(rs.getString("ext"));
permission.setPid(rs.getInt("pid"));
permission.setpLevel(rs.getInt("pLevel"));
permission.setpUrl(rs.getString("pUrl"));
return permission;
}
@Override
public ImmutableList<Permission> getPermissionsByRoleId(final int roleId) {
if (0 < roleId) {
final List<Permission> permissions = Lists.newLinkedList();
try {
boolean getResult = DB.select(PERMISSION_GET_BY_ROLEID, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
permissions.add(handResult(new Permission(), rs));
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, roleId);
}
});
if (getResult) {
return ImmutableList.copyOf(permissions);
}
} catch (SQLException e) {
log.error("根据角色查询权限信息异常!");
e.printStackTrace();
}
}
return null;
}
}
|
package com.cutter.web.account.dao.achieve;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.tiles.jsp.taglib.InsertAttributeTag;
import org.pureart.persistement.database.easydb.DB;
import org.pureart.persistement.database.easydb.IUStH;
import org.pureart.persistement.database.easydb.ParamReadStH;
import org.pureart.persistement.database.easydb.ReadStH;
import com.cutter.web.account.dao.entity.Log;
import com.cutter.web.account.dao.inter.LogDao;
import com.cutter.web.account.util.StringUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
public class LogDaoAchieve implements LogDao {
private static final Logger log = Logger.getLogger(LogDaoAchieve.class);
private static final String LOG_TABLE = " test.dt_log ";
private static final String LOG_SELECT = " SELECT * FROM " + LOG_TABLE;
private static final String LOG_GET_BY_ID = LOG_SELECT + " WHERE id=? ;";
private static final String LOG_DELETE_BY_ID = " DELETE FROM " + LOG_TABLE + " WHERE id=? ;";
private static final String LOG_LIST_ALL = LOG_SELECT + " ORDER BY createDate DESC ;";
private static final String LOG_LIST_ALL_BY_PAGE = LOG_SELECT + " ORDER BY createDate DESC limit ?,? ;";
private static final String LOG_LIST_ALL_BY_TYPE = LOG_SELECT + " WHERE type=? ;";
private static final String LOG_ADD = " INSERT INTO " + LOG_TABLE
+ " (userName,ip,type,content,createDate,ext1,ext2) VALUES(?,?,?,?,?,?,?) ;";
private static final String LOG_UPDATE = " UPDATE " + LOG_TABLE
+ " SET userName=? , ip=? , type=? , content=? , createDate=? , ext1=? , ext2=? WHERE id=? ;";
@Override
public boolean update(final Log entity) {
if (null != entity) {
try {
final boolean flag = 0 < entity.getId();
return DB.insertUpdate(flag ? LOG_UPDATE : LOG_ADD, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setString(1, entity.getUserName());
stmt.setString(2, entity.getIp());
stmt.setInt(3, entity.getType());
stmt.setString(4, entity.getContent());
stmt.setDate(5, StringUtil.transforFromUtilToSqlDate(entity.getCreateDate()));
stmt.setString(6, entity.getExt1());
stmt.setString(7, entity.getExt2());
if (flag) {
stmt.setInt(8, entity.getId());
}
stmt.executeUpdate();
}
});
} catch (SQLException e) {
log.error("更新用户日志异常!");
e.printStackTrace();
}
}
return false;
}
@Override
public Log get(final int id) {
try {
final Log log = new Log();
boolean selectResult = DB.select(LOG_GET_BY_ID, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
if (rs.next()) {
handleResult(log, rs);
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
}
});
if (selectResult) {
return log;
}
} catch (SQLException e) {
log.error("查询单条的日志信息异常!");
e.printStackTrace();
}
return null;
}
@Override
public boolean delete(final int id) {
try {
if (0 < id) {
return DB.insertUpdate(LOG_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, id);
stmt.executeUpdate();
}
});
}
} catch (SQLException e) {
log.error("删除用户日志异常!");
e.printStackTrace();
}
return false;
}
@Override
public boolean batchDelete(final int[] idArray) {
try {
if (null != idArray && 1 <= idArray.length) {
return DB.insertUpdate(LOG_DELETE_BY_ID, new IUStH() {
@Override
public void handleInsertUpdate(PreparedStatement stmt) throws SQLException {
for (int i : idArray) {
stmt.setInt(1, i);
stmt.addBatch();
}
stmt.executeBatch();
}
});
}
} catch (SQLException e) {
log.error("批量删除日志异常!");
e.printStackTrace();
}
return false;
}
@Override
public ImmutableList<Log> listAll() {
final List<Log> logList = Lists.newLinkedList();
try {
final boolean listResult = DB.select(LOG_LIST_ALL, new ReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
logList.add(handleResult(new Log(), rs));
}
}
});
if (listResult) {
return ImmutableList.copyOf(logList);
}
} catch (SQLException e) {
log.error("查询所有的日志异常!");
e.printStackTrace();
}
return null;
}
@Override
public ImmutableList<Log> list(final int pageSize, final int page) {
try {
if (0 < page && 0 < pageSize) {
final List<Log> logList = Lists.newLinkedList();
final boolean listResult = DB.select(LOG_LIST_ALL_BY_PAGE, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
logList.add(handleResult(new Log(), rs));
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
int start = pageSize * (page - 1);
int end = start + pageSize;
stmt.setInt(1, start);
stmt.setInt(2, end);
}
});
if (listResult) {
return ImmutableList.copyOf(logList);
}
}
} catch (SQLException e) {
log.error("分页查询日志异常!");
e.printStackTrace();
}
return null;
}
@Override
public ImmutableList<Log> getLogByType(final int typeId) {
final List<Log> logList = Lists.newLinkedList();
try {
final boolean listResult = DB.select(LOG_LIST_ALL, new ParamReadStH() {
@Override
public void handleRead(ResultSet rs) throws SQLException {
while (rs.next()) {
logList.add(handleResult(new Log(), rs));
}
}
@Override
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, typeId);
}
});
if (listResult) {
return ImmutableList.copyOf(logList);
}
} catch (SQLException e) {
log.error("根据类型id查询日志异常!");
e.printStackTrace();
}
return null;
}
private Log handleResult(final Log log, ResultSet rs) throws SQLException {
log.setId(rs.getInt("id"));
log.setUserName(rs.getString("userName"));
log.setIp(rs.getString("ip"));
log.setContent(rs.getString("content"));
log.setCreateDate(StringUtil.transforFromSqlToUtilDate(rs.getDate("createDate")));
log.setType(rs.getInt("type"));
log.setExt1(rs.getString("ext1"));
log.setExt2(rs.getString("ext2"));
return log;
}
}
|