hibernate 使用版本hibernate-release-4.3.8.Final.zip 官方最新版 2015年3月11日 在已经搭建好的 Struts2环境基础上 1:添加jar包 位于hibernate-release-4.3.8.Final\lib\required下 antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.8.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 添加mysql的jar包 mysql-connector-java-5.1.20-bin.jar jstl.jar standard.jar 2:src下新建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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.demo.bean.User"/>
</session-factory>
</hibernate-configuration>
3:新建User.java 包com.demo.bean 内容如下
package com.demo.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
private int id;
private String name;
private String password;
private String headimage;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHeadimage() {
return headimage;
}
public void setHeadimage(String headimage) {
this.headimage = headimage;
}
}
4:新建 IUserDao.Java 包 com.demo.dao 内容如下
package com.demo.dao;
import java.util.List;
import com.demo.bean.User;
public interface IUserDao {
void add(User user);
void delete(User user);
void deleteById(int id);
void update(User user);
User findById(int id);
List<User> findAllUser();
}
5:新建UserDaoImpl.java 包com.demo.dao.impl 内容如下
package com.demo.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.demo.bean.User;
import com.demo.dao.IUserDao;
import com.demo.utils.HibernateUtil;
public class UserDaoImpl implements IUserDao {
public void add(User user) {
Session s = HibernateUtil.getSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
}
public void delete(User user) {
Session s = HibernateUtil.getSession();
s.beginTransaction();
s.delete(user);
s.getTransaction().commit();
}
public void deleteById(int id) {
Session s = HibernateUtil.getSession();
s.beginTransaction();
String sql="delete from User where id=?";
Query createQuery = s.createQuery(sql).setInteger(0, id);
createQuery.executeUpdate();
s.getTransaction().commit();
}
@SuppressWarnings("unchecked")
public List<User> findAllUser() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
String sql="from User";
Query createQuery = s.createQuery(sql);
List<User> list=createQuery.list();
s.getTransaction().commit();
return list;
}
public User findById(int id) {
Session s = HibernateUtil.getSession();
s.beginTransaction();
User user=(User) s.get(User.class, id);
s.getTransaction().commit();
return user;
}
public void update(User user) {
Session s = HibernateUtil.getSession();
s.beginTransaction();
s.update(user);
s.getTransaction().commit();
}
}
6:新建IUserService.java 包com.demo.service 内容如下
package com.demo.service;
import java.util.List;
import com.demo.bean.User;
public interface IUserService {
void add(User user);
void delete(User user);
void deleteById(int id);
void update(User user);
User findById(int id);
List<User> findAllUser();
}
7:新建UserServiceImpl.java 包com.demo.service.impl 内容如下
package com.demo.service.impl;
import java.util.List;
import com.demo.bean.User;
import com.demo.dao.IUserDao;
import com.demo.dao.impl.UserDaoImpl;
import com.demo.service.IUserService;
public class UserServiceImpl implements IUserService {
IUserDao dao = new UserDaoImpl();
public void add(User user) {
dao.add(user);
}
public void delete(User user) {
dao.delete(user);
}
public void deleteById(int id) {
dao.deleteById(id);
}
public List<User> findAllUser() {
return dao.findAllUser();
}
public User findById(int id) {
return dao.findById(id);
}
public void update(User user) {
dao.update(user);
}
}
8:新建HibernateUtil.java 包com.demo.utils 内容如下
package com.demo.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
// 创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化
Configuration config = new Configuration();
config.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}
public static Session openSession() {
return sessionFactory.openSession();
}
public static Session getSession() {
return sessionFactory.getCurrentSession();
}
}
9:修改HelloWorldAction.java 内容如下
package com.demo.action;
import com.demo.service.IUserService;
import com.demo.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 6717047345781138351L;
IUserService userService = new UserServiceImpl();
public String execute() {
ActionContext.getContext().put("user", userService.findAllUser());
return SUCCESS;
}
}
10:修改hellowrold.jsp 内容如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'helloworld.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table>
<tr>
<td>id</td>
<td>name</td>
<td>password</td>
<td>headimage</td>
</tr>
<c:forEach var="user" items="${user}">
<tr>
<td>${user.id }</td>
<td>${user.name }</td>
<td>${user.password }</td>
<td>${user.headimage}</td>
</tr>
</c:forEach>
</table>
<br>
</body>
</html>
11:访问http://localhost:9999/Struts2WebDemo/helloworld 12:目录结构如下
-── Struts2WebDemo
├── src
│ ├── com
│ │ └── demo
│ │ ├── action
│ │ │ ├── HelloWorldAction.java
│ │ │ └── UserAction.java
│ │ ├── bean
│ │ │ └── User.java
│ │ ├── dao
│ │ │ ├── impl
│ │ │ │ └── UserDaoImpl.java
│ │ │ └── IUserDao.java
│ │ ├── service
│ │ │ ├── impl
│ │ │ │ └── UserServiceImpl.java
│ │ │ └── IUserService.java
│ │ └── utils
│ │ └── HibernateUtil.java
│ ├── hibernate.cfg.xml
│ └── struts.xml
└── WebRoot
├── helloworld.jsp
├── index.jsp
├── META-INF
│ └── MANIFEST.MF
└── WEB-INF
├── classes
│ ├── com
│ │ └── demo
│ │ ├── action
│ │ │ ├── HelloWorldAction.class
│ │ │ └── UserAction.class
│ │ ├── bean
│ │ │ └── User.class
│ │ ├── dao
│ │ │ ├── impl
│ │ │ │ └── UserDaoImpl.class
│ │ │ └── IUserDao.class
│ │ ├── service
│ │ │ ├── impl
│ │ │ │ └── UserServiceImpl.class
│ │ │ └── IUserService.class
│ │ └── utils
│ │ └── HibernateUtil.class
│ ├── hibernate.cfg.xml
│ └── struts.xml
├── lib
│ ├── antlr-2.7.7.jar
│ ├── asm-5.0.2.jar
│ ├── asm-commons-5.0.2.jar
│ ├── asm-tree-5.0.2.jar
│ ├── commons-fileupload-1.3.1.jar
│ ├── commons-io-2.2.jar
│ ├── commons-lang3-3.2.jar
│ ├── dom4j-1.6.1.jar
│ ├── freemarker-2.3.19.jar
│ ├── hibernate-commons-annotations-4.0.5.Final.jar
│ ├── hibernate-core-4.3.8.Final.jar
│ ├── hibernate-jpa-2.1-api-1.0.0.Final.jar
│ ├── jandex-1.1.0.Final.jar
│ ├── javassist-3.11.0.GA.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.jar
│ ├── log4j-1.2.17.jar
│ ├── mysql-connector-java-5.1.20-bin.jar
│ ├── ognl-3.0.6.jar
│ ├── standard.jar
│ ├── struts2-core-2.3.20.jar
│ └── xwork-core-2.3.20.jar
└── web.xml