Spring与hibernate对数据库操作的打包
Spring与hibernate对数据库操作的封装
步骤 1 创建实体类 2 创建基类接口和实现基类接口 3创建实体类的dao的接口 4 创建 service层的接口和实现类 和接口的实现类 5就是测试了吗
-
创建数据库表的实体类
-
@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);
}
}
-
创建实体类的dao 因为可能会定义自己特需的方法,我们可以将dao层也做一个接口,
-
dao的接口去继承基类的接口 在不需要定义特需方法的情况下,我们可以什么都不写。
public interface IEmpDao extends IBaseDao<Emp, Integer>{
}
-
dao接口的实现类,既要实现自己的接口 也要去继承基类的实现类,因为我们建基类的目的就是要用基类实现类的方法。
@Repository
public class EmpDaoImpl extends BaseDaoImpl<Emp, Integer> implements IEmpDao {
}
-
创建业务逻辑层的接口和实现类
-
接口 定义你所需要干的事给定个方法让实现类去实现
public interface IEmpService { void save(Emp emp); }
-
接口的实现类 定义所要Dao的接口为变量
@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配置 jar包就是hibernate和spring 所需jar包
<?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>