package core.dao;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import com.forestry.model.sys.SysUser;
import core.extjs.ExtJSBaseParameter;
import core.support.BaseParameter;
import core.support.QueryResult;
/**
* @author 郑为中
*/
public class BaseDao<E> implements Dao<E> {
protected final Logger log = Logger.getLogger(BaseDao.class);
private static Map<String, Method> MAP_METHOD = new HashMap<String, Method>();
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Resource(name = "sessionFactory")
public void setSF(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
protected Class<E> entityClass;
public BaseDao(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void persist(E entity) {
getSession().save(entity);
}
/**
* 获取属性类型(type),属性名(name),属性值(value)的map组成的list
* */
private ArrayList getFiledsInfo(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
ArrayList list = new ArrayList();
Map infoMap=null;
for(int i=0;i<fields.length;i++){
infoMap = new HashMap();
infoMap.put("type", fields[i].getType().toString());
infoMap.put("name", fields[i].getName());
infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
list.add(infoMap);
}
return list;
}
/**
* 根据属性名获取属性值
* */
private Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
log.error(e.getMessage(),e);
return null;
}
}
/**
* 获取属性名数组
* */
private String[] getFiledName(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
for(int i=0;i<fields.length;i++){
System.out.println(fields[i].getType());
fieldNames[i]=fields[i].getName();
}
return fieldNames;
}
public static java.sql.Date StringToDate(String sDate) {
/**
*str转date方法
*/
String str = sDate;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = null;
try {
d = format.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
java.sql.Date date = new java.sql.Date(d.getTime());
return date;
}
public void updateQmm(Object entity){
String[] conditionName= {"id"};
Long[] conditionValue= new Long[1];
ArrayList<String> arrPropertyName=new ArrayList();
ArrayList<String> arrPropertyValue=new ArrayList();
ArrayList<String> arrPropertyType=new ArrayList();
ArrayList<Map> fieldsInfo=getFiledsInfo(entity);
for(int i=0;i<fieldsInfo.size();i++) {
Map m=(Map)fieldsInfo.get(i);
String type=(String)m.get("type");
String name=(String)m.get("name");
if(!name.equals("serialVersionUID")) {
if(m.get("value")!=null) {
String value=String.valueOf(m.get("value"));
if(((String)m.get("name")).equals("id")){
conditionValue[0]=Long.valueOf(value);
}else {
arrPropertyName.add(name);
arrPropertyValue.add(value);
arrPropertyType.add(type);
}
}
}
}
String[] propertyName= new String[arrPropertyName.size()];
Object[] propertyValue= new Object[arrPropertyValue.size()];
for(int i=0;i<arrPropertyName.size();i++) {
String name=(String)arrPropertyName.get(i);
String type=(String)arrPropertyType.get(i);
String value=(String)arrPropertyValue.get(i);
propertyName[i]=name;
if(type.equals("class java.lang.Long")){
propertyValue[i]=Long.valueOf(value);
}else
if(type.equals("class java.lang.String")){
propertyValue[i]=String.valueOf(value);
}else
if(type.equals("class java.sql.Date")){
propertyValue[i]=StringToDate((String)value);
}else
if(type.equals("class java.util.Date")){
propertyValue[i]=StringToDate((String)value);
}
}
updateByProperties(conditionName,conditionValue,propertyName,propertyValue);
}
public boolean deleteByPK(Serializable... id) {
boolean result = false;
if (id != null && id.length > 0) {
for (int i = 0; i < id.length; i++) {
E entity = get(id[i]);
if (entity != null) {
getSession().delete(entity);
result = true;
}
}
}
return result;
}
public void deleteByProperties(String[] propName, Object[] propValue) {
if (propName != null && propName.length > 0 && propValue != null && propValue.length > 0 && propValue.length == propName.length) {
StringBuffer sb = new StringBuffer("delete from " + entityClass.getName() + " o where 1=1 ");
appendQL(sb, propName, propValue);
Query query = getSession().createQuery(sb.toString());
setParameter(query, propName, propValue);
query.executeUpdate();
}
}
public void delete(E entity) {
getSession().delete(entity);
}
public void deleteByProperties(String propName, Object propValue) {
deleteByProperties(new String[] { propName }, new Object[] { propValue });
}
public void updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) {
if (propertyName != null && propertyName.length > 0 && propertyValue != null && propertyValue.length > 0 && propertyName.length == propertyValue.length && conditionValue != null && conditionValue.length > 0) {
StringBuffer sb = new StringBuffer();
sb.append("update " + entityClass.getName() + " o set ");
for (int i = 0; i < propertyName.length; i++) {
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(" where 1=1 ");
appendQL(sb, conditionName, conditionValue);
Query query = getSession().createQuery(sb.toString());
for (int i = 0; i < propertyName.length; i++) {
query.setParameter("p_" + propertyName[i], propertyValue[i]);
}
setParameter(query, conditionName, conditionValue);
query.executeUpdate();
} else {
throw new IllegalArgumentException("Method updateByProperties in BaseDao argument is illegal!");
}
}
public void upda
没有合适的资源?快使用搜索试试~ 我知道了~
java基于Spring的房屋租赁管理系统源码.zip
共2000个文件
gif:3143个
png:502个
java:268个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2023-03-21
14:15:28
上传
评论
收藏 47.4MB ZIP 举报
温馨提示
java基于Spring的房屋租赁管理系统源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
java基于Spring的房屋租赁管理系统源码.zip (2000个子文件)
BaseDao.class 18KB
AppHouseController.class 13KB
BaseService.class 10KB
SysUserController.class 10KB
ForestryController.class 9KB
AppTenantController.class 8KB
HouseController.class 8KB
TenantController.class 8KB
AppUserController.class 8KB
DepartmentController.class 8KB
ExtJSBaseController.class 7KB
HouseOwnerController.class 7KB
RoomToTenantController.class 6KB
House.class 6KB
AuthorityController.class 6KB
RoomController.class 6KB
AppHouse.class 6KB
HouseOwner.class 6KB
HouseTypeController.class 6KB
SystemInitListener.class 6KB
BeanUtils.class 6KB
QRCodeUtil.class 5KB
Authority.class 5KB
Tenant.class 5KB
SysUser.class 5KB
ForestryTypeController.class 5KB
RoomToTenantQuery.class 5KB
ConfigController.class 5KB
Room.class 5KB
TenantQuery.class 5KB
RoomToTenant.class 5KB
AppCheckTenant.class 5KB
SensorController.class 5KB
HouseOwnerRegController.class 5KB
AppTenantAndHouseAndRoom.class 5KB
HouseOwnerQuery.class 5KB
Forestry.class 5KB
Service.class 4KB
Dao.class 4KB
AppTenant.class 4KB
AppLoginController.class 4KB
QueryRoom.class 4KB
AppUser.class 4KB
AppAdmim.class 4KB
BaseParameter.class 4KB
ForestryType.class 4KB
AppRoomAndTenant.class 4KB
MonitorLog.class 4KB
Sensor.class 4KB
Attachment.class 4KB
WxFileUploadController.class 3KB
SensorLastData.class 3KB
SensorData.class 3KB
AppRoom.class 3KB
AppCheckHouse.class 3KB
TenantParameter.class 3KB
HtmlUtils.class 3KB
SensorDaoImpl.class 3KB
HouseType.class 3KB
Department.class 3KB
Config.class 3KB
RoleAuthority.class 3KB
HttpUtils.class 3KB
BufferedImageLuminanceSource.class 3KB
PageView.class 2KB
PageUtils.class 2KB
AppPeople.class 2KB
LoginFilter.class 2KB
AppFWType.class 2KB
ForestryServiceImpl.class 2KB
HouseOwnerServiceImpl.class 2KB
HouseParameter.class 2KB
AppHouseType.class 2KB
RoomServiceImpl.class 2KB
AppHouseTypeAndHouse.class 2KB
TenantServiceImpl.class 2KB
AppHouseAndRoom.class 2KB
AppDepartment.class 2KB
AppUserAndHouse.class 2KB
HouseOwnerDaoImpl.class 2KB
HouseServiceImpl.class 2KB
TenantDaoImpl.class 2KB
SpringBeanFactoryUtils.class 2KB
RoomDaoImpl.class 2KB
SysUserParameter.class 2KB
AuthorityServiceImpl.class 2KB
SensorServiceImpl.class 2KB
RoomToTenantParameter.class 2KB
HouseDaoImpl.class 2KB
ForestryParameter.class 2KB
AppHouseServiceImpl.class 2KB
HighPreciseComputor.class 2KB
RoleAuthorityController.class 2KB
SensorDataController.class 2KB
DESede.class 2KB
AttachmentController.class 2KB
MonitorLogController.class 2KB
RoomToTenantServiceImpl.class 2KB
RoomToTenantDaoImpl.class 2KB
AuthorityDaoImpl.class 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
oligaga
- 粉丝: 52
- 资源: 2万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功