jar包
配置文件
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ssh1</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>
<!-- 一定要放在struts2核心过滤器之前进行配置 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<!-- 核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
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>
<package name="crm" namespace="/" extends="struts-default">
<action name="customer_*" class="customerAction" method="{1}" >
<result name=""></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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 必须配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置C3P0的连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 映射配置文件 -->
<mapping resource="com/huida/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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.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">
<bean id="customerService" class="com.huida.service.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<!-- 1.struts.xml 对应的Action class 填充是spring bean actionId
2.一定要给我们Action配置多例
-->
<bean id="customerAction" class="com.huida.web.CustomerAction" scope="prototype">
<!-- 必须手动注入service属性 -->
<property name="customerService" ref="customerService"></property>
</bean>
<bean id="customerDao" class="com.huida.dao.CustomerDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
<!-- <property name="dataSource" ref="dataSource"></property>
配置相关属性
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
引入配置文件
<property name="mappingResources">
<list>
<value>com/huida/domain/Customer.hbm.xml</value>
</list>
</property> -->
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理的开启 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置c3p0
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcurl" value="jdbc:mysql:///ssh"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> -->
</beans>
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
web层
CustomerAction
package com.huida.web;
import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.huida.domain.Customer;
import com.huida.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
//手动实例化对象
private Customer customer=new Customer();
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
/**
* 保存客户的方法
*/
public String save(){
System.out.println("Action中执行了save方法");
//传统的web工厂方法
/*WebApplicationContext webApplicationContext
= WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
CustomerService customerService=(CustomerService)webApplicationContext.getBean("customerService");*/
customerService.save(customer);
return NONE;
}
public String load(){
Customer c=customerService.load(1l);
System.out.println(c.getCust_name());
return NONE;
}
@Override
public Customer getModel() {
// TODO Auto-generated method stub
return customer;
}
}
service层
package com.huida.service;
import org.springframework.transaction.annotation.Transactional;
import com.huida.dao.CustomerDao;
import com.huida.domain.Customer;
@Transactional
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public void save(Customer customer){
System.out.println("Service层中的save方法呗执行");
customerDao.save(customer);
}
@Override
public void update(Customer customer) {
customerDao.update(customer);
}
@Override
public Customer load(Long id) {
Customer load = customerDao.load(id);
return load;
}
}
dao层
package com.huida.dao;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.huida.domain.Customer;
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
@Override
public void save(Customer c) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(c);
}
@Override
public void update(Customer c) {
// TODO Auto-generated method stub
this.getHibernateTemplate().update(c);
}
public void getById(Long id){
this.getHibernateTemplate().get(Customer.class, 1l);
//hql
//this.getHibernateTemplate().find("from Customer", 1l);
}
public void getByCri(Long id){
DetachedCriteria criteria=DetachedCriteria.forClass(Customer.class) ;
this.getHibernateTemplate().findByCriteria(criteria);
}
public Customer load(Long id){
return this.getHibernateTemplate().load(Customer.class, id);
}
}
domain层前边已经写过了,这里就省略了