//接口层:包括dao接口和业务逻辑接口 package com.genericdao.intf; import java.util.List; /** * 泛型接口,封装了通用的操作数据库的方法,被 dao 基类实现 * @author lin * * @param <T> */ public interface BaseDao<T> { /** * 增加 * @param t */ public void create(T t); /** * 根据id查询 * @param id * @return */ public T read(Integer id); /** * 更新 * @param t */ public void update(T t); /** * 删除 * @param t */ public void delete(T t); /** * 根据id删除 * @param id */ public void delete(Integer id); /** * 查询全部 * @return */ public List<T> listAll(); /** * 条件查询 * @param hql * @param parameters * @return */ public List<T> listAll(String hql,Object... parameters); } //专门为 User 类写一个接口 package com.genericdao.intf; import com.genericdao.pojo.User; public interface UserDao extends BaseDao<User> { /** * 该实体 dao 所特有方法,根据实际情况添加,在实际情况中user需要更多新的操作database的方法时可以修改这个接口 */ public User findByName(String name); } //业务接口 package com.genericdao.intf; import java.util.List; /** * 业务接口,被业务层基类实现 * @author lin * * @param <T> */ public interface BaseService<T> { /** * 增加 * @param t */ public void add(T t); /** * 根据id删除一个实体 * @param id */ public void delete(Integer id); /** * 更新 * @param t */ public void update(T t); /** * 根据id 查询并返回一个实体 * @param id * @return */ public T get(Integer id); /** * 获取全部 * @return */ public List<T> getAll(); /** * 条件查询 * @param hqlExpression hql语句 * @param parameters 查询参数 * @return 符合查询条件的实体集合 */ public List<T> getAll(String hqlExpression,Object... parameters); } //专门为 User 类的业务组件写一个接口,该接口继承自 BaseService 接口 package com.genericdao.intf; import com.genericdao.pojo.User; /** * * @author lin * */ public interface UserService extends BaseService<User> { /** * 添加user业务组件的特有方法 */ public User findByName(String name); } //dao层,实现dao接口 package com.genericdao.dao; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.genericdao.intf.BaseDao; /** * dao 基类,实现通用 dao 接口,被实体 dao 类继承 * @author lin * * @param <T> */ public abstract class BaseDaoImpl<T> implements BaseDao<T> { /** * 用于确定具体实体 dao 的泛型实际类型 */ private Class<? extends T> type; /** *由 Spring 注入sessionfactory */ private SessionFactory sessionFactory; public BaseDaoImpl() { //获取真实泛型数据类型 Type t = getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) t; type = (Class) pt.getActualTypeArguments()[0]; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public void create(T t) { if(t != null) { Session session = sessionFactory.getCurrentSession(); session.save(t); } } @Override public T read(Integer id) { if(id != null) { Session session = sessionFactory.getCurrentSession(); System.out.println("----------------------type:"+type); return (T) session.get(type, id); } return null; } @Override public void update(T t) { if(t != null) { Session session = sessionFactory.getCurrentSession(); session.update(t); } } @Override public void delete(T t) { if(t != null) { Session session = sessionFactory.getCurrentSession(); session.delete(t); } } @Override public void delete(Integer id) { if(id != null) { this.delete(this.read(id)); } } @Override public List<T> listAll() { Session session = sessionFactory.getCurrentSession(); Criteria criteria = session.createCriteria(type); List<T> list = criteria.list(); return list; } @Override public List<T> listAll(String hql, Object... parameters) { if(hql != null && !hql.equals("") && parameters.length != 0) { Session session = sessionFactory.getCurrentSession(); Query query = session.createQuery(hql); for(int i = 0;i<parameters.length;i++) { query.setParameter(i, parameters[i]); } return query.list(); } return null; } } //user dao package com.genericdao.dao; import com.genericdao.intf.UserDao; import com.genericdao.pojo.User; /** * 具体实体类的dao * @author lin * */ public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao { @Override public User findByName(String name) { //Session session = this.getSessionFactory().getCurrentSession(); return null; } } //业务逻辑层,实现service接口 package com.genericdao.service; import java.util.List; import com.genericdao.intf.BaseDao; import com.genericdao.intf.BaseService; /** * 业务层基类,被具体业务组件继承 * @author lin * * @param <T> */ public abstract class BaseServiceImpl<T> implements BaseService<T> { /** * 注入dao */ private BaseDao<T> baseDao; public BaseServiceImpl(){ } public BaseServiceImpl(BaseDao<T> baseDao) { this.baseDao = baseDao; } public BaseDao<T> getBaseDao() { return baseDao; } public void setBaseDao(BaseDao<T> baseDao) { this.baseDao = baseDao; } @Override public void add(T t) { if(t != null) { baseDao.create(t); } } @Override public void delete(Integer id) { if(id != null) { baseDao.delete(id); } } @Override public void update(T t) { if(t != null) { baseDao.update(t); } } @Override public T get(Integer id) { if(id != null) { return baseDao.read(id); } return null; } @Override public List<T> getAll() { return baseDao.listAll(); } @Override public List<T> getAll(String hqlExpression, Object... parameters) { return baseDao.listAll(hqlExpression, parameters); } } //user 的业务逻辑组件 package com.genericdao.service; import com.genericdao.intf.BaseDao; import com.genericdao.intf.UserDao; import com.genericdao.intf.UserService; import com.genericdao.pojo.User; /** * * @author lin * */ public class UserServiceImpl extends BaseServiceImpl<User> implements UserService { private UserDao userDao; public UserServiceImpl(){ } public UserServiceImpl(BaseDao<User> baseDao) { super(baseDao); this.userDao = (UserDao) baseDao; } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public User findByName(String name) { userDao.findByName("JeanseLam"); return null; } }
另外,spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--JDBC DataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/hibernate" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <!-- SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- - <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> --> <!-- 通過classpath的方式指向了映射文件所在的目錄,把domain包及子包下所有的映射文件全部加載了 --> <property name="mappingDirectoryLocations"> <value>classpath:com/genericdao/pojo</value> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- hibernate 在整合了 spring 后,hibernate 的事务将交由 spring 管理 --> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 事务传播行为 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" /> <tx:method name="delete*" /> <tx:method name="update*" /> <tx:method name="get*" /> <tx:method name="*" rollback-for="Exception" propagation="REQUIRED"></tx:method> </tx:attributes> </tx:advice> <!-- aop 配置 --> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.genericdao.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config> <!-- dao --> <!-- base dao --> <bean name="baseDao" class="com.genericdao.dao.BaseDaoImpl" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- user dao --> <bean name="userDaoImpl" class="com.genericdao.dao.UserDaoImpl" parent="baseDao"/> <!-- service --> <!-- base service --> <bean name="baseServiceImpl" class="com.genericdao.service.BaseServiceImpl" abstract="true"> <!-- <constructor-arg ref="baseDao"></constructor-arg> --> </bean> <!-- user service --> <bean name="userServiceImpl" class="com.genericdao.service.UserServiceImpl" parent="baseServiceImpl"> <constructor-arg ref="userDaoImpl"></constructor-arg> </bean> <!-- action --> <bean name="testAction" class="test.TestAction"> <property name="userService" ref="userServiceImpl"></property> <property name="userDao" ref="userDaoImpl"></property> </bean> <!-- pojo --> <bean name="user" class="com.genericdao.pojo.User" /> </beans>
最后放出项目的目录结构截图,我这里使用maven构建项目。