@
qping model层:
@
Entitypublic class Node {
private String name;
@
Id public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
dao层:
baseDaoImpl.java:
public class BaseDaoImpl {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@
Resource(name = "sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
NodeDaoImpl.java:
@
Repositorypublic class NodeDaoImpl extends BaseDaoImpl implements NodeDao {
@
Override public Serializable save(Node node){
Session session = getCurrentSession();
session.setFlushMode(FlushMode.COMMIT);
Serializable id = session.save(node);
//session.flush();
return id;
}
}
service层:
@
Servicepublic class NodeServiceImpl implements NodeService {
@
Override public Node add(Node node) {
int id = (Integer)nodeDao.save(node);
return node;
}
@
Resource public void setNodeDao(NodeDao nodeDao) {
this.nodeDao = nodeDao;
}
private NodeDao nodeDao;
}
controller层(spring mvc):
@
Controller@
RequestMapping("/admin")
public class AdminController {
@
RequestMapping public String index(){
return "admin/admin";
}
@
RequestMapping(value = "/node")
public String addNode(@RequestParam("name")String nodename,ModelMap model){
Node node = new Node();
node.setName(nodename);
nodeService.add(node);
model.addAttribute("msg","添加成功");
return "admin/admin";
}
@
Resource public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
private NodeService nodeService;
}
spring-hibernate.xml配置文件:
<?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:tx="
http://www.springframework.org/schema/tx"
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/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">
<!-- 数据库连接配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driveClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_name}" />
<property name="password" value="${jdbc_password}" />
<property name="initialSize" value="5" />
<property name="maxConnLifetimeMillis" value="60000" />
<property name="maxTotal" value="20" />
<property name="maxIdle" value="5" />
</bean>
<!-- Hibernate属性配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>model</value>
</list>
</property>
</bean>
<!-- 事务配置 -->
<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<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="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="is*" propagation="REQUIRED" read-only="true"/>
<tx:method name="follow*" propagation="REQUIRED" />
<!--<tx:method name="*" propagation="REQUIRED" />-->
</tx:attributes>
</tx:advice>
<!-- 需要引入aop的命名空间 -->
<aop:config proxy-target-class="true">
<!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
<aop:pointcut id="transactionPointcut" expression="execution(* service.Impl.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" />
</aop:config>
</beans>