struts2与heibernate的整合
一个简单的struts2与heibernate的入门整合案例
项目结构图
1.pom.xml
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加struts2依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.18</version> </dependency> <!-- 添加Hibernate依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- 添加Log4J依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.6.4</version> </dependency> <!-- 添加javassist --> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.0.GA</version> </dependency> <!-- mysql数据库的驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- 配置hibernate的c3p0数据库连接池 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>3.6.10.Final</version> </dependency> <!--servlet配置--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
2.hibernate.cmd.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> <!--SessionFactory用来创建Session对象,Session对象时操作数据库的核心--> <!--SessionFactory必要部分:1.连接数据库信息;2.hibernate的可选配置;3.映射文件配置--> <session-factory> <!-- 1.连接数据库的基本信息 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mybatis</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 2.hibernate的可选配置 --> <!-- hibernate里的配置,数据库使用的方言,开发过程中要在控制台显示,sql --> <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>--> <property name="hibernate.show_sql">true</property> <!-- <property name="hibernate.format_sql">true</property>--> <property name="hibernate.hbm2ddl.auto">update</property> <!--绑定线程和session,实现一个session只有一个线程。--> <property name="hibernate.current_session_context_class">thread</property> <!-- c3p0的配置 --> <!-- hibernate.connection.provider_class使用c3p0数据库连接池的一个配置 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--3.映射文件配置--> <!-- ORM映射关系 ,导入相应模型的绝对路径--> <mapping resource="user.hbm.xml"/> </session-factory> </hibernate-configuration>
3.struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--模块开发时引入其它模块--> <include file="struts/struts_user.xml"/> </struts>
4.struts_user.xm
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!--开启动态访问action--> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="demo01" extends="struts-default" namespace="/"> <!--通配符配置的方式--> <action name="user_*" class="com.web.action.UserAction" method="{1}"> <result name="users">/jsp/userlist.jsp</result> <allowed-methods>findAll</allowed-methods> </action> </package> </struts>
5.user.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.bean"> <class name="User" table="sturts_user"> <id name="uid" column="uid"> <generator class="native"></generator> </id> <property name="uname" column="uname"></property> <property name="pwd" column="pwd"></property> </class> </hibernate-mapping>
6.index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <body> <H3>通配符访问</H3> <a href="/user_findAll">查询所有用户</a></br> </body> </html>
7.UserMapperImpl.java
package com.dao.impl; import com.bean.User; import com.dao.UserMapper; import com.utils.HibernateUtils; import org.hibernate.Transaction; import org.hibernate.classic.Session; import java.util.List; public class UserMapperImpl implements UserMapper { public List<User> findAll(){ Session session = HibernateUtils.getCurrnetSession(); Transaction tx = session.beginTransaction(); List<User> list = session.createQuery("from User").list(); tx.commit(); return list; } }
8.UserAction.java
import com.bean.User; import com.opensymphony.xwork2.ActionSupport; import com.service.UserService; import com.service.impl.UserServiceImpl; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.List; public class UserAction extends ActionSupport { private UserService userService=new UserServiceImpl(); public String findAll(){ List<User> userList = userService.findAll(); System.out.println("UserAction:"+userList); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); session.setAttribute("userList",userList); session.setAttribute("test","testceshi"); return "users"; } }
9.userlist.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030" isELIgnored="false"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <title>Title</title> </head> <body> <h3>用户列表</h3> <table border="1px"> <tr> <td>id</td> <td>名称</td> <td>密码</td> </tr> <c:forEach items="${userList}" var="user"> <tr> <td>${user.uid}</td> <td>${user.uname}</td> <td>${user.pwd}</td> </tr> </c:forEach> </table> <h3>${test}</h3> </body> </html>