以上是项目接口图,由于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文件
4.0.0
com.book
model
0.0.1-SNAPSHOT
jar
model
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
UTF-8
UTF-8
1.8
2.6
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
commons-lang
commons-lang
${commonslang.version}
org.springframework.boot
spring-boot-maven-plugin
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 int insert(P paramP);
public abstract
int update(P paramP);
public abstract
int delete(P paramP);
public abstract T selectObject(P paramP);
public abstract List selectObjectList(P paramP);
public abstract Map selectMap(P paramP);
public abstract List
package com.book.common.base.page;
import java.util.Collection;
import com.book.model.obj.AbstractObject;
public class Page extends AbstractObject
{
private int page;
private int pageSize;
private long count;
public Collection data;
public Page(int pageIndex, int pageSize, long rowCount, Collection 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 getData() {
return this.data;
}
public void setData(Collection 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 int insert(P paramP);
public abstract
int update(P paramP);
public abstract
int delete(P paramP);
public abstract T selectObject(P paramP);
public abstract List selectObjectList(P paramP);
public abstract Map selectMap(P paramP);
public abstract List
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 int insert(T parameter)
{
return getDao().insert(parameter);
}
public int update(T parameter)
{
return getDao().update(parameter);
}
public int delete(T parameter)
{
return getDao().delete(parameter);
}
public T selectObject(P parameter)
{
return getDao().selectObject(parameter);
}
public List selectObjectList(P parameter)
{
return getDao().selectObjectList(parameter);
}
public Map selectMap(P parameter)
{
return getDao().selectMap(parameter);
}
public List
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文件
4.0.0
com.book
common
0.0.1-SNAPSHOT
jar
common
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
UTF-8
UTF-8
1.8
com.book
model
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
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
配置文件目录结构
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}
)
update tb_file_store
file_id=#{fileId},
group_id=#{groupId},
file_path=#{filePath},
file_name=#{fileName},
file_ext_name=#{fileExtName},
file_size=#{fileSize},
create_date=#{createDate},
create_user=#{createUser},
last_update_date=#{lastUpdateDate},
last_update_user=#{lastUpdateUser},
delete_flag=#{deleteFlag},
file_id=#{fileId}
delete from
tb_file_store
where
file_id=#{fileId}
from tb_file_store
file_id=#{fileId}
and group_id=#{groupId}
and file_path=#{filePath}
and file_name=#{fileName}
and file_ext_name=#{fileExtName}
and file_size=#{fileSize}
and create_date=#{createDate}
and create_user=#{createUser}
and last_update_date=#{lastUpdateDate}
and last_update_user=#{lastUpdateUser}
and delete_flag=#{deleteFlag}
spring 切面事务
#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
pom.xml
4.0.0
com.book
service
0.0.1-SNAPSHOT
service
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
UTF-8
UTF-8
1.8
2.6
1.3.8.RELEASE
3.2.4
2.5.3
0.1
com.book
common
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
${spring.boot.starter.log4j.version}
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
druid
1.0.15
org.aspectj
aspectjweaver
cglib
cglib
${cglib.version}
com.alibaba
dubbo
${dubbo.version}
org.springframework
spring
org.slf4j
slf4j-api
com.github.sgroschupf
zkclient
${zkclient.version}
org.springframework.boot
spring-boot-maven-plugin
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 map=new HashMap();
List 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 map=new HashMap();
Page 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 map=new HashMap();
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);
}
}
消费者配置文件
application.properties
server.port=8081
pom.xml依赖
4.0.0
com.book
controller
0.0.1-SNAPSHOT
jar
controller
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
UTF-8
UTF-8
1.8
2.5.3
0.1
com.book
common
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
com.alibaba
dubbo
${dubbo.version}
org.springframework
spring
org.slf4j
slf4j-api
com.github.sgroschupf
zkclient
${zkclient.version}
org.springframework.boot
spring-boot-maven-plugin
然后是启动项目
先打包model ,再打包 common,再跑生产端 main方法 ,最后消费端main方法,启动的时候可以上dubbo-admin管理中心查看是否已经注册
下面是运行截图
项目启动以后 访问生产者端 http://localhost:8081/test/list 可以看到生产者调用消费者接口返回的数据,消费者不依赖于生产者
如果不足的地方欢迎修正