java之spring之整合ssh
Posted vincent-yuan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之spring之整合ssh相关的知识,希望对你有一定的参考价值。
这篇主要讲解spring + struts2 + hibernate :
目录结构如下:
t_role
t_user
1.新建 web项目 :spring_ssh
2.在 WebRoot/WEB-INF/lib 下 导入jar包
antlr-2.7.7.jar aopalliance.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar aspectjweaver.jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-lang3-3.1.jar commons-logging.jar dom4j-1.6.1.jar freemarker-2.3.19.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.10.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar jstl-1.2.jar mysql-connector-java-5.1.20-bin.jar ognl-3.0.5.jar spring-aop-4.1.6.RELEASE.jar spring-aspects-4.1.6.RELEASE.jar spring-beans-4.1.6.RELEASE.jar spring-context-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar spring-expression-4.1.6.RELEASE.jar spring-jdbc-4.1.6.RELEASE.jar spring-orm-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-web-4.1.6.RELEASE.jar struts2-core-2.3.4.jar struts2-spring-plugin-2.3.4.jar xwork-core-2.3.4.jar
3.编写vo类 :Role.java , User.java
Role.java
package cn.vincent.vo; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_role") public class Role implements Serializable @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; public int getId() return id; public void setId(int id) this.id = id; public String getName() return name; public void setName(String name) this.name = name;
User.java
package cn.vincent.vo; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_user") public class User implements Serializable @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; private int age; @ManyToOne @JoinColumn(name="roleId") private Role role; public int getId() return id; public void setId(int id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public int getAge() return age; public void setAge(int age) this.age = age; public Role getRole() return role; public void setRole(Role role) this.role = role;
4.编写 dao
UserDao.java
package cn.vincent.dao; import java.util.List; import cn.vincent.vo.User; public interface UserDao public List<User> findAll();
UserDaoImpl.java
package cn.vincent.dao.impl; import java.util.List; import org.hibernate.SessionFactory; import cn.vincent.dao.UserDao; import cn.vincent.vo.User; public class UserDaoImpl implements UserDao private SessionFactory sessionFactory; @Override public List<User> findAll() return sessionFactory.getCurrentSession() .createQuery("from User").list(); public void setSessionFactory(SessionFactory sessionFactory) this.sessionFactory = sessionFactory;
5.编写 service
UserService.java
package cn.vincent.service; import java.util.List; import cn.vincent.vo.User; public interface UserService public List<User> findAll();
UserServiceImpl.java
package cn.vincent.service.impl; import java.util.List; import cn.vincent.dao.UserDao; import cn.vincent.service.UserService; import cn.vincent.vo.User; public class UserServiceImpl implements UserService private UserDao userDao; @Override public List<User> findAll() return userDao.findAll(); public void setUserDao(UserDao userDao) this.userDao = userDao;
6.编写 action
package cn.vincent.action; import java.util.List; import com.opensymphony.xwork2.Action; import cn.vincent.service.UserService; import cn.vincent.vo.User; public class UserAction private List<User> list; private UserService userService; public String list() list = userService.findAll(); return Action.SUCCESS; public List<User> getList() return list; public void setList(List<User> list) this.list = list; public UserService getUserService() return userService; public void setUserService(UserService userService) this.userService = userService;
7.编写 spring 配置文件
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///test"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- sessionFactory对象由spring来创建 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 通用属性配置 --> <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> </props> </property> <!-- 配置映射文件 --> <property name="annotatedClasses"> <array> <value>cn.vincent.vo.User</value> <value>cn.vincent.vo.Role</value> </array> </property> </bean> <!-- 配置声明式事务 --> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 表示以save开头的方法都需要事务 propagation 表示事务的传播特性 REQUIRED 查看当前是否有事务,如果有,使用当前事务,如果没有开启新事务 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务aop --> <aop:config> <!--expression 指明事务在哪里起作用 第一个* 表示所有返回值 第二个* 表示所有类 第三个* 表示类中的所有方法 .. 表示所有参数 --> <aop:pointcut expression="execution(* cn.vincent.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> </beans>
applicationContext-asd.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="userDao" class="cn.vincent.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="userService" class="cn.vincent.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <bean id="userAction" class="cn.vincent.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> </beans>
8.编写 struts2 配置文件
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="default" namespace="/" extends="struts-default"> <action name="list" class="userAction" method="list"> <result>/list.jsp</result> </action> </package> </struts>
9.编写 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- spring的配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <!-- 监听器 启动spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>osiv</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>osiv</filter-name> <url-pattern>*.action</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> </web-app>
10.编写 jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> 用户管理</title> </head> <body> <table width="80%" align="center"> <tr> <td>编号</td> <td>姓名</td> <td>年龄</td> <td>角色</td> </tr> <c:forEach items="$list " var="bean"> <tr> <td>$bean.id </td> <td>$bean.name </td> <td> $bean.age </td> <td> $bean.role.name </td> </tr> </c:forEach> </table> </body> </html>
11. 运行web项目
首先部署项目
访问地址如下:
至于访问地址为什么是这个,可以查看 java之struts框架入门教程
github地址:https://github.com/Vincent-yuan/spring_ssh
以上是关于java之spring之整合ssh的主要内容,如果未能解决你的问题,请参考以下文章