spring+mybatis+activiti整合

 

项目结构:

pom文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>


  <groupId>com.simple.activiti.demo</groupId>
  <artifactId>activiti</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>activiti</name>
  <url>http://maven.apache.org</url>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <activiti.version>5.22.0</activiti.version>
      <fastjson.version>1.2.37</fastjson.version>
      <spring.version>4.3.14.RELEASE</spring.version>
  </properties>


  <dependencies>
    <!-- activiti -->
      <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring</artifactId>
        <version>${activiti.version}</version>
      </dependency>
      <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-json-converter</artifactId>
        <version>${activiti.version}</version>
      </dependency>
      <dependency><!-- 模型相关的操作:创建、保存、转换json与xml格式等 -->
        <groupId>org.activiti</groupId>
        <artifactId>activiti-modeler</artifactId>
        <version>${activiti.version}</version>
      </dependency>
      <dependency><!-- 处理流程图有关的功能:流程图布局(layout)、节点高亮等 -->
        <groupId>org.activiti</groupId>
        <artifactId>activiti-diagram-rest</artifactId>
        <version>${activiti.version}</version>
      </dependency>
<!-- activiti end -->


<!-- json解析 -->
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
      </dependency>
      
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- spring -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
      </dependency>
<!-- spring end -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>




<!-- springmvc begin -->
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-jdbc</artifactId> 
<version>4.3.14.RELEASE</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-web</artifactId> 
<version>4.3.14.RELEASE</version> 
</dependency>
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-webmvc</artifactId> 
<version>4.3.14.RELEASE</version> 
</dependency> 
<!-- springmvc end -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mybatis/spring包 --> 
<dependency> 
<groupId>org.mybatis</groupId> 
<artifactId>mybatis-spring</artifactId> 
<version>1.3.2</version> 
</dependency> 
<!-- junit相关 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- log manger begin -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>


<!-- log manger end -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>


  </dependencies>

</project>


---------------mybatis-cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 

</configuration>

------------log4j.properties

log4j.rootLogger=DEBUG,Console,Stdout 


#Console 
log4j.appender.Console=org.apache.log4j.ConsoleAppender 
log4j.appender.Console.layout=org.apache.log4j.PatternLayout 
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 


log4j.logger.java.sql.ResultSet=INFO 
log4j.logger.org.apache=INFO 
log4j.logger.java.sql.Connection=DEBUG 
log4j.logger.java.sql.Statement=DEBUG 
log4j.logger.java.sql.PreparedStatement=DEBUG 


log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender 
log4j.appender.Stdout.File = D://logs/bpmnTest.log 
log4j.appender.Stdout.Append = true 
log4j.appender.Stdout.Threshold = DEBUG 
log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout 

log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n


-------spring-mvc.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">
<!-- spring 整合mybaitis begin -->
<!-- 引入jdbc配置文件 --> 
<!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!-- <property name="locations"> -->
<!-- <list> -->
<!-- <value>classpath:properties/*.properties</value> -->
<!-- 要是有多个配置文件,只需在这里继续添加即可 -->
<!-- </list> -->
<!-- </property> -->
<!-- </bean> -->




<!-- 
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="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<!-- 连接数据的配置 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/jep?useUnicode=true&amp;characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<!-- 使用properties来配置 --> 
<!-- <property name="driverClassName"> -->
<!-- <value>${jdbc_driverClassName}</value> -->
<!-- </property> -->
<!-- <property name="url"> -->
<!-- <value>${jdbc_url}</value> -->
<!-- </property> -->
<!-- <property name="username"> -->
<!-- <value>${jdbc_username}</value> -->
<!-- </property> -->
<!-- <property name="password"> -->
<!-- <value>${jdbc_password}</value> -->
<!-- </property> -->
</bean>
<!-- 配置数据源事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/> <!-- 引用上面的数据源 -->
</bean>


<!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
<property name="basePackage" 
value="com.simple.activiti.demo.dao" /> 
</bean> 
<!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置--> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
<property name="dataSource" ref="dataSource" /> 
<property name="mapperLocations" value="classpath*:com/simple/activiti/demo/mapper/**/*.xml"/> 
<property name="configLocation" value="classpath:mybatis/mybatis-cfg.xml" /> 


</bean> 


<!-- 自动扫描注解的bean --> 
<context:component-scan base-package="com.simple.activiti.demo.service" /> 
<!-- spring 整合mybaitis end -->


<!-- activiti begin -->
<!-- 配置流程引擎配置类 注意:这是用 org.activiti.spring.SpringProcessEngineConfiguration 这个类-->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="createDiagramOnDeploy" value="true" /> <!-- 是否生成流程定义图片 -->
<!-- 生成流程图的字体不设置会乱码 -->
<property name="activityFontName" value="宋体"/>
<property name="labelFontName" value="宋体"/>
</bean>


<!-- 配置流程引擎工厂 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>


<!-- 配置注入一些服务 -->
<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="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<!-- activiti end -->

</beans>


---------UserDao

package com.simple.activiti.demo.dao;


import com.simple.activiti.demo.domain.User;
/**

* @author Ancan
*
*/
public interface UserDao {
public User selectUserById(Integer userId); 

}


------User

package com.simple.activiti.demo.domain;
/**

* @author Ancan
*
*/
public class User {
private Integer userId; 
private String userName; 
private String userPassword; 
private String userEmail; 


public Integer getUserId() { 
return userId; 



public void setUserId(Integer userId) { 
this.userId = userId; 



public String getUserName() { 
return userName; 



public void setUserName(String userName) { 
this.userName = userName; 



public String getUserPassword() { 
return userPassword; 



public void setUserPassword(String userPassword) { 
this.userPassword = userPassword; 



public String getUserEmail() { 
return userEmail; 



public void setUserEmail(String userEmail) { 
this.userEmail = userEmail; 



@Override 
public String toString() { 
return "User [userId=" + userId + ", userName=" + userName 
+ ", userPassword=" + userPassword + ", userEmail=" + userEmail 
+ "]"; 

}


------UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.simple.activiti.demo.dao.UserDao"> 
<!--设置domain类和数据库中表的字段一一对应,注意数据库字段和domain类中的字段名称不致,此处一定要!--> 
<resultMap id="BaseResultMap" type="com.simple.activiti.demo.domain.User"> 
<id column="USER_ID" property="userId" jdbcType="INTEGER" /> 
<result column="USER_NAME" property="userName" jdbcType="CHAR" /> 
<result column="USER_PASSWORD" property="userPassword" jdbcType="CHAR" /> 
<result column="USER_EMAIL" property="userEmail" jdbcType="CHAR" /> 
</resultMap> 
<!-- 查询单条记录 --> 
<select id="selectUserById" parameterType="int" resultMap="BaseResultMap"> 
SELECT * FROM t_user WHERE USER_ID = #{userId} 
</select> 
</mapper>



-------UserService

package com.simple.activiti.demo.service;


import com.simple.activiti.demo.domain.User;


public interface UserService {
User selectUserById(Integer userId); 

}


------UserServiceImpl

package com.simple.activiti.demo.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.simple.activiti.demo.dao.UserDao;
import com.simple.activiti.demo.domain.User;
import com.simple.activiti.demo.service.UserService;


@Service
public class UserServiceImpl implements UserService{
@Autowired 
private UserDao userDao; 


public User selectUserById(Integer userId) { 
return userDao.selectUserById(userId); 



}


------BaseTestCase

package com.simple.activiti.demo.bpm;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;


/**
* 加载activiti的23张表的运行例子
* @author AnCan
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:springmvc/spring-mvc.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@Transactional
public class BaseTestCase extends AbstractTransactionalJUnit4SpringContextTests{


@Test
/**
* 运行此方法,加载activiti的数据库表信息
*/
public void test(){
System.out.println ("test"); 
}


}


-----BpmDefinitionTest

package com.simple.activiti.demo.bpm;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.ImplementationType;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.ServiceTask;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


public class BpmDefinitionTest extends BaseTestCase {


@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
TaskService taskService;


@Test
public void testDeploy() throws IOException {
InputStream is = readXmlFile();
Assert.assertNotNull(is);
// 发布流程
Deployment deployment = repositoryService.createDeployment().addInputStream("bpmn20.xml", is)
.name("holidayRequest").deploy();
Assert.assertNotNull(deployment);
System.out.println("deployId:" + deployment.getId());
// 查询流程定义
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId(deployment.getId()).singleResult();


Long businessKey = new Double(1000000 * Math.random()).longValue();
// 启动流程
runtimeService.startProcessInstanceById(processDefinition.getId(), businessKey.toString());
// 查询任务实例
List<Task> taskList = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
Assert.assertNotNull(taskList == null);
Assert.assertTrue(taskList.size() > 0);
for (Task task : taskList) {
System.out.println("task name is " + task.getName() + " ,task key is " + task.getTaskDefinitionKey());
}
}


public InputStream readXmlFile() throws IOException {
String filePath = "doublePointTemplate.bpmn";
return Class.class.getClass().getResource("/" + filePath).openStream();
}


}

-----UserserviceTest

package com.simple.activiti.demo.user;


import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


import com.simple.activiti.demo.bpm.BaseTestCase;
import com.simple.activiti.demo.domain.User;
import com.simple.activiti.demo.service.UserService;


public class UserserviceTest extends BaseTestCase {
@Autowired 
private UserService userService; 
Logger logger = LoggerFactory.getLogger(UserserviceTest.class); 


@Test 
public void selectUserByIdTest(){ 
User user = userService.selectUserById(10); 
logger.debug("查找结果" + user); 

}


-----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>activiti2</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

阅读更多
个人分类: 学习笔记
上一篇Spring之旅(一、装配Bean)
下一篇spring学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭