1、集成环境(spring4、hibernate5、struts2)
2、引入包,maven管理,pom如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wyj</groupId>
<artifactId>ssh</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssh Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.34</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.34</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
</dependencies>
<build>
<finalName>ssh</finalName>
</build>
</project>
3、项目结构
建一个数据库
4、entity
package ssh.entity;
public class User {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
5、dao
package ssh.dao;
import java.util.List;
import ssh.entity.User;
public interface IndexDao {
public List<User> getAllUser();
}
package ssh.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ssh.entity.User;
public class IndexDaoImpl implements IndexDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<User> getAllUser() {
/*Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory(); */
/*ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
sessionFactory =(SessionFactory)context.getBean("sessionFactory");*/
Session session = sessionFactory.openSession();
Query query = session.createQuery("from User");//这是实体,不是表名
List<User> list = query.getResultList();
/*for(User u:list){
System.out.println(u);
}*/
session.close();
sessionFactory.close();
return list;
}
}
6、service
package ssh.service;
import java.util.List;
import ssh.entity.User;
public interface IndexService {
public List<User> getAllUser();
}
package ssh.service;
import java.util.List;
import ssh.dao.IndexDao;
import ssh.dao.IndexDaoImpl;
import ssh.entity.User;
public class IndexServiceImpl implements IndexService {
private IndexDao id;
public void setId(IndexDao id) {
this.id = id;
}
public List<User> getAllUser() {
List<User> userList = id.getAllUser();
return userList;
}
}
7、action
package ssh.action;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.entity.User;
import ssh.service.IndexService;
import ssh.service.IndexServiceImpl;
public class IndexAction extends ActionSupport{
private IndexService is;
public void setIs(IndexService is) {
this.is = is;
}
public String execute() {
/*ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
is =(IndexService)context.getBean("myIndexService");*/
List<User> myUserList = is.getAllUser();
// System.out.println("结果集:"+myUserList.size());
ActionContext ac = ActionContext.getContext();
ac.put("myUserList", myUserList);
return SUCCESS;
}
}
8、User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-6-9 20:33:49 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="ssh.entity.User" table="user">
<id name="username" type="java.lang.String">
<column name="username" />
<generator class="assigned" />
</id>
<property name="password" type="java.lang.String">
<column name="password" />
</property>
</class>
</hibernate-mapping>
9、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.password">填写自己的密码</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">填写自己的登录名</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
10、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<property name="is" ref="myIndexService"/>
</bean>
<!-- myIndexService = new ssh.service.IndexServiceImpl() -->
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"/>
</bean>
<bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<!-- 把sessionFactory 注入给IndexDao -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 添加sessionFactory bane ,注意,该类是Spring提供的 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
</beans>
11、struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="struts2" extends="struts-default">
<action name="Index" class="myIndexAction">//注意这里是填applicationContext里的值
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
12、web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- spring的监听器配置开始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
13、index.jsp
<html>
<body>
<h2>Hello World!</h2>
<form action="Index.action" method="put">
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
14、hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<td>用户名</td>
<td>密码</td>
</tr>
<s:iterator value="#myUserList" status="bcs">
<tr>
<td><s:property value="username"></s:property></td>
<td><s:property value="password"></s:property></td>
</tr>
</s:iterator>
</table>
</body>
</html>