这次使用struts2+hibernate+spring三个框架整合实现一个简单的注册功能,是基于idea这个智能的java IDE。
一,创建一个Project,如下图
勾选红框内的选项
向下翻,还有hibernate
一共四个地方,选好后OK,静静等待idea下载jar包
二,导入必须的包
- spring-web-x.x.x.RELEASE.jar
- org.springframework.web.jar
- mysql-connector-java-x.x.x-bin.jar
就这三个,其他的idea已经自动下载好了
三,准备工作也已完了,下面就是代码了
先将项目文件目录结构贴出来
一,dao
UserDAO.java
package per.summer.dao;
/**
* Created by summer on 17-4-6.
*/
public interface UserDAO {
public void addUser(Object entity);
}
UserDAOImpl.java
package per.summer.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
* Created by summer on 17-4-6.
*/
public class UserDAOImpl implements UserDAO {
SessionFactory sessionFactory;
//为spring的注入提供setter方法
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
//添加一个用户
@Override
public void addUser(Object entity) {
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
session.save(entity);
tx.commit();
session.close();
}
}
二,action
UserAction.java
package per.summer.action;
import com.opensymphony.xwork2.ActionSupport;
import per.summer.dao.UserDAO;
import per.summer.entity.UserEntity;
/**
* Created by summer on 17-4-6.
*/
public class UserAction extends ActionSupport{
private String username;
private String password;
private UserDAO userDAO;
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;
}
//为spring的注入提供setter方法
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public String execute() throws Exception {
UserEntity userEntity = new UserEntity(username, password);
userDAO.addUser(userEntity);
return SUCCESS;
}
}
三,entity
UserEntity.java
package per.summer.entity;
import javax.persistence.*;
/**
* Created by summer on 17-4-6.
*/
@Entity
@Table(name = "user_inf")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String password;
public UserEntity() {
}
public UserEntity(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
对于hibernate持久化实体,这里我使用的是注解,就没有UserEntity.hbm.xml文件
四,spring-config.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>per.summer.entity.UserEntity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/ssh</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">manager</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
<bean id="userDAO" class="per.summer.dao.UserDAOImpl"
p:sessionFactory-ref="sessionFactory"/>
<bean id="userAction" class="per.summer.action.UserAction"
p:userDAO-ref="userDAO"/>
</beans>
五,strusts.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" extends="struts-default">
<!--这里的class属性的userAction就不是具体的实现类了,而是在前面的spring-config.xml文件中定义的一个bean-->
<action name="login" class="userAction">
<result>/WEB-INF/jsp/success.jsp</result>
</action>
</package>
</struts>
六,web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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:spring-config.xml</param-value>
</context-param>
</web-app>
七,index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User: summer
Date: 17-4-6
Time: 下午6:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="username"/>
<s:textfield name="password" label="password"/>
<s:submit value="login"/>
</s:form>
</body>
</html>