Spring+Dubbo+MyBatis+Linner分布式Web开发环境搭建
本文承接我之前的博客《Spring+Maven+Dubbo+MyBatis+Linner+Handlebars—Web开发环境搭建》,以下我在对相关的Maven配置和详细的Spring配置文件进行简单介绍。
整个开发框架大体结构如下:
1) 核心业务逻辑工程用于处理系统自身的核心业务逻辑;
2) 桥梁工程用于关联页面工程和后台处理逻辑;
3) 公共业务逻辑对公共的业务逻辑进行处理;
4) Maven父工程用于对整个开发环境进行配置,包括jar包的定义与管理,系统基本环境的配置(例:数据库连接的管理);
5) 客户端接口工程用于发布服务给桥梁工程或可信任的第三方调用;
6) 客户端接口实现工程用于具体实现服务接口;
7) 任务调度工程用于处理相关任务调度;
8) 总的框架工程用于对前面的后台工程进行统一的管理。
以下我将对各个工程进行逐一介绍:
1.核心业务逻辑mkhl-biz:
mkhl-biz工程的具体结构如下图:
1)biz工程的pom文件:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>mkhl-parent</artifactId>
<groupId>com.ouc.mkhl.supplier</groupId>
<version>1.0</version>
<relativePath>../mkhl-parent/pom.xml</relativePath>
</parent>
<artifactId>mkhl-biz</artifactId>
<name>mkhl-biz</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- oop开放平台核心业务(此jar包须具有相关开发权限) -->
<dependency>
<groupId>com.ouc</groupId>
<artifactId>oop-core</artifactId>
</dependency>
<dependency>
<groupId>weblogic</groupId>
<artifactId>wlfullclient</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!--
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
-->
<dependency>
<groupId>com.ouc.mkhl.supplier</groupId>
<artifactId>mkhl-service-client</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.ouc.mkhl.supplier</groupId>
<artifactId>mkhl-common</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
</dependency>
<!-- mysql连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <version>5.1.34</version> -->
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<!-- <version>1.6</version> -->
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<!-- <version>1.4</version> -->
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
</dependency>
<!-- XStream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3_min</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.terracotta.quartz</groupId>
<artifactId>quartz-terracotta</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.haier.openplatform.hfs</groupId>
<artifactId>hfs-service-client</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>com.ouc.openplatform</groupId>
<artifactId>monitor-service-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>config-toolkit-easyzk</artifactId>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j</artifactId>
</dependency>
<dependency>
<groupId>mx4j</groupId>
<artifactId>mx4j-tools</artifactId>
</dependency>
<dependency>
<groupId>com.ouc</groupId>
<artifactId>openapi-auth</artifactId>
</dependency>
<dependency>
<groupId>com.ouc</groupId>
<artifactId>openapi-service-client</artifactId>
</dependency>
</dependencies>
</project>
注:当中与ouc相关的包不对外开发。
2)以安全模块为例,对单一模块的编写进行简单介绍。
单一模块结构如下图:
(1)advice实例:OperationLogAdvice
package com.ouc.mkhl.supplier.security.advice;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import com.ouc.openplatform.log.advice.DefaultOperationLogAdvice;
import com.ouc.openplatform.log.config.LogConfiguration;
import com.ouc.openplatform.log.domain.OperationLog;
import com.ouc.openplatform.security.LoginContext;
import com.ouc.openplatform.security.LoginContextHolder;
import com.ouc.openplatform.util.OOPConstant;
import com.ouc.mkhl.supplier.security.service.OperationLogService;
/**
* @author Tom
*/
public class OperationLogAdvice extends DefaultOperationLogAdvice {
private OperationLogService operationLogService;
@Override
protected void saveLog(OperationLog operationLog) {
operationLogService.save(operationLog);
}
public void setOperationLogService(OperationLogService operationLogService) {
this.operationLogService = operationLogService;
}
protected OperationLog createOperationLog(ProceedingJoinPoint thisJoinPoint,LogConfiguration logConfiguration){
LoginContext loginContext = LoginContextHolder.get();
if(loginContext == null){
loginContext = new LoginContext();
loginContext.setUserId(-999L);
loginContext.setUserName("-SYSTEM-");
}
OperationLog operationLog = new OperationLog();
operationLog.setAppName(HOPConstant.getAppName());
operationLog.setGmtCreate(new Date());
operationLog.setGmtModified(new Date());
operationLog.setUserId(loginContext.getUserId());
operationLog.setUserName(loginContext.getUserName());
operationLog.setOperationType(logConfiguration.getType());
operationLog.setModule(logConfiguration.getModule());
operationLog.setDescription(executeTemplate(getMessage(logConfiguration.getMessageMap()),thisJoinPoint));
return operationLog;
}
}
(2)dao实例:SupplyUserDAO
package com.ouc.mkhl.supplier.security.dao;
import java.util.List;
import com.ouc.mkhl.supplier.security.model.SupplyUser;
public interface SupplyUserDAO {
public int deleteByPrimaryKey(String supplycode);
public int insert(SupplyUser record);
public int insertSelective(SupplyUser record);
public SupplyUser selectByPrimaryKey(String supplycode);
public int updateByPrimaryKeySelective(SupplyUser record);
public int updateByPrimaryKey(SupplyUser record);
public List<SupplyUser> selectAllSupplyUser();
public SupplyUser selectByVCode(String supplycode);
}
(3)domain实例:OrderNum
package com.ouc.mkhl.supplier.security.domain;
public class OrderNum {
private String itemName; // 项目名
private int myOrderNum; // 我的订单
private int proOrderNum; // 订单生产
private int tstOrderNum; // 订单检测
private int subOrderNum; // 订单交付
private String percentage; // 完成率
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName == null ? null : itemName.trim();
}
public int getMyOrderNum() {
return myOrderNum;
}
public void setMyOrderNum(int myOrderNum) {
this.myOrderNum = myOrderNum;
}
public int getProOrderNum() {
return proOrderNum;
}
public void setProOrderNum(int proOrderNum) {
this.proOrderNum = proOrderNum;
}
public int getTstOrderNum() {
return tstOrderNum;
}
public void setTstOrderNum(int tstOrderNum) {
this.tstOrderNum = tstOrderNum;
}
public int getSubOrderNum() {
return subOrderNum;
}
public void setSubOrderNum(int subOrderNum) {
this.subOrderNum = subOrderNum;
}
public String getPercentage() {
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage == null ? null : percentage.trim();
}
}
(4)model实例:SupplyUser
package com.ouc.mkhl.supplier.security.model;
import java.io.Serializable;
public class SupplyUser implements Serializable{
private static final long serialVersionUID = -123120032141L;
private String supplycode;
private String supplypass;
private String supplyname;
public String getSupplycode() {
return supplycode;
}
public void setSupplycode(String supplycode) {
this.supplycode = supplycode == null ? null : supplycode.trim();
}
public String getSupplypass() {
return supplypass;
}
public void setSupplypass(String supplypass) {
this.supplypass = supplypass == null ? null : supplypass.trim();
}
public String getSupplyname() {
return supplyname;
}
public void setSupplyname(String supplyname) {
this.supplyname = supplyname == null ? null : supplyname.trim();
}
}
(5)内部Service接口实例:SupplyUserService
package com.ouc.mkhl.supplier.security.service;
import java.util.List;
import com.ouc.mkhl.supplier.security.model.SupplyUser;
public interface SupplyUserService {
public int saveSupplyUser(SupplyUser supplyUser);
public List<SupplyUser> getAllSupplyUser();
public SupplyUser getSupplyUserByVCode(String supplycode);
}
(6)
内部Service接口实现实例:SupplyUserServiceImpl
package com.ouc.mkhl.supplier.security.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ouc.mkhl.supplier.security.dao.SupplyUserDAO;
import com.ouc.mkhl.supplier.security.model.SupplyUser;
import com.ouc.mkhl.supplier.security.service.SupplyUserService;
@Service
public class SupplyUserServiceImpl implements SupplyUserService {
private SupplyUserDAO supplyUserDAO;
public SupplyUserDAO getSupplyUserDAO() {
return supplyUserDAO;
}
public void setSupplyUserDAO(SupplyUserDAO supplyUserDAO) {
this.supplyUserDAO = supplyUserDAO;
}
@Override
public int saveSupplyUser(SupplyUser supplyUser) {
int a = supplyUserDAO.insert(supplyUser);
return a;
}
@Override
public List<SupplyUser> getAllSupplyUser() {
System.out.println("进入了biz层impl的getAllSupplyUser");
List<SupplyUser> supplyUserList = null;
try {
supplyUserList = supplyUserDAO.selectAllSupplyUser();
} catch (Exception e) {
e.printStackTrace();
}
return supplyUserList;
}
@Override
public SupplyUser getSupplyUserByVCode(String supplycode) {
SupplyUser supplyUser = null;
try {
supplyUser = supplyUserDAO.selectByVCode(supplycode);
} catch (Exception e) {
e.printStackTrace();
}
return supplyUser;
}
}
3)MyBatis配置:
(1)sqlMapConfig.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>
<settings>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="useGeneratedKeys" value="false" />
<setting name="autoMappingBehavior" value="PARTIAL" />
<setting name="defaultExecutorType" value="SIMPLE" /><!-- SIMPLE REUSE BATCH -->
<!-- <setting name="defaultExecutorType" value="BATCH" /> -->
<setting name="defaultStatementTimeout" value="25000" />
<setting name="safeRowBoundsEnabled" value="false" />
<setting name="mapUnderscoreToCamelCase" value="false" />
<setting name="localCacheScope" value="SESSION" />
<!-- <setting name="jdbcTypeForNull" value="OTHER" /> -->
<setting name="jdbcTypeForNull" value="NULL" />
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
</settings>
<typeAliases>
<!-- =========================================================== -->
<!-- security模块 -->
<!-- =========================================================== -->
<typeAlias alias="supplyUser" type="com.ouc.mkhl.supplier.security.model.SupplyUser"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="com.ouc.openplatform.dao.mybatis.SerializableTypeHandler"/>
</typeHandlers>
</configuration>
(2)mappers/security/SupplyUserMapper.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.ouc.mkhl.supplier.security.dao.SupplyUserDAO" >
<resultMap id="SupplyUserMap" type="supplyUser" >
<id column="SupplyCode" property="supplycode" jdbcType="VARCHAR" />
<result column="SupplyPass" property="supplypass" jdbcType="VARCHAR" />
<result column="SupplyName" property="supplyname" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
SupplyCode, SupplyPass, SupplyName
</sql>
<select id="selectAllSupplyUser" resultMap="SupplyUserMap">
select * from supplyuser
</select>
<select id="selectByVCode" resultType="supplyUser" parameterType="java.lang.String" >
select * from supplyuser
where SupplyCode = #{supplycode,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from supplyuser
where SupplyCode = #{supplycode,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="supplyUser" >
insert into supplyuser (SupplyCode, SupplyPass, SupplyName
)
values (#{supplycode,jdbcType=VARCHAR}, #{supplypass,jdbcType=VARCHAR}, #{supplyname,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="supplyUser" >
insert into supplyuser
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="supplycode != null" >
SupplyCode,
</if>
<if test="supplypass != null" >
SupplyPass,
</if>
<if test="supplyname != null" >
SupplyName,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="supplycode != null" >
#{supplycode,jdbcType=VARCHAR},
</if>
<if test="supplypass != null" >
#{supplypass,jdbcType=VARCHAR},
</if>
<if test="supplyname != null" >
#{supplyname,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="supplyUser" >
update supplyuser
<set >
<if test="supplypass != null" >
SupplyPass = #{supplypass,jdbcType=VARCHAR},
</if>
<if test="supplyname != null" >
SupplyName = #{supplyname,jdbcType=VARCHAR},
</if>
</set>
where SupplyCode = #{supplycode,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="supplyUser" >
update supplyuser
set SupplyPass = #{supplypass,jdbcType=VARCHAR},
SupplyName = #{supplyname,jdbcType=VARCHAR}
where SupplyCode = #{supplycode,jdbcType=VARCHAR}
</update>
</mapper>
4)Spring相关配置:
(1)spring/spring-common.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframew