Maven整合SSH框架
Maven整合SSH框架(maven3+spring4+Hibernation5+Struts2)
由于 struts 框架自身的 Bug 一直没有修复,以及 Spring MVC 已经足够强大,所以在最新的 spring 4 中已经移除了 spring-struts 模块。
在pom.xml声明依赖,利用该网站查找配置写法(http://mvnrepository.com/)
Hibernate5.2使用手册:
http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html
Spring5中文参考在线文档:https://lfvepclr.gitbooks.io/spring-framework-5-doc-cn/content/
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.16.Final</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9003</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
struts.xml:
<struts>
<constant name="struts.action.extension" value="action,"/>
<package name="shh" extends="struts-default" namespace="/">
<action name="customer_save" class="com.fly.action.CustomerAction" method="save"/>
<action name="test" class="com.fly.action.test"/>
</package>
</struts>
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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///sshzj"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
<!--注入连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置hibernate的相关属性-->
<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>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/sshzj</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/fly/pojo/Customer.hbm.xml</value>
</list>
</property>
</bean>
<bean class="com.fly.dao.impl.CustomerDaoImpl" id="customerDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="customerService" class="com.fly.service.impl.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"/>
</bean>
<bean id="customerAction" class="com.fly.action.CustomerAction">
<property name="customerService" ref="customerService"/>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<!-- 解决延迟加载问题的过滤器
使用load方法查询某一个对象的时候(不常用)
查询到某个对象以后,显示其关联对象
-->
<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>*.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>
<!--配置spring的核心监听器-->
<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>
CustomerDaoImpl:
public class CustomerDaoImpl implements CustomerDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void save(Customer customer) {
Session session = sessionFactory.getCurrentSession();
session.save(customer);
}
}
CustomerServiceImpl:
@Transactional
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
}
CustomerAction:
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Override
public Customer getModel() {
return customer;
}
private CustomerService customerService;
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public String save(){
System.out.println("sava action...");
System.out.println(customer.getCustName());
customerService.save(customer);
return NONE;
}
}
注解开发:
新增:
<!--struts2注解-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.24</version>
</dependency>
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: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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///sshzj"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
<!--注入连接池-->
<property name="dataSource" ref="dataSource"/>
<!--配置hibernate的相关属性-->
<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>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/sshzj</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
</props>
</property>
<property name="packagesToScan" value="com.fly.pojo"/>
</bean>
<context:component-scan base-package="com.fly.dao,com.fly.service,com.fly.action"/>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开启注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
CustomerDaoImpl:
@Repository(value = "customerDao")
public class CustomerDaoImpl implements CustomerDao {
@Resource
private SessionFactory sessionFactory;
@Override
public void save(Customer customer) {
Session session = sessionFactory.getCurrentSession();
session.save(customer);
}
}
CustomerServiceImpl:
@Service(value = "customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Resource(name = "customerDao")
private CustomerDao customerDao;
@Override
public void save(Customer customer) {
customerDao.save(customer);
}
}
CustomerAction:
@Controller(value = "customerAction")
@Scope("prototype") //原型模式
@ParentPackage(value = "struts-default")
@Namespace("/")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Override
public Customer getModel() {
return customer;
}
@Resource(name = "customerService")
private CustomerService customerService;
@Action(value = "customer_save")
public String save(){
System.out.println("sava action...");
System.out.println(customer.getCustName());
customerService.save(customer);
return NONE;
}
}
Customer:
@Entity
@Table(name = "cst_customer", schema = "sshzj")
public class Customer {
private long custId;
private String custName;
private String custSource;
private String custIndustry;
private String custLevel;
private String custPhone;
private String custMobile;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键由数据库自动生成
@Column(name = "cust_id")
public long getCustId() {
return custId;
}
web.xml:
<!-- 解决延迟加载问题的过滤器
使用load方法查询某一个对象的时候(不常用)
查询到某个对象以后,显示其关联对象
-->
<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>*.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>
<!--配置spring的核心监听器-->
<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>

浙公网安备 33010602011771号