以上是项目接口图,由于dubbo 官方要求接口要生产端和消费端共享,所以接口暂时放在common包下面,然后model装所有的entity,因为service 和 controller工程都要公用model,先来项目结构图
model 模块
model代码
package com.book.model.file;
import java.util.Date;
import com.book.model.obj.AbstractObject;
/**
* 【】持久化对象 数据库表:tb_file_store
*
* @date 2017-03-25 17:55:49
*
*/
public class FileStore extends AbstractObject{
public static final long serialVersionUID = 1L;
// 主键
private String fileId;
//
private String groupId;
//
private String filePath;
//
private String fileName;
//
private String fileExtName;
//
private Integer fileSize;
//
private Date createDate;
//
private String createUser;
//
private Date lastUpdateDate;
//
private String lastUpdateUser;
//
private Integer deleteFlag;
/** 获取 主键 属性 */
public String getFileId() {
return fileId;
}
/** 设置 主键 属性 */
public void setFileId(String fileId) {
this.fileId = fileId;
}
/** 获取 属性 */
public String getGroupId() {
return groupId;
}
/** 设置 属性 */
public void setGroupId(String groupId) {
this.groupId = groupId;
}
/** 获取 属性 */
public String getFilePath() {
return filePath;
}
/** 设置 属性 */
public void setFilePath(String filePath) {
this.filePath = filePath;
}
/** 获取 属性 */
public String getFileName() {
return fileName;
}
/** 设置 属性 */
public void setFileName(String fileName) {
this.fileName = fileName;
}
/** 获取 属性 */
public String getFileExtName() {
return fileExtName;
}
/** 设置 属性 */
public void setFileExtName(String fileExtName) {
this.fileExtName = fileExtName;
}
/** 获取 属性 */
public Integer getFileSize() {
return fileSize;
}
/** 设置 属性 */
public void setFileSize(Integer fileSize) {
this.fileSize = fileSize;
}
/** 获取 属性 */
public Date getCreateDate() {
return createDate;
}
/** 设置 属性 */
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/** 获取 属性 */
public String getCreateUser() {
return createUser;
}
/** 设置 属性 */
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
/** 获取 属性 */
public Date getLastUpdateDate() {
return lastUpdateDate;
}
/** 设置 属性 */
public void setLastUpdateDate(Date lastUpdateDate) {
this.lastUpdateDate = lastUpdateDate;
}
/** 获取 属性 */
public String getLastUpdateUser() {
return lastUpdateUser;
}
/** 设置 属性 */
public void setLastUpdateUser(String lastUpdateUser) {
this.lastUpdateUser = lastUpdateUser;
}
/** 获取 属性 */
public Integer getDeleteFlag() {
return deleteFlag;
}
/** 设置 属性 */
public void setDeleteFlag(Integer deleteFlag) {
this.deleteFlag = deleteFlag;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("FileStore");
sb.append("{fileId=").append(fileId);
sb.append(", groupId=").append(groupId);
sb.append(", filePath=").append(filePath);
sb.append(", fileName=").append(fileName);
sb.append(", fileExtName=").append(fileExtName);
sb.append(", fileSize=").append(fileSize);
sb.append(", createDate=").append(createDate);
sb.append(", createUser=").append(createUser);
sb.append(", lastUpdateDate=").append(lastUpdateDate);
sb.append(", lastUpdateUser=").append(lastUpdateUser);
sb.append(", deleteFlag=").append(deleteFlag);
sb.append('}');
return sb.toString();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof FileStore) {
FileStore fileStore = (FileStore) obj;
if (this.getFileId().equals(fileStore.getFileId())) {
return true;
}
}
return false;
}
public int hashCode() {
String pkStr = "" + this.getFileId();
return pkStr.hashCode();
}
}
package com.book.model.obj;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
public abstract class AbstractObject
implements Serializable, Cloneable
{
public static final long serialVersionUID = 1L;
public String toString()
{
return ToStringBuilder.reflectionToString(this);
}
public Object clone()
{
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
}
package com.book.model;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ModelApplication {
public static void main(String[] args) {
SpringApplication.run(ModelApplication.class, args);
}
}
model模块的 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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.book</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>model</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<commonslang.version>2.6</commonslang.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commonslang.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
common模块 ,主要是做一些底层公共方法的封装,以及接口的暴露
从上到下的顺序
package com.book.common.base.constant;
/**
* 常量类
* @author Administrator
*
*/
public class BaseConstant {
/** 通用状态 -- 启用 */
public static final int STATUS_ENABLED = 1;
/** 通用状态 -- 禁用 */
public static final int STATUS_DISABELD = 0;
/** 返回状态--成功 */
public static final int STATUS_SUCCESS = 0;
/** 返回状态--失败 */
public static final int STATUS_FAILURE = 1;
public static int getStatusEnabled() {
return STATUS_ENABLED;
}
public static int getStatusDisabeld() {
return STATUS_DISABELD;
}
public static int getStatusSuccess() {
return STATUS_SUCCESS;
}
public static int getStatusFailure() {
return STATUS_FAILURE;
}
}
package com.book.common.base.dao;
import java.util.List;
import java.util.Map;
public abstract interface MyBatisBaseDao
{
public abstract <P> int insert(P paramP);
public abstract <P> int update(P paramP);
public abstract <P> int delete(P paramP);
public abstract <T, P> T selectObject(P paramP);
public abstract <T, P> List<T> selectObjectList(P paramP);
public abstract <K, V, P> Map<K, V> selectMap(P paramP);
public abstract <K, V, P> List<Map<K, V>> selectMapList(P paramP);
public abstract <T, P> List<T> page(P paramP);
public abstract <P> int pageCount(P paramP);
}
package com.book.common.base.page;
import java.util.Collection;
import com.book.model.obj.AbstractObject;
public class Page<E> extends AbstractObject
{
private int page;
private int pageSize;
private long count;
public Collection<E> data;
public Page(int pageIndex, int pageSize, long rowCount, Collection<E> data)
{
this.page = pageIndex;
this.pageSize = pageSize;
this.count = rowCount;
this.data = data;
}
public int getPage() {
return this.page;
}
public void setPage(int page) {
this.page = page;
}
public int getPageSize() {
return this.pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getCount() {
return this.count;
}
public void setCount(long count) {
this.count = count;
}
public Collection<E> getData() {
return this.data;
}
public void setData(Collection<E> data) {
this.data = data;
}
}
package com.book.common.base.service;
import java.util.List;
import java.util.Map;
import com.book.common.base.page.Page;
public abstract interface MybatisBaseService
{
public abstract <P> int insert(P paramP);
public abstract <P> int update(P paramP);
public abstract <P> int delete(P paramP);
public abstract <T, P> T selectObject(P paramP);
public abstract <T, P> List<T> selectObjectList(P paramP);
public abstract <V, P> Map<String, V> selectMap(P paramP);
public abstract <V, P> List<Map<String, V>> selectMapList(P paramP);
public abstract <T> Page<T> page(Map<String, Object> paramMap, int paramInt1, int paramInt2);
public abstract <T> Page<T> page(String paramString1, String paramString2, Map<String, Object> paramMap, int paramInt1, int paramInt2);
}
package com.book.common.base.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.book.common.base.dao.MyBatisBaseDao;
import com.book.common.base.page.Page;
import com.book.common.base.service.MybatisBaseService;
public abstract class MybatisBaseServiceImpl implements MybatisBaseService
{
private static final Logger LOGGER = LoggerFactory.getLogger(MybatisBaseServiceImpl.class);
public static int type = 1;
public static final int MYSQL = 1;
public static final int ORACLE = 2;
public abstract MyBatisBaseDao getDao();
public <T> int insert(T parameter)
{
return getDao().insert(parameter);
}
public <T> int update(T parameter)
{
return getDao().update(parameter);
}
public <T> int delete(T parameter)
{
return getDao().delete(parameter);
}
public <T, P> T selectObject(P parameter)
{
return getDao().selectObject(parameter);
}
public <T, P> List<T> selectObjectList(P parameter)
{
return getDao().selectObjectList(parameter);
}
public <V, P> Map<String, V> selectMap(P parameter)
{
return getDao().selectMap(parameter);
}
public <V, P> List<Map<String, V>> selectMapList(P parameter)
{
return getDao().selectMapList(parameter);
}
public <T> Page<T> page(Map<String, Object> map, int pageIndex, int pageSize)
{
int count = getDao().pageCount(map);
int offset = (pageIndex - 1) * pageSize;
if (type == 1) {
map.put("offset", Integer.valueOf(offset));
map.put("rows", Integer.valueOf(pageSize));
} else if (type == 2) {
map.put("begin", Integer.valueOf(offset));
map.put("end", Integer.valueOf(offset + pageSize));
}
List list = getDao().page(map);
return new Page(pageIndex, pageSize, count, list);
}
public <T> Page<T> page(String pageSelectId, String pageCountSelectId, Map<String, Object> map, int pageIndex, int pageSize)
{
MyBatisBaseDao dao = getDao();
int count = 0;
Class clazz = dao.getClass();
Method method = null;
try {
method = clazz.getDeclaredMethod(pageCountSelectId, new Class[] { Map.class });
} catch (NoSuchMethodException e1) {
LOGGER.error("分页查询【记录数】异常,请检查【{}.{}(Map<String,Object> map)】方法是否存在!", new Object[] { dao.getClass().getName(), pageCountSelectId });
}
catch (SecurityException e1) {
e1.printStackTrace();
}
try
{
try {
count = ((Integer)method.invoke(dao, new Object[] { map })).intValue();
} catch (ClassCastException e) {
LOGGER.error("分页查询【记录数】异常,【{}.{}(Map<String,Object> map)】方法的返回值不是int类型!", new Object[] { dao.getClass().getName(), pageCountSelectId });
}
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
LOGGER.error("分页查询【记录数】异常,请检查Mapper.xml文件中是否有对应 select id 【{}】!", new Object[] { pageCountSelectId });
}
int offset = (pageIndex - 1) * pageSize;
if (type == 1) {
map.put("offset", Integer.valueOf(offset));
map.put("rows", Integer.valueOf(pageSize));
} else if (type == 2) {
map.put("begin", Integer.valueOf(offset));
map.put("end", Integer.valueOf(offset + pageSize));
}
List list = null;
Class clazz2 = dao.getClass();
Method method2 = null;
try {
method2 = clazz2.getDeclaredMethod(pageSelectId, new Class[] { Map.class });
} catch (NoSuchMethodException e) {
LOGGER.error("分页查询【记录数】异常,请检查【{}.{}(Map<String,Object> map)】方法是否存在!", new Object[] { dao.getClass().getName(), pageSelectId });
}
catch (SecurityException e) {
e.printStackTrace();
}
try
{
try {
list = (ArrayList)method2.invoke(dao, new Object[] { map });
} catch (ClassCastException e) {
LOGGER.error("分页查询【记录数】异常,【{}.{}(Map<String,Object> map)】方法的返回值不是List类型!", new Object[] { dao.getClass().getName(), pageSelectId });
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
LOGGER.error("分页查询【数据集】异常,请检查Mapper.xml文件中是否有对应 select id 【{}】!", new Object[] { pageSelectId });
}
return new Page(pageIndex, pageSize, count, list);
}
public String dbThreadName()
{
String threadName = "DB_" + getClass().getSimpleName().replace("ServiceImpl", "").toUpperCase() + "_THREAD";
return threadName;
}
}
package com.book.common.base.to;
import com.book.common.base.constant.BaseConstant;
/**
*
* @Description: TODO 结果统一返回
* @date 2017年6月4日
* @author haoran
*/
public class ResultTO {
private static final long serialVersionUID = 1L;
//返回状态 0成功 1失败 2token过期,状态统一使用BaseConstant里面的STATUS_SUCCESS、STATUS_FAILURE和STATUS_TOKEN_INVALID
private int status;
//附加消息
private String msg;
//数据体
private Object data;
public ResultTO() {
}
private ResultTO(int status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
/**
* 成功,且无特殊附加消息时使用
* @param data
* @return
* @return ResultTO
*/
public static ResultTO newSuccessResultTO(Object data) {
return ResultTO.newSuccessResultTO("success", data);
}
/**
* 失败,且无特殊附加消息时使用
* @param data
* @return
* @return ResultTO
*/
public static ResultTO newFailResultTO(Object data) {
return ResultTO.newSuccessResultTO("failure", data);
}
/**
* 成功 时候使用
* @param msg
* @param data
* @return
* @return ResultTO
*/
public static ResultTO newSuccessResultTO(String msg, Object data) {
return new ResultTO(BaseConstant.STATUS_SUCCESS, msg, data);
}
/**
* 失败时候使用
* @param msg
* @param data
* @return
* @return ResultTO
*/
public static ResultTO newFailResultTO(String msg, Object data) {
return new ResultTO(BaseConstant.STATUS_FAILURE, msg, data);
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String error) {
this.msg = error;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package com.book.common.dao.file;
import com.book.common.base.dao.MyBatisBaseDao;
public interface FileStoreDao extends MyBatisBaseDao{
}
package com.book.common.service.file;
import com.book.common.base.service.MybatisBaseService;
/**
* 【】 服务类 接口
*
* @date 2017-03-25 14:24:55
*
*/
public interface FileStoreService extends MybatisBaseService {
}
package com.book.common;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CommonApplication {
public static void main(String[] args) {
SpringApplication.run(CommonApplication.class, args);
}
}
common的 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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.book</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>common</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- model -->
<dependency>
<groupId>com.book</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service 模块的目录图
service 代码
package com.book.server.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan("com.book.common.dao.*")
@ImportResource("classpath:spring/spring-tx.xml")
public class DataSourceConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceConfiguration.class);
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/mapper/*/*.xml"));
LOGGER.info("数据库初始化完成");
return sqlSessionFactoryBean.getObject();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean(name="transactionManager")
public DataSourceTransactionManager transactionManager() {
LOGGER.info("切入事务完成");
return new DataSourceTransactionManager(dataSource());
}
}
package com.book.server.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.rpc.Exporter;
@Configuration
@ConditionalOnClass(Exporter.class)
@ImportResource("classpath:dubbo/dubbo-provider.xml")
public class DubboConfiguration {
}
package com.book.server.service.impl.file;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.book.common.base.dao.MyBatisBaseDao;
import com.book.common.base.service.impl.MybatisBaseServiceImpl;
import com.book.common.dao.file.FileStoreDao;
import com.book.common.service.file.FileStoreService;
/**
* 【】 服务类 实现类
*
* @date 2017-03-25 14:24:55
*
*/
@Service(value = "fileStoreService")
public class FileStoreServiceImpl extends MybatisBaseServiceImpl implements FileStoreService {
private static final Logger LOGGER = LoggerFactory.getLogger(FileStoreServiceImpl.class);
@Autowired
private FileStoreDao fileStoreDao;
@Override
public MyBatisBaseDao getDao() {
return fileStoreDao;
}
}
package com.book.server;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.book.common.service.file.FileStoreService;
@RestController
@SpringBootApplication
public class ServiceApplication {
@Autowired
private FileStoreService fileStoreService;
@RequestMapping("/")
List<Object> home() {
Map<String,Object> map=new HashMap<String,Object>();
map.put("fileId", 2);
return fileStoreService.selectObjectList(map);
}
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
配置文件目录结构
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
<dubbo:application name="kshop-haoran" />
<dubbo:provider delay="-1" timeout="10000" retries="0"/>
<dubbo:registry address="zookeeper://106.14.77.86:2181" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- **************************** 系统Service ******************************* -->
<!-- 测试接口 Service -->
<dubbo:service interface="com.book.common.service.file.FileStoreService" ref="fileStoreService" version="1.1.0"/>
</beans>
<?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">
<!--
【】对象关系映射 配置
@date 2017-03-25 17:55:49
-->
<!-- namespace必须指向Dao接口 -->
<mapper namespace="com.book.common.dao.file.FileStoreDao">
<resultMap id="fileStoreResultMap" type="com.book.model.file.FileStore">
<id property="fileId" column="file_id" />
<result property="groupId" column="group_id" />
<result property="filePath" column="file_path" />
<result property="fileName" column="file_name" />
<result property="fileExtName" column="file_ext_name" />
<result property="fileSize" column="file_size" />
<result property="createDate" column="create_date" />
<result property="createUser" column="create_user" />
<result property="lastUpdateDate" column="last_update_date" />
<result property="lastUpdateUser" column="last_update_user" />
<result property="deleteFlag" column="delete_flag" />
</resultMap>
<resultMap id="mapResultMap" type="java.util.HashMap" extends="fileStoreResultMap"></resultMap>
<resultMap id="pageResultMap" type="java.util.HashMap" extends="fileStoreResultMap"></resultMap>
<insert id="insert" parameterType="com.book.model.file.FileStore" useGeneratedKeys="true" keyProperty="fileId" >
insert into tb_file_store (
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
)values (
#{fileId},
#{groupId},
#{filePath},
#{fileName},
#{fileExtName},
#{fileSize},
#{createDate},
#{createUser},
#{lastUpdateDate},
#{lastUpdateUser},
#{deleteFlag}
)
</insert>
<update id="update" parameterType="com.book.model.file.FileStore">
update tb_file_store
<set>
<if test="fileId != null">
file_id=#{fileId},
</if>
<if test="groupId != null">
group_id=#{groupId},
</if>
<if test="filePath != null">
file_path=#{filePath},
</if>
<if test="fileName != null">
file_name=#{fileName},
</if>
<if test="fileExtName != null">
file_ext_name=#{fileExtName},
</if>
<if test="fileSize != null">
file_size=#{fileSize},
</if>
<if test="createDate != null">
create_date=#{createDate},
</if>
<if test="createUser != null">
create_user=#{createUser},
</if>
<if test="lastUpdateDate != null">
last_update_date=#{lastUpdateDate},
</if>
<if test="lastUpdateUser != null">
last_update_user=#{lastUpdateUser},
</if>
<if test="deleteFlag != null">
delete_flag=#{deleteFlag},
</if>
</set>
<where>
file_id=#{fileId}
</where>
</update>
<delete id="delete" parameterType="map">
delete from
tb_file_store
where
file_id=#{fileId}
</delete>
<select id="selectObject" parameterType="map" resultMap="fileStoreResultMap">
select
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
from tb_file_store
where
file_id=#{fileId}
</select>
<select id="selectObjectList" parameterType="map" resultMap="fileStoreResultMap">
select
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
from tb_file_store
<where>
<if test="fileId != null">
file_id=#{fileId}
</if>
<if test="groupId != null">
and group_id=#{groupId}
</if>
<if test="filePath != null">
and file_path=#{filePath}
</if>
<if test="fileName != null">
and file_name=#{fileName}
</if>
<if test="fileExtName != null">
and file_ext_name=#{fileExtName}
</if>
<if test="fileSize != null">
and file_size=#{fileSize}
</if>
<if test="createDate != null">
and create_date=#{createDate}
</if>
<if test="createUser != null">
and create_user=#{createUser}
</if>
<if test="lastUpdateDate != null">
and last_update_date=#{lastUpdateDate}
</if>
<if test="lastUpdateUser != null">
and last_update_user=#{lastUpdateUser}
</if>
<if test="deleteFlag != null">
and delete_flag=#{deleteFlag}
</if>
</where>
</select>
<select id="selectMap" parameterType="map" resultMap="mapResultMap">
select
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
from tb_file_store
where
file_id=#{fileId}
</select>
<select id="selectMapList" parameterType="map" resultMap="mapResultMap">
select
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
from tb_file_store
<where>
<if test="fileId != null">
file_id=#{fileId}
</if>
<if test="groupId != null">
and group_id=#{groupId}
</if>
<if test="filePath != null">
and file_path=#{filePath}
</if>
<if test="fileName != null">
and file_name=#{fileName}
</if>
<if test="fileExtName != null">
and file_ext_name=#{fileExtName}
</if>
<if test="fileSize != null">
and file_size=#{fileSize}
</if>
<if test="createDate != null">
and create_date=#{createDate}
</if>
<if test="createUser != null">
and create_user=#{createUser}
</if>
<if test="lastUpdateDate != null">
and last_update_date=#{lastUpdateDate}
</if>
<if test="lastUpdateUser != null">
and last_update_user=#{lastUpdateUser}
</if>
<if test="deleteFlag != null">
and delete_flag=#{deleteFlag}
</if>
</where>
</select>
<select id="page" parameterType="map" resultMap="pageResultMap">
select
file_id,
group_id,
file_path,
file_name,
file_ext_name,
file_size,
create_date,
create_user,
last_update_date,
last_update_user,
delete_flag
<include refid="pageCondition" />
limit #{offset},#{rows}
</select>
<select id="pageCount" parameterType="map" resultType="int">
select count(1)
<include refid="pageCondition" />
</select>
<sql id="pageCondition">
from tb_file_store
<where>
<if test="fileId != null">
file_id=#{fileId}
</if>
<if test="groupId != null">
and group_id=#{groupId}
</if>
<if test="filePath != null">
and file_path=#{filePath}
</if>
<if test="fileName != null">
and file_name=#{fileName}
</if>
<if test="fileExtName != null">
and file_ext_name=#{fileExtName}
</if>
<if test="fileSize != null">
and file_size=#{fileSize}
</if>
<if test="createDate != null">
and create_date=#{createDate}
</if>
<if test="createUser != null">
and create_user=#{createUser}
</if>
<if test="lastUpdateDate != null">
and last_update_date=#{lastUpdateDate}
</if>
<if test="lastUpdateUser != null">
and last_update_user=#{lastUpdateUser}
</if>
<if test="deleteFlag != null">
and delete_flag=#{deleteFlag}
</if>
</where>
</sql>
</mapper>
spring 切面事务
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
"
>
<!-- 2、配置事物属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="change*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="modify*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="cancel*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="confirm*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置事物切入点,以及把事物切入点和事物属性关联起来 -->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.book.service.impl.*.*.*(..))" />
<aop:advisor pointcut-ref="txPointCut" advice-ref="txAdvice"/>
</aop:config>
</beans>
#port
server.port=8080
#datasource
spring.datasource.url=jdbc:MySQL://IP:3306/filedata
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
log配置
log4j.rootLogger = dubug,console,file
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold = dubug
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%-d{yyyy-MM-dd HH:mm:ss}] - [ %p ] %m%n
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File =../logs/service/server.log
log4j.appender.file.Append = true
log4j.appender.file.Threshold = dubug
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = [%-d{yyyy-MM-dd HH:mm:ss}]-[ %p ] %c %m%n
mybatis-config.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="logImpl" value="LOG4J"/>
<setting name="cacheEnabled" value="true" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
<setting name="defaultStatementTimeout" value="25000" />
</settings>
</configuration>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.book</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<commonslang.version>2.6</commonslang.version>
<spring.boot.starter.log4j.version>1.3.8.RELEASE</spring.boot.starter.log4j.version>
<cglib.version>3.2.4</cglib.version>
<dubbo.version>2.5.3</dubbo.version>
<zkclient.version>0.1</zkclient.version>
</properties>
<dependencies>
<!-- common -->
<dependency>
<groupId>com.book</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- web log4j-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>${spring.boot.starter.log4j.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- druid 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.15</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller模块
代码
package com.book.api.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.rpc.Invoker;
@Configuration
@ConditionalOnClass(Invoker.class)
@ImportResource("classpath:dubbo/dubbo-customer.xml")
public class DubboConfiguration {
}
package com.book.api.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.book.common.base.page.Page;
import com.book.common.base.to.ResultTO;
import com.book.common.service.file.FileStoreService;
import com.book.model.file.FileStore;
/**
*
* @Description: TODO 测试 rest
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author haoran
*/
@RestController
@RequestMapping("test/*")
public class TestController {
@Autowired
private FileStoreService fileStoreService;
/**
* 查询所有
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author Administrator
*/
@RequestMapping(value = "/list",method = RequestMethod.GET)
public ResultTO list(){
Map<String,Object> map=new HashMap<String,Object>();
List<FileStore> fileStoreList= fileStoreService.selectObjectList(map);
return ResultTO.newSuccessResultTO("成功",fileStoreList);
}
/**
* 分页查询列表
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author Administrator
*/
@RequestMapping(value = "/page",method = RequestMethod.GET)
public ResultTO page(@RequestParam int pageIndex,@RequestParam int pageSize){
Map<String,Object> map=new HashMap<String,Object>();
Page<FileStore> fileStoreList= fileStoreService.page(map, pageIndex, pageSize);
return ResultTO.newSuccessResultTO("成功",fileStoreList);
}
/**
* 单条查询
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author Administrator
*/
@RequestMapping(value = "/{fileId}",method = RequestMethod.GET)
public ResultTO queryById(@PathVariable("fileId")String fileId){
Map<String,Object> map=new HashMap<String,Object>();
map.put("fileId", fileId);
FileStore fileStore = fileStoreService.selectObject(map);
return ResultTO.newSuccessResultTO("成功",fileStore);
}
/**
* 添加
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author Administrator
*/
@RequestMapping(value = "/add",method = RequestMethod.POST)
public ResultTO add(@RequestBody FileStore fileStore){
int count=fileStoreService.insert(fileStore);
if(count>0){
return ResultTO.newSuccessResultTO("成功");
}else{
return ResultTO.newSuccessResultTO("失败");
}
}
/**
* 删除
* @Description: TODO(这里用一句话描述这个方法的作用)
* @return ResultTO 返回类型
* @date 2017年6月4日
* @author Administrator
*/
@RequestMapping(value = "/{fileId}",method = RequestMethod.DELETE)
public ResultTO del(@PathVariable("fileId")String fileId){
int count=fileStoreService.delete(fileId);
if(count>0){
return ResultTO.newSuccessResultTO("成功");
}else{
return ResultTO.newSuccessResultTO("失败");
}
}
}
package com.book.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ControllerApplication {
public static void main(String[] args) {
SpringApplication.run(ControllerApplication.class, args);
}
}
消费者配置文件
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="kshop-haoran"/>
<dubbo:registry address="zookeeper://IP:2181" />
<!-- 消费者配置 -->
<!-- ******************************** 系统Service ************************ -->
<!-- 测试Service -->
<dubbo:reference id="fileStoreService" interface="com.book.common.service.file.FileStoreService" version="1.1.0"/>
</beans>
application.properties
server.port=8081
pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<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.book</groupId>
<artifactId>controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>controller</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo.version>2.5.3</dubbo.version>
<zkclient.version>0.1</zkclient.version>
</properties>
<dependencies>
<!-- common -->
<dependency>
<groupId>com.book</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后是启动项目
先打包model ,再打包 common,再跑生产端 main方法 ,最后消费端main方法,启动的时候可以上dubbo-admin管理中心查看是否已经注册
下面是运行截图
项目启动以后 访问生产者端 http://localhost:8081/test/list 可以看到生产者调用消费者接口返回的数据,消费者不依赖于生产者
如果不足的地方欢迎修正