lcwben 2017-10-25
@Entity @Table(name="emp") public class Emp { @Id @GeneratedValue private Integer empno; private String ename; private String job; private Integer mgr; @Temporal(TemporalType.DATE) private Date hiredate; private double sal; private double comm; private Integer deptno; get set toString 的方法就不写上了 }
基类
public interface IBaseDao<T,PK> { T get(PK id); void save(T entity); void update(T entity); void delect(PK id); }
实现基类
@Repository public class BaseDaoImpl<T,PK extends Serializable> implements IBaseDao<T,PK>{ @Autowired private SessionFactory sessionFactory; public Class clz; public BaseDaoImpl() { Class cla = getClass(); Type type = cla.getGenericSuperclass(); if(type instanceof ParameterizedType){ Type[] types = ((ParameterizedType) type).getActualTypeArguments(); clz = (Class) types[0]; } } public Session getSession() { return sessionFactory.getCurrentSession(); } @Override public T get(PK id) { return (T) getSession().get(clz, id); } @Override public void save(T entity) { getSession().save(entity); } @Override public void update(T entity) { getSession().update(entity); } @Override public void delect(PK id) { T t = get(id); getSession().delete(t); } }
public interface IEmpDao extends IBaseDao<Emp, Integer>{ }
@Repository public class EmpDaoImpl extends BaseDaoImpl<Emp, Integer> implements IEmpDao { }
public interface IEmpService { void save(Emp emp); }
@Service public class EmpServiceImpl implements IEmpService{ @Autowired private IEmpDao dao; @Override public void save(Emp emp) { dao.save(emp); } }
public class Test1 { ApplicationContext app =null; @Before public void before(){ app = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void save(){ IEmpService be = app.getBean("empServiceImpl",IEmpService.class); Emp emp = new Emp(); emp.setEname("楼教主"); be.save(emp); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启全局扫描 --> <context:component-scan base-package="com.zhidi"/> <!-- 开启外部文件配置 --> <context:property-placeholder location="jdbc.properties"/> <!-- 配置数据库连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 初始化连接数 --> <property name="initialPoolSize" value="1"/> <!-- 最小连接数 --> <property name="minPoolSize" value="3"/> <!-- 最大连接时间 --> <property name="maxConnectionAge" value="28800"/> <property name="maxPoolSize" value="5"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <!-- 数据库方言的设置 --> <prop key="hibernate.dialect">${dialect}</prop> <!--设置session上下文这里不能设置thread,设置thread就需要手动管理并不能让Spring管理,要么不指定要么指定下面的类 --> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <!-- 显示SQl和格式化SQl --> <prop key="hibernate.show_sql">${showSql}</prop> </props> </property> <property name="packagesToScan" value="com.zhidi.entity"/> </bean> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delect*" propagation="REQUIRED"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 将通知运用到Aop的切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.zhidi.service.impl..*.*(..))"/> </aop:config> </beans>