Activiti学习文档(一)之整合SSH框架开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/samile6899/article/details/52557242

条件:

a)    Activiti5.4版本;

        b)    Eclipse开发工具;

        c)    MySQL 5.0以上版本;


         1,使用Eclipse创建一个Web工程;(注: a,在创建Web 工程之前,要先配置tomcat 的目录;b,在创建Web工程之后,改变编译后的class 文件和资源文件的存放位置; c,指定编码格式,UTF-8)


2,导入相应的 jar 包;(注: jar 包分为 SSH框架所需要的jar包和Activiti 5.4的 jar 包)

            http://download.csdn.net/detail/samile6899/9631620   ( SSH所需JAR包 )

            http://download.csdn.net/detail/samile6899/9631623  ( Activiti 所需JAR包 )


3,配置文件

                

               以下分别是各个配置文件:

                

                     activiti-context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<!-- spring负责创建流程引擎的配置文件 -->
	<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置事务管理器,统一事务 -->
		<property name="transactionManager" ref="transManager" />
		<!-- 设置建表策略,如果没有表,自动创建表 -->
		<property name="databaseSchemaUpdate" value="true" />
	</bean>
	<!-- 创建流程引擎对象 -->
	<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
		<property name="processEngineConfiguration" ref="processEngineConfiguration" />
	</bean>
	
	<!-- 
	相当于下面的代码
	RepositoryServicie repositoryService = processEngine.getRepositoryService();
	RuntimeServicie repositoryService = processEngine.getRuntimeServicie();
	TaskServicie taskServicie = processEngine.getTaskServicie();
	HistoryServicie historyServicie = processEngine.getHistoryServicie();
	 -->
	<!-- 由流程引擎对象,提供的方法,创建项目中使用的Activiti工作流的Service -->
	<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
	<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
	<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
	<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
	<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
	
</beans>
                        

                   applicationContext-action.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
		                    http://www.springframework.org/schema/tx 
		                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		                    http://www.springframework.org/schema/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
</beans>
 

                       applicationContext-dao.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
		                    http://www.springframework.org/schema/tx 
		                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		                    http://www.springframework.org/schema/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

</beans>

                      applicationContext-service.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
		                    http://www.springframework.org/schema/tx 
		                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		                    http://www.springframework.org/schema/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	 

</beans>

                 applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		                    http://www.springframework.org/schema/context 
		                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
		                    http://www.springframework.org/schema/tx 
		                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		                    http://www.springframework.org/schema/aop 
		                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="${jdbc.url}" />
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 配置外部数据库连接信息 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 创建SessionFactory,这是spring整合hibernate的核心 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<!-- 1.配置datasource -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 2.配置Hibernate属性 -->
		<property name="hibernateProperties">
			<value>
				hibernate.hbm2ddl.auto=update
				hibernate.show_sql=true
				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
			</value>
		</property>
		<!-- 3.配置映射文件 -->
		<property name="mappingLocations">
			<list>
				<value>classpath:cn/itcast/ssh/domain/*.hbm.xml</value>
			</list>
		</property>
	</bean>

	<!-- 配置事务 -->
	<!-- 1.配置事务管理器 -->
	<bean id="transManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 2.配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transManager">
		<tx:attributes>
			<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 3.配置切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* cn.itcast.ssh.service..*.*(..))"
			id="aopPointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="aopPointcut" />
	</aop:config>

	<!-- 配置基础的Dao,在其他的DAO中只需要继承即可 -->
	<bean id="baseDao" abstract="true">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 导入相关配置 -->
	<import resource="classpath:applicationContext-dao.xml" />
	<import resource="classpath:applicationContext-service.xml" />
	<import resource="classpath:applicationContext-action.xml" />
	<import resource="classpath:activiti-context.xml" />



</beans>                    

                db.properties文件:

jdbc.url=jdbc:mysql:///itcast0820project?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123


                 log4j.properties文件:

log4j.rootLogger=WARN, CA

# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n


                  struts.xml文件:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 配置模式为开发模式,自动加载struts.xml和显示错误提示信息 -->
    <constant name="struts.devMode" value="true" />
    <!-- 设置页面主题为简单主题,去掉struts2开发提供的样式 -->
    <constant name="struts.ui.theme" value="simple" />

    <package name="default" namespace="/" extends="struts-default">
    
    </package>
</struts>

                      web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  
  <display-name>Test01</display-name>
  <!-- 配置OpenSessionInViewFilter过滤器,可以解决项目中出现的懒加载问题 -->
  <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <!-- 配置spring启动的监听器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置struts2启动的过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

               新建com.itcast.ssh.domain 包,在domain 包中新建 hibernate 需要的 javabean和映射文件(  xxxx.hbm.xml  )


                      Employee类:

package cn.itcast.ssh.domain;

public class Employee {
	private Long id;
	private String name;
	private String password;
	private String email;
	private String role;
	private Employee manager;
	
	public Employee() {
		super();
	}

	public Employee(Long id, String name, String password, String email, String role, Employee manager) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.email = email;
		this.role = role;
		this.manager = manager;
	}

	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;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public Employee getManager() {
		return manager;
	}

	public void setManager(Employee manager) {
		this.manager = manager;
	}
	
}


Employee.hbm.xml 文件:              
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.ssh.domain">
<class name="Employee" table="a_employee">
<id name="id" type="long" column="id">
            <generator class="native"/>
</id>
<property name="name" type="string" column="name" unique="true" />
<property name="password" type="string" column="password" />
<property name="email" type="string" column="email" unique="true"/>
<property name="role" type="string" column="role" />
       <many-to-one name="manager" class="Employee" column="manager_id"></many-to-one>
</class>
</hibernate-mapping>

               LeaveBill类:
package cn.itcast.ssh.domain;

import java.util.Date;

public class LeaveBill {
	private Long id;
	private Integer days;
	private String content;
	private Date leaveDate=new Date();
	private String remark;
	private Employee user;
	private Integer state=0;
	
	public LeaveBill() {
		super();
	}

	public LeaveBill(Long id, Integer days, String content, Date leaveDate, String remark, Employee user,
			Integer state) {
		super();
		this.id = id;
		this.days = days;
		this.content = content;
		this.leaveDate = leaveDate;
		this.remark = remark;
		this.user = user;
		this.state = state;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getDays() {
		return days;
	}

	public void setDays(Integer days) {
		this.days = days;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getLeaveDate() {
		return leaveDate;
	}

	public void setLeaveDate(Date leaveDate) {
		this.leaveDate = leaveDate;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Employee getUser() {
		return user;
	}

	public void setUser(Employee user) {
		this.user = user;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}
	
}
  

                   LeaveBill.hbm.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.ssh.domain">
<class name="LeaveBill" table="a_leaveBill">
<id name="id" type="long" column="id">
            <generator class="native"/>
</id>
<property name="days" type="integer" column="days"/>
<property name="content"  type="string" column="content"/>
<property name="remark" type="string" column="remark"/>
<property name="leaveDate" type="date" column="leaveDate"/>
<property name="state" type="integer" column="state"/>
<many-to-one name="user" class="Employee" column="user_id"></many-to-one>
</class>
</hibernate-mapping>


          4,项目包结构:

 

       

          5,新建 login.jsp 页面:

   

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	<h1>欢迎首页</h1>
</body>
</html>

      

         6,初始化 Activiti 工作流所需的23张表

                 a,新建 activiti.cfg.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<!-- 
		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
		//连接数据库的配置
		processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
		processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/itcast0711activiti?useUnicode=true&characterEncoding=utf8");
		processEngineConfiguration.setJdbcUsername("root");
		processEngineConfiguration.setJdbcPassword("root");
		
		/**
		 	public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自动创建表,需要表存在
  			public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表
  			public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在,自动创建表
		 */
		processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
	 -->
	<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
		<!-- 连接数据的配置 -->
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mi?useUnicode=true&characterEncoding=utf8"></property>
		<property name="jdbcUsername" value="root"></property>
		<property name="jdbcPassword" value="123"></property>
		<!-- 没有表创建表 -->
		<property name="databaseSchemaUpdate" value="true"></property>
	</bean>

</beans>

                  b,新建  TestActiviti 测试类,初始化数据表

package com.juyuan238.mi.test;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.junit.Test;

public class TestAaciviti {
	
	@Test
	public void createTable(){
		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
		//工作流的核心对象,ProcessEnginee对象
		ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
		System.out.println("processEngine: "+processEngine);
	}
}

                 c,运行  TestActiviti 测试类,OK……


           这样的话,搭建的过程就整个完成了。可以运行Tomcat 服务器测试下,是否能够跑的通。下一节继续讲Activiti5.4在SSH框架的开发与使用……


      **************************************************************************************************

      注:Activiti 学习总结资料下载 (http://download.csdn.net/detail/samile6899/9829785

猜你喜欢

转载自blog.csdn.net/samile6899/article/details/52557242
今日推荐