/**
* 角色对象
*
* @author wanganqi
* @version v1.0
* @since 2013年7月30日上午10:32:55
*/
@SuppressWarnings("serial")
@Entity
public class Role implements Serializable
{
private Long id;
private String name;
private String description;
// ********************** Accessor Methods ********************** //
@Id
@GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
} |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://IP地址:3306/schema名</property>
<!-- <property name="hibernate.connection.characterEncoding">gbk</property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.c3p0.max_size">1</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">180</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.jdbc.fetch_size">50</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- <property name="hibernate.connection.autocommit">true</property> -->
<mapping class="com.wang.anqi.model.Role" />
</session-factory>
</hibernate-configuration> |
/**
* DAO层接口-角色
*
* @author wanganqi
* @version v1.0
* @since 2013年7月30日下午6:13:29
*/
public interface RoleDAO extends GenericDAO<Role, Long>
{
/**
* 按照角色名称查找其对应的用户列表
*
* @param roleNmae 角色名称
* @return 用户列表 List<User>
* @throws DatabaseException 异常
*/
List<User> findByRoleName(String roleName) throws DatabaseException;
} |
/**
* 各个DAO的通用模板接口。
* <p>
* 增删改查(create, read, update, delete)这几种基本数据访问操作放在这个接口中。
* <p>
* @author wanganqi
* @version v1.0
*
* @since 2012年12月1日下午2:49:50
* @param <T> 模板类型
* @param <ID> 主键
*/
public interface GenericDAO<T, ID extends Serializable>
{
/**
* 添加
*
* @param entity 实体类
* @return 实体类
*/
T saveOrUpdate(T entity);
/**
* 根据给定的id,以lock规定的锁模式,查找并返回对应的实体对象。
*
* @param id 主键
* @param lock 是否加锁
* @return T 查找到的对象
*/
T findById(ID id, boolean lock);
/**
* 按条件查找
*
* @param criterion 条件
* @return 实体对象列表
*/
List<T> findByCriteria(Criterion... criterion);
/**
* 返回T类型的所有对象。
*
* @return List<T> T对象的列表
*/
List<T> findAll();
/**
* 查询
*
* @param exampleInstance 条件
* @param excludeProperty 条件
* @return 实体对象列表
*/
List<T> findByExample(T exampleInstance, String... excludeProperty);
/**
* 将实体entity持久化。(添加或更新)
*
* @param entity 需持久化的实体
* @return 持久化对象
*/
T makePersistent(T entity);
/**
* 将持久化实体entity从数据库中删除。
*
* @param entity 需删除的实体。
*/
void makeTransient(T entity);
/**
* 根据ID删除实体
*
* @param id ID
*/
void delete(ID id);
/**
* 同步对象在内存与数据库中的状态。
*/
void flush();
/**
* 彻底清理当前session。
*/
void clear();
/**
* 统一抛出数据库操作异常
*
* @param e 异常
* @throws DatabaseException 数据库异常
*/
void throwDatabaseException(Exception e) throws DatabaseException;
/**
* 执行数据表结构更新
*
* @return 是否更新成功
* @throws DatabaseException 数据库异常
*/
boolean excuteUpdateShema() throws DatabaseException;
} |
/**
* DAO层接口实现-角色访问
*
* @author wanganqi
* @version v1.0
* @since 2013年7月30日下午6:18:41
*/
@Repository("RoleDAOImpl")
public class RoleDAOImpl extends GenericDAOImpl<Role, Long> implements RoleDAO
{
@SuppressWarnings("unchecked")
@Override
public List<User> findByRoleName(String roleName) throws DatabaseException
{
return getSession().createCriteria(User.class)
.add(Restrictions.eq("roleName", roleName)).list();
}
} |
/**
* You have to inject a current Hibernate <tt>Session</tt> to use a DAO.
* Otherwise, this generic implementation will use
* <tt>HibernateUtil.getSessionFactory()</tt> to obtain the curren
* <tt>Session</tt>.
*
* @author wanganqi
* @version v1.0
* @since 2012年8月1日下午2:46:08
* @param <T> 模板类
* @param <ID> 模板ID
*
*/
public abstract class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID>
{
private Class<T> m_persistentClass;
@Autowired
private SessionFactory m_sessionFactory;
@Autowired
private LocalSessionFactoryBean m_sessionFactoryBean;
/**
* 构造函数
*
*/
@SuppressWarnings("unchecked")
public GenericDAOImpl()
{
// 运用反射机制,获取当前具体的类的类型。
this.m_persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
/**
* 获取sessionFactory
*
* @return SessionFactory
*/
public SessionFactory getSessionFactory()
{
return m_sessionFactory;
}
protected Session getSession()
{
return m_sessionFactory.getCurrentSession();
}
/**
* 获取模板类
*
* @return 模板类
*/
public Class<T> getPersistentClass()
{
return m_persistentClass;
}
@SuppressWarnings("unchecked")
@Override
public T findById(ID id, boolean lock)
{
T entity;
if (lock)
{
entity = (T) getSession().get(getPersistentClass(), id, LockOptions.UPGRADE);
}
else
{
entity = (T) getSession().get(getPersistentClass(), id);
}
return entity;
}
@Override
public List<T> findAll()
{
List<T> result = findByCriteria();
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByExample(T exampleInstance, String... excludeProperty)
{
Criteria crit = getSession().createCriteria(getPersistentClass());
Example example = Example.create(exampleInstance);
for (String exclude : excludeProperty)
{
example.excludeProperty(exclude);
}
crit.add(example);
return crit.list();
}
@SuppressWarnings("unchecked")
@Override
public T makePersistent(T entity)
{
return (T) getSession().merge(entity);
}
@Override
public void makeTransient(T entity)
{
getSession().delete(entity);
}
@SuppressWarnings("unchecked")
@Override
public void delete(ID id)
{
T entity = (T) getSession().get(getPersistentClass(), id);
if (entity != null)
{
getSession().delete(entity);
}
}
@Override
public void flush()
{
getSession().flush();
}
@Override
public void clear()
{
getSession().clear();
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByCriteria(Criterion... criterion)
{
Criteria crit = getSession().createCriteria(getPersistentClass());
for (Criterion c : criterion)
{
crit.add(c);
}
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return crit.list();
}
@SuppressWarnings("unchecked")
protected T queryForObject(String hql, Object[] params)
{
Query query = getSession().createQuery(hql);
setQueryParams(query, params);
return (T) query.uniqueResult();
}
private void setQueryParams(Query query, Object[] params)
{
if (null == params)
{
return;
}
for (int i = 0; i < params.length; i++)
{
query.setParameter(i, params[i]);
}
}
@Override
public T saveOrUpdate(T entity)
{
getSession().saveOrUpdate(entity);
return entity;
}
@Override
public void throwDatabaseException(Exception e) throws DatabaseException
{
DatabaseException de = new DatabaseException(new ErrMsg("数据库操作异常", (e.getCause() != null ? e.getCause().getMessage()
: e.getMessage())));
throw de;
}
private FilenameFilter filter(final String extension)
{
return new FilenameFilter()
{
@Override
public boolean accept(File file, String path)
{
String filename = new File(path).getName();
return filename.indexOf(extension) != -1;
}
};
}
@Override
public boolean excuteUpdateShema() throws DatabaseException
{
try
{
org.hibernate.cfg.Configuration config = new Configuration();
config.configure("classpath:hibernate.cfg.xml");
String path = URLDecoder.decode(this.getClass().getClassLoader().getResource("").getPath(), "utf-8");
path += "resource/";
File hbmDir = new File(path);
if (hbmDir.exists())
{
List<File> fList = Arrays.asList(hbmDir.listFiles(filter(".hbm.xml")));
if (null != fList && fList.size() > 0)
{
for (File f : fList)
{
config.addResource("resource/" + f.getName());
}
}
}
new SchemaUpdate(config).execute(true, true);
}
catch (Exception e)
{
throwDatabaseException(e);
}
return true;
}
} |
/**
* 逻辑层BLL接口-角色
*
* @author wanganqi
* @version v1.0
* @since 2013年7月30日下午6:19:38
*/
public interface RoleBLL extends GenericBLL<Role, Long>
{
/**
* 列出指定角色下面的所有用户
*
* @param role 角色名称
* @return 用户列表 List<User>
* @throws DatabaseException DAO层异常
* @throws BusinessException 逻辑层异常
*/
List<User> listUsers(String roleName) throws DatabaseException,
BusinessException;
} |
/**
* 各个BLL的通用模板
*
* @author wanganqi
* @version v1.0
* @since 2012年8月8日上午9:33:41
* @param <T>
* @param <ID>
*/
public interface GenericBLL<T, ID extends Serializable>
{
List<T> findALL();
T findById(ID id);
T addT(T t);
T editT(T t);
boolean deleteT(T t);
boolean deleteById(ID id);
} |
/**
* 逻辑BLL层-角色处理
*
* @author wanganqi
* @version v1.0
* @since 2013年7月30日下午6:36:10
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
@Component("RoleBLLImpl")
public class RoleBLLImpl extends GenericBLLImpl<Role, Long> implements RoleBLL
{
@Autowired
@Qualifier("RoleDAOImpl")
private RoleDAO m_roleDAO; /**
* 注入
*/
@PostConstruct
public void postConstruct()
{
super.setGenericDAO(m_roleDAO);
} @Override
public List<User> listRoleUsers(Role role) throws DatabaseException, BusinessException
{
List<User> users = m_roleDAO.findByRoleId(role.getId());
return users;
}} |
/**
* 各个BLLImpl的通用模板
*
* @author wanganqi
* @version v1.0
* @since 2012年8月8日下午2:20:29
* @param <T>
* @param <ID>
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
public abstract class GenericBLLImpl<T, ID extends Serializable> implements
GenericBLL<T, ID>
{
private GenericDAO<T, ID> genericDAO;
public void setGenericDAO(GenericDAO<T, ID> genericDAO)
{
this.genericDAO = genericDAO;
}
public GenericBLLImpl(GenericDAO<T, ID> gd)
{
genericDAO = gd;
}
public GenericBLLImpl()
{
}
public List<T> findALL()
{
List<T> retList = genericDAO.findAll();
return retList;
}
public T findById(ID id)
{
T retT = genericDAO.findById(id, false);
return retT;
}
public T addT(T t)
{
T rt = genericDAO.makePersistent(t);
return rt;
}
public T saveOrUpdate(T entity)
{
T resultT = addT(entity);
return resultT;
}
public T editT(T t)
{
return addT(t);
}
public boolean deleteT(T t)
{
if (null == t)
{
return true;
}
genericDAO.makeTransient(t);
return true;
}
public boolean deleteById(ID id)
{
return deleteT(findById(id));
}
} |
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:configLocation="/WEB-INF/hibernate.cfg.xml">
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.wang.anqi.model" />
<context:component-scan base-package="com.wang.anqi.dao.impl" />
<!-- 发布bean -->
<bean id="roleBLLImpl" class="com.wang.anqi.dataBll.impl.RoleBLLImpl" />
<!-- 发布bean -->
</beans>
|
关于项目中的DAL数据接入层架构设计,布布扣,bubuko.com
原文:http://www.cnblogs.com/wgp13x/p/3900096.html