https://wenku.baidu.com/view/d0dcfc017f1922791788e8a5.html
一, 通过IDE创建项目, 并创建相应的包结构
查看web.xml中的StrutsPrepareAndExecuteFilter包名是否正确.不正确的话去掉.ng包.
打开Project Structure and Fix the Problems
二 整合Hibernate到Spring中
右键数据库, 创建表
如果遇到如下问题:
解决方法:
手动创建一个sessionFactory
此时选项中便有了可选项
自动生成如下内容
导入需要的jdbc驱动包后修改配置如下:
com.minicuper.ssh.entity.UserEntity
jdbc:mysql://localhost:3306/ssh
com.mysql.jdbc.Driver
root
1234
thread
true
true
org.hibernate.dialect.MySQL5Dialect
create
生成的实体类中有一些错误
解决方法:为sessionFactory指定对应的DataSources即可
三 整合Struts到Spring中.
配置ContextLoaderListener, 使WebApp启动时便初始化Spring配置文件
ContextLoaderListener类在spring-web.jar包中.需要手动引入jar包.
web.xml
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-config.xml
创建的类,及修改的类 :
UserAction.java
package com.minicuper.ssh2.action;
import com.minicuper.ssh2.entity.User;
import com.minicuper.ssh2.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction extends ActionSupport implements ModelDriven {
public String result = "";
private User user = new User();
@Override
public User getModel() {
return user;
}
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String regist(){
System.out.println("UserAction regist()");
if(userService.regist(user)){
result = "注册成功";
return SUCCESS;
}else {
result = "注册失败";
return ERROR;
}
}
}
User.java
package com.minicuper.ssh2.entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Objects;
@Entity
public class User {
private int uid;
private String name;
private String pwd;
@Id
@Column(name = "uid")
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "pwd")
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return uid == user.uid &&
Objects.equals(name, user.name) &&
Objects.equals(pwd, user.pwd);
}
@Override
public int hashCode() {
return Objects.hash(uid, name, pwd);
}
}
UserService.java
package com.minicuper.ssh2.service;
import com.minicuper.ssh2.dao.UserDao;
import com.minicuper.ssh2.entity.User;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean regist(User user){
System.out.println("UserService regist()");
return userDao.regist(user);
}
}
UserDao.java
package com.minicuper.ssh2.dao;
import com.minicuper.ssh2.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
public class UserDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public boolean regist(User user){
System.out.println("UserDao regist()");
Session currentSession = sessionFactory.openSession();
Transaction transaction = currentSession.beginTransaction();
try {
currentSession.save(user);
transaction.commit();
return true;
}catch (Exception e){
e.printStackTrace();
transaction.rollback();
}finally {
currentSession.close();
sessionFactory.close();
}
return false;
}
}
spring-config.xml
com.minicuper.ssh2.entity.User
jdbc:mysql://localhost:3306/ssh?serverTimezone=UTC
com.mysql.cj.jdbc.Driver
1234
root
create
true
true
org.hibernate.dialect.MySQL5Dialect
struts.xml
regex:.*
result.jsp
result.jsp
web.xml
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-config.xml
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
姓名:
密码:
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
Result:
部署到Tomcat服务器:
常见问题:
- sessionFactory.getCurrentSession()
异常: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
配置不起作用:thread
解决方法: 配置HibernateTransactionManager
在Spring配置文件中添加:
后在Service类上添加注解:@Transactional
- 配置事务时
出现异常:org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
解决方法:
1.导入c3p0相关的包.
2.修改spring.config.xml
3.Dao类中原来的事务操作删掉即可
添加
修改
com.minicuper.practice.entity.User
org.hibernate.dialect.MySQL5Dialect
true
true
create
添加
添加
- 配置c3p0时: 出现 ClassNotFoundException: com.mchange.v2.ser.Indirector
解决方法:添加 mchange-commons-java.jar包