在新建项目中选择Spring中的Spring 、Web 和Hibernate,设置为稍后设置库。
-
如果web中有struts选项直接选择,没有的话可以稍后设置。
-
如果左侧没有spring,可以按住Ctrl+Alt+Shift+/,选择弹出界面的第一个选项,找到javaee.legacy.project.wizard选中即可。
在项目中新建lib文件夹导入如图46个jar包,并添加为库。
配置数据库环境创建一个数据库,添加一个person表如图的数据库环境。
配置Struts环境 配置 Struts 核心过滤器在web.xml中配置struts核心过滤器
添加 struts.xml 配置文件struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /*
在src中添加struts.xml文件,然后为struts添加文件集。
添加 log4j.properties 文件在 Hibernate 解压包中的 projectetc 路径下找到 log4j.properties 的文件,并复制到 src 源文件夹中。打开并编辑后,如下所示。
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file myLog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=E:/myLog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
配置Spring环境
添加 applicationContext.xml 文件
在src中添加applicationContext.xml文件,然后为spring添加应用程序上下文。
配置 Spring 的监听器和过滤器在web.xml中配置Spring的监听器和过滤器
配置 Hibernate 环境struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /* contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener OpenSessionInViewFilter org.springframework.orm.hibernate5.support.OpenSessionInViewFilter OpenSessionInViewFilter /*
在src中添加hibernate.cfg.xml文件。
Spring 和 Hibernate 整合 创建实体类jdbc:mysql://localhost:3306/filmsystem?serverTimezone=UTC com.mysql.cj.jdbc.Driver root 123456 org.hibernate.dialect.MySQL5Dialect
package POJO;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -3541561917509006050L;
private String id;
private String name;
public Person() {
}
public Person(String id,String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
创建映射文件
编写 Spring 的配置信息
编写测试类
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import POJO.Person;
public class TestMerge {
ClassPathXmlApplicationContext ctx;
@Before
public void loadCtx() {
// 加载配置文件
ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
}
@Test
public void testHibernate() {
SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory");
Session session = sf.openSession();
Transaction transaction = session.beginTransaction();
session.save(new Person("1","用户1"));
transaction.commit();
session.close();
sf.close();
}
}
运行测试类
如图,测试成功!
Spring 与 Struts2 整合 实现 Service 的配置 创建接口package service;
public interface PersonService {
public void say();
}
创建接口实现类
package service;
public class PersonServiceImpl implements PersonService{
@Override
public void say() {
System.out.println("Service say hello");
}
}
配置 Spring
在applicationContext.xml中配置
验证配置
为了验证 Spring 的加载是否正确,可以在测试类 TestMerge.java 中创建一个名称为 testSpring 的方法进行测试。该方法代码如下所示
@Test
public void testSpring(){
PersonService ts = (PersonService)ctx.getBean("personService");
ts.say();
}
如图配置成功!
实现 Action 的配置 创建 Actionpackage action;
import com.opensymphony.xwork2.ActionSupport;
import service.PersonService;
public class PersonAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public String execute() {
personService.say();
return SUCCESS;
}
}
配置 Struts2
配置 Action 的 Bean 信息/index.jsp
查看测试结果
启动项目后,运行jsp页面,如下图:
此时控制台输出
说明配置成功! 项目结构


