设计代码分3层:
view-->service-->Dao
Struts2+hibernate+spring+JBPM+junit+jquery
一. 建数据库:
create database itcastoa0720 default character set utf8;
查看:
show create database itcastoa0720
二. Myeclipse工程
1. 新建web工程, 工程右键属性, 编码设为utf-8
2. 添加框架环境,
Struts2: jar, struts.xml, web.xml
hibernate: jar, hibernate.cfg.xml, *.hbm.xml
spring:jar, applicationContext
junit: jar
3. 整合SSH
Struts2+spring整合: 目的, spring负责主要IOC(管理对象), AOP(事务管理), 就为了让spring容器来管理struts的action
hibernate+spring整合: 目的, sessionFactory是创建session的, factory就需要一个, 管理一个工厂的话,用spring来管理一个单例工厂, 最重要的是, 声明式事务管理
4. 资源分类
5. 配置日志
添加junit的jar
添加框架环境, Struts2
1. 添加Struts2的jar (commons-fileupload, commons-io, freemarker, ognl, struts2-core, xwork-core), Struts.xml, web.xml
struts.xml:
alt+/可以提示, 但是不能自动.设置自动提示:
1. Preferences对话框中,选择“XML Catalog” ,
2. add目录:D:\Java\jar\struts-2.3.24.1\src\core\src\main\resources --> URI.
3. 设置xml关联编辑器: Preferences->file asso...->找到.xml, 设置myeclipse xml editor为默认.
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
2.配置 web.xml, 添加struts2的filter:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
添加框架环境, hibernate: jar包, hibernate.cfg.xml, *.hbm.xml
jar包: 核心包, 必须包, jpa, c3p0, jdbc
hibernate.cfg.xml:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost/hibernate
root
bjsxt
true
update
1
添加框架环境, spring: jar包, applicationContext.xml/beans.xml
applicationContext.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
整合spring和struts2:
新建class TestAction, 包cn.itcast.oa.test:
package cn.itcast.oa.test;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("===>TestAction.execute()");
return "success";
}
}
创建一个jsp:test.jsp, 有编码就改成utf-8
My JSP 'index.jsp' starting pageStruts2添加成功.
配置struts.xml:
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
/test.jsp
test.jsp:
My JSP 'index.jsp' starting pageStruts2添加成功.
运行成功.
整合前测试spring:
1. 用于声明bean
@Service用于标注业务层组件
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
2. 配置bean的生命周期: prototype, 不是单例.
详细步骤:
1. 新建类SpringTest.java:
package cn.itcast.oa.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
}
2. 在testAction类前加入@Controller
package cn.itcast.oa.test;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("===>TestAction.execute()");
return "success";
}
}
3. 在SpringTest上运行 junit test , 成功!
package cn.itcast.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
}
还有一种方法: XML, 只需要删除testAction类前的@Controller 更改ApplicationContext.xml:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
整合开始: 1. 在web.xml里配置spring的监听器 2. 加入整合jar包
4. 配置web.xml里的sping监听器:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext*.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
index.jsp
5. 加入struts2-sping-plugin的jar包
6. 更改struts.xml:, 把action的class属性改成bean名称
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
/test.jsp
整合成功测试test.jsp:
My JSP 'index.jsp' starting pageStruts2添加成功.
Struts2与spring整合成功.
运行: http://localhost:8080/Itcastoa/test.action, 整合成功!!!
spring与struts2整合总结:
1. 加入整合jar包, struts2-sping-plugin的jar包,
在web.xml里配置spring的监听器
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext*.xml
2. 在action上面加入@Controller:
package cn.itcast.oa.test;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("===>TestAction.execute()");
return "success";
}
}
3. 配置struts.xml文件, 就可以在配置action的class的时候直接使用 action名字
/test.jsp
所以spring整合struts2的目的就是为了管理action , 如果action里想使用某个service文件的话, 直接注入@Resource就可以了.
hibernate+spring整合
整合目的:
1. spring管理sessionFactory实例(只需要一个)
2. 声明式事务管理
改配置文件:
1. 考虑到连接池的问题, 将hibernate配置文件里的jdbc配置注释掉:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
true
update
1
2. 新增jdbc.properties, 加入mysql配置:
jdbcUrl=jdbc:mysql://localhost/itcastoa0720
driverClass=com.mysql.jdbc.Driver
user=root
password=linda0213
3. applicationContext.xml配置: 别忘了要导入外部properties文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
4. springtest.java里测试sessionFactory:
package cn.itcast.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
@Test
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
}
测试成功后进行声明式事务管理:
1. 配置applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
2. 新建user.java, 包: cn.itcast.oa.domain
package cn.itcast.oa.domain;
public class User {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3. User.hbm.xml:
/p>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. 配置hibernate.cfg.xml里的mapping内容:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
true
update
1
5. 开始写测试文件, 先添加一个TestService.java:
package cn.itcast.oa.test;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.oa.domain.User;
@Service
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUsers(){
Session session = sessionFactory.getCurrentSession();
session.save(new User());
int a = 1/0; //exception
session.save(new User());
}
}
写测试文件:
package cn.itcast.oa.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testBean() throws Exception {
TestAction testAction = (TestAction) ac.getBean("testAction");
System.out.println(testAction);
}
@Test
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUsers();
}
}
测试文件test.jsp,
My JSP 'index.jsp' starting pageStruts2添加成功.
Struts2与spring整合成功.
Struts2与spring与hibernate整合成功.
布置工程时出错, 导入slf-nop包解决!!
http://localhost:8080/Itcastoa/test.action后,
创建了新表, 把service里的异常删掉, 再执行, 保存进去两个数据. 并且从2,3开始计数id
到此整合完成!!!
spring和hibernate整合总结:
1. 新建jdbc.properties, 把数据库连接信息填入:
jdbcUrl=jdbc:mysql://localhost/itcastoa0720
driverClass=com.mysql.jdbc.Driver
user=root
password=linda0213
2. 配置hibernate.cfg.xml:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
true
update
1
3. applicationContext.xml配置内容: 导入属性文件, sessionFactory, dataSource, transaction
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
4. 编写业务逻辑service类: 此时就可以注入容器里的sessionFactory了, 同时声明事务管理
@Service
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUsers(){
Session session = sessionFactory.getCurrentSession();
session.save(new User());
//int a = 1/0; //exception
session.save(new User());
}
}
5. 测试文件可以直接获取testService了:
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testTransaction() throws Exception {
TestService testService = (TestService) ac.getBean("testService");
testService.saveTwoUsers();
}
}
到此 spring和hibernate整合完成,
再验证struts整合:
action调用testService测试:
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport{
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("===>TestAction.execute()");
testService.saveTwoUsers();
return "success";
}
}
浏览器输入http://localhost:8080/itcastoa/test.action验证成功!!!
Struts2添加成功.
Struts2与spring整合成功.
Struts2与spring与hibernate整合成功.