注:
注解必须提供除spring和hibernet的正常的包外还需要相应的三个jar包
*********************************webxml************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Demotext</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-过滤器->
<filter>
<filter-name>OSIV</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OSIV</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-监听器->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-配置监听的路径->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-核心过滤器->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
*******************Beans.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username"value="textdemo"></property>
<property name="password"value="textdemo"></property>
<property name="driverClassName"value="oracle.jdbc.OracleDriver"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 使用扫描(装配) -->
<!-- 扫描实体类 -->
<property name="packagesToScan"value="com.libo.po"/>
</bean>
<!-- 管理bean自动装配(扫描) -->
<context:component-scan base-package="com.libo"></context:component-scan>
<bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!-- 需要哪个sessionFactory使用到事务 -->
<property name="sessionFactory"ref="sessionFactory"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
*****************JAVA.SRC*********************
package com.libo.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.libo.dao.StudentDao;
import com.libo.po.Student;
@Repository("sDao")
public class StudentDaoImpl implements StudentDao{
@Resource
privateSessionFactory sessionFactory;
publicList<Student> getall() {
//Sessionsession=sessionFactory.openSession();
Sessionsession=sessionFactory.getCurrentSession();
returnsession.createQuery("from Student").list();
}
@Override
public voidsave(Student stu) {
Sessionsession=sessionFactory.getCurrentSession();
session.save(stu);
}
}
package com.libo.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
importorg.springframework.transaction.annotation.Transactional;
import com.libo.dao.StudentDao;
import com.libo.po.Student;
import com.libo.service.StudentService;
@Service("service")(必须和action的new的封装次一样)
@Transactional(提交事物的注解单词)
public class StudentServiceImpl implements StudentService{
@Resource(引用他的父类)
private StudentDao sDao;
public List<Student> getall() {
return sDao.getall();
}
@Override
public void save(Student stu) {
sDao.save(stu);
}
}
package com.libo.action;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.libo.po.Student;
import com.libo.service.StudentService;
import com.opensymphony.xwork2.ActionSupport;
@Action(value = "login", results = { @Result(location = "/index.jsp"), @Result(name = "saves", location = "login", type = "redirectAction") })
public class LoginAction extends ActionSupport{
@Resource
private StudentService service;
private List<Student> list;
private Student student;
@Override
public String execute() throws Exception {
list=service.getall();
return SUCCESS;
}
public String save(){
System.out.println(student);
service.save(student);
return "saves";
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
}