1、类
********************************************************************************
bean类:
public class Person implements Serializable {
private Long pid;
private String pname;
private String psex;
// set/get属性
}
dao接口:
public interface PersonDao {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
dao实现类:
@Repository("personDao")
public class PersonDaoImpl implements PersonDao {
@Resource(name="hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public void savePerson(Person person) {
this.hibernateTemplate.save(person);
}
public Person getPersonById(Serializable id) {
return (Person) this.hibernateTemplate.load(Person.class, id);
}
}
service接口:
public interface PersonService {
public void savePerson(Person person);
public Person getPersonById(Serializable id);
}
service实现类:
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Resource(name="personDao")
private PersonDao personDao;
// 注解解析器解析到该注解,就会为该类或方法创建代理对象
@Transactional(readOnly=false)
public void savePerson(Person person) {
this.personDao.savePerson(person);
}
public Person getPersonById(Serializable id) {
return this.personDao.getPersonById(id);
}
}
action类:
@Controller("personAction")
@Scope("prototype")
public class PersonAction extends ActionSupport {
@Resource(name="personService")
private PersonService personService;
public String savePerson(){
Person person = new Person();
person.setPname("小李1234");
person.setPsex("男");
// TODO 有问题 但是页面跳转无问题 No suitable driver found for jdbc:mysql://localhost:3306/test
this.personService.savePerson(person);
System.out.println("aaa1234");
return "index";
}
public String showPerson(){
Person person = this.personService.getPersonById(1L);
ServletActionContext.getRequest().setAttribute("person", person);
return "index";
}
}
工具类:
public class SpringInit {
public static ApplicationContext context;
static{
context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
}
显示层index.jsp:
<body>
${requestScope.person.pname}
</body>
********************************************************************************
2、配置文件
********************************************************************************
-----------------------------------------------------------------------------
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 自动加载配置文件 -->
<constant name="struts.devMode" value="true"></constant>
<include file="struts2/struts-person.xml"></include>
</struts>
-----------------------------------------------------------------------------
struts-person.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="person" namespace="/" extends="struts-default">
<!-- 注: class的值与spring配置中的action类的id值对应 -->
<action name="personAction_*" method="{1}" class="personAction">
<result name="index">index.jsp</result>
</action>
</package>
</struts>
-----------------------------------------------------------------------------
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 一个session-factory只能连接一个数据库 -->
<session-factory>
<!-- 数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">123456</property>
<!-- url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/s2shDemo
</property>
<!-- 方言 告诉hibernate链接的mysql数据库 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<!-- 作用:根据持久化类和映射文件生成表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示hibernate内部生成的sql语句 -->
<property name="show_sql">true</property>
<!-- hibernate中使用currentSession需要配置该内容 -->
<!-- <property name="current_session_context_class">thread</property>-->
<!-- 加载映射文件 -->
<mapping resource="cn/itcast/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
-----------------------------------------------------------------------------
Person.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.domain.Person">
<id name="pid" length="10">
<generator class="increment"></generator>
</id>
<property name="pname" length="20"></property>
<property name="psex" length="25"></property>
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------
applicationContext.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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="applicationContext-db.xml"/>
<import resource="applicationContext-annotation.xml"/>
</beans>
-----------------------------------------------------------------------------
applicationContext-db.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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- spring结合hibernate配置sessionFactory的方式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------
applicationContext-annotation.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:context="http://www.springframework.org/schema/context"
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 类扫描的注解解析器 component指的就是一个类 base-package在该包及子包中进行扫描 -->
<context:component-scan base-package="cn.itcast"></context:component-scan>
<!-- 提供事务的注解解析器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
-----------------------------------------------------------------------------
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 整合spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- OpenSessionInViewFilter的过滤器 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
-----------------------------------------------------------------------------
********************************************************************************
3、测试类:
********************************************************************************
public class ConfigTest extends SpringInit{
@Test
public void test(){
context.getBean("sessionFactory");
// 测试各种bean
}
}
public class PersonTest extends SpringInit {
@Test
public void test(){
// 注: 启事务是在service层启动的,所以用personService来操作 目标类
// 打断点测试,必须为代理类$proxy4
PersonService personService = (PersonService) context.getBean("personService");
Person person = new Person();
person.setPname("小1");
person.setPsex("女");
personService.savePerson(person);
}
}
********************************************************************************