mirror of
https://github.com/wahyd4/Martian.git
synced 2026-08-09 05:16:21 +10:00
添加示例
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
1. 这个模块是一个示例,跟本项目并无关系,只是用本框架开发一个示例,供大家参考。
|
||||
2. 由于是一个示例,所以模块的划分没有按照文档上推荐的来,而是用包来划分的模块,大家千万不要被带偏哦。
|
||||
|
||||
1. This module is an example and has nothing to do with this project. It is just an example developed with this framework for your reference.
|
||||
2. Because it is an example, the division of the modules is not according to the recommendations in the documentation, but the modules divided by the package.
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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.mars.example</groupId>
|
||||
<artifactId>mars-example</artifactId>
|
||||
<version>3.0.19</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.yuyenews</groupId>
|
||||
<artifactId>mars-starter</artifactId>
|
||||
<version>3.0.19</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!--
|
||||
由于这个模块是一个示例,所以不需要deploy到中央库。
|
||||
实际开发中,下面这个配置项,你并不需要
|
||||
-->
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.mars.exp;
|
||||
|
||||
import com.mars.exp.config.ExpConfig;
|
||||
import com.mars.start.StartMars;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
*/
|
||||
public class ExpStart {
|
||||
|
||||
public static void main(String[] args) {
|
||||
StartMars.start(ExpStart.class,new ExpConfig());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.mars.exp.api;
|
||||
|
||||
import com.mars.common.annotation.api.MarsApi;
|
||||
import com.mars.common.annotation.api.RequestMethod;
|
||||
import com.mars.common.annotation.enums.ReqMethod;
|
||||
import com.mars.exp.api.vo.ExpVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@MarsApi(refBean = "expApiService")
|
||||
public interface ExpApi {
|
||||
|
||||
/**
|
||||
* get请求示例
|
||||
* http://127.0.0.1:8080/expGetRequest?name=张三&names=王五&names=赵六
|
||||
* @param expVO
|
||||
* @return
|
||||
*/
|
||||
List<ExpVO> expGetRequest(ExpVO expVO);
|
||||
|
||||
/**
|
||||
* post请求示例
|
||||
* http://127.0.0.1:8080/expPostRequest
|
||||
* 传参用表单,不支持json传参
|
||||
* @param expVO
|
||||
* @return
|
||||
*/
|
||||
@RequestMethod(ReqMethod.POST)
|
||||
List<ExpVO> expPostRequest(ExpVO expVO);
|
||||
|
||||
/**
|
||||
* 文件上传示例
|
||||
* http://127.0.0.1:8080/expUploadRequest
|
||||
* 传参用formData,不支持json传参
|
||||
* @param expVO
|
||||
* @return
|
||||
*/
|
||||
@RequestMethod(ReqMethod.POST)
|
||||
String expUploadRequest(ExpVO expVO);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.mars.exp.api.inters;
|
||||
|
||||
import com.mars.common.annotation.api.MarsInterceptor;
|
||||
import com.mars.mvc.base.BaseInterceptor;
|
||||
import com.mars.server.server.request.HttpMarsRequest;
|
||||
import com.mars.server.server.request.HttpMarsResponse;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 拦截所有exp开头的api
|
||||
*/
|
||||
@MarsInterceptor(pattern = "/exp*")
|
||||
public class ExpInter implements BaseInterceptor {
|
||||
|
||||
/**
|
||||
* api执行前
|
||||
* @param request
|
||||
* @param response
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Object beforeRequest(HttpMarsRequest request, HttpMarsResponse response) {
|
||||
System.out.println("开始执行拦截器");
|
||||
// 返回SUCCESS表示通过,如果不通过的话,直接返回对象或者提示信息,前端会收到
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* api执行后
|
||||
* @param request
|
||||
* @param response
|
||||
* @param obj 控制层返回的数据
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Object afterRequest(HttpMarsRequest request, HttpMarsResponse response, Object obj) {
|
||||
System.out.println("执行拦截器结束");
|
||||
// 返回SUCCESS表示通过,如果不通过的话,直接返回对象或者提示信息,前端会收到
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 有些被拦截规则拦截了的api,但是这个拦截器又不打算处理,
|
||||
* 可以添加到List中,并在这个方法里返回
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<String> exclude() {
|
||||
// 这两个api直接通过,不拦截
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("/expPostRequest");
|
||||
list.add("/expUploadRequest");
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.mars.exp.api.vo;
|
||||
|
||||
import com.mars.common.annotation.api.MarsDataCheck;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
|
||||
/**
|
||||
* 所有实体类都支持lombok,这里为了兼容所有的环境,就还是采用的原始的get,set
|
||||
*/
|
||||
public class ExpVO {
|
||||
|
||||
@MarsDataCheck(notNull = true, msg = "名称不可以为空")
|
||||
private String name;
|
||||
|
||||
@MarsDataCheck(notNull = true, msg = "名称们不可以为空")
|
||||
private String[] names;
|
||||
|
||||
private MarsFileUpLoad marsFileUpLoad;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String[] getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public void setNames(String[] names) {
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
public MarsFileUpLoad getMarsFileUpLoad() {
|
||||
return marsFileUpLoad;
|
||||
}
|
||||
|
||||
public void setMarsFileUpLoad(MarsFileUpLoad marsFileUpLoad) {
|
||||
this.marsFileUpLoad = marsFileUpLoad;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.mars.exp.config;
|
||||
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 更多配置项,请到官网查看官方文档
|
||||
*/
|
||||
public class ExpConfig extends MarsConfig {
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Properties> jdbcProperties() {
|
||||
List<Properties> list = new ArrayList<>();
|
||||
|
||||
// 用的是阿里巴巴的 druid数据源,其他属性可自行查阅
|
||||
Properties properties = new Properties();
|
||||
properties.put("name","dataSource");
|
||||
properties.put("url","");
|
||||
properties.put("username","");
|
||||
properties.put("password","");
|
||||
properties.put("driverClassName","com.mysql.jdbc.Driver");
|
||||
|
||||
list.add(properties);
|
||||
|
||||
// 如果要多个数据源,add多个到list即可
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.mars.exp.core.aop;
|
||||
|
||||
import com.mars.aop.base.BaseAop;
|
||||
|
||||
/**
|
||||
* AOP示例
|
||||
*/
|
||||
public class ExpAop implements BaseAop {
|
||||
|
||||
/**
|
||||
* 被监听的方法执行前执行这个方法
|
||||
* @param args 被监听的方法的参数
|
||||
*/
|
||||
@Override
|
||||
public void startMethod(Object[] args) {
|
||||
System.out.println("开始执行AOP");
|
||||
}
|
||||
|
||||
/**
|
||||
* 被监听的方法执行结束后执行这个方法
|
||||
* @param args 被监听的方法的参数
|
||||
* @param result 被监听方法的返回值
|
||||
*/
|
||||
@Override
|
||||
public void endMethod(Object[] args, Object result) {
|
||||
System.out.println("执行AOP结束");
|
||||
}
|
||||
|
||||
/**
|
||||
* 被监听的方法出异常了,执行这个方法
|
||||
* @param e 异常
|
||||
*/
|
||||
@Override
|
||||
public void exp(Throwable e) {
|
||||
System.out.println("方法出异常了");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.mars.exp.core.dao;
|
||||
|
||||
import com.mars.common.annotation.jdbc.MarsDao;
|
||||
import com.mars.exp.core.dto.ExpDTO;
|
||||
import com.mars.jdbc.annotation.DataSource;
|
||||
import com.mars.jdbc.annotation.MarsGet;
|
||||
import com.mars.jdbc.annotation.MarsSelect;
|
||||
import com.mars.jdbc.annotation.MarsUpdate;
|
||||
import com.mars.jdbc.annotation.enums.OperType;
|
||||
import com.mars.jdbc.helper.model.PageModel;
|
||||
import com.mars.jdbc.helper.model.PageParamModel;
|
||||
import com.mars.jdbc.helper.templete.JdbcTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@MarsDao
|
||||
public abstract class ExpDAO {
|
||||
|
||||
/**
|
||||
* 根据主键查询一条数据
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@MarsGet(tableName = "数据库表名",primaryKey = "id")
|
||||
public abstract ExpDTO selectById(int id);
|
||||
|
||||
/**
|
||||
* 插入一条数据
|
||||
* @param expDTO
|
||||
* @return
|
||||
*/
|
||||
@MarsUpdate(tableName = "数据库表名",operType = OperType.INSERT)
|
||||
public abstract int insert(ExpDTO expDTO);
|
||||
|
||||
/**
|
||||
* 根据主键删除一条数据
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@MarsUpdate(tableName = "数据库表名",operType = OperType.DELETE,primaryKey = "id")
|
||||
public abstract int delete(int id);
|
||||
|
||||
/**
|
||||
* 根据主键更新一条数据
|
||||
* @param expDTO
|
||||
* @return
|
||||
*/
|
||||
@MarsUpdate(tableName = "数据库表名",operType = OperType.UPDATE,primaryKey = "id")
|
||||
public abstract int update(ExpDTO expDTO);
|
||||
|
||||
/**
|
||||
* sql语句固定的查询
|
||||
* #{} 占位符,无sql注入风险
|
||||
* ${} 字符串拼接,有sql注入风险
|
||||
* @param expDTO
|
||||
* @return
|
||||
*/
|
||||
@DataSource("数据源name,不传默认采用第一个")//如果要采用默认数据源的话,这个注解可以不配
|
||||
@MarsSelect(sql = "select a,b,c from 表名 where a=#{a} and b=${b}",resultType = ExpDTO.class)
|
||||
public abstract List<ExpDTO> selectList(ExpDTO expDTO);
|
||||
|
||||
/**
|
||||
* sql语句固定的分页查询
|
||||
* @param pageParamModel
|
||||
* @return
|
||||
*/
|
||||
@MarsSelect(sql = "select a,b,c from 表名 where a=#{a} and b=${b}",resultType = ExpDTO.class,page = true)
|
||||
public abstract PageModel<ExpDTO> selectPageList(PageParamModel pageParamModel);
|
||||
|
||||
/**
|
||||
* sql语句不固定的查询
|
||||
* @param expDTO
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public List<ExpDTO> selectList2(ExpDTO expDTO) throws Exception {
|
||||
StringBuffer sql = new StringBuffer();
|
||||
|
||||
// 这里根据expDTO里的参数,判断,然后拼接sql就好了
|
||||
|
||||
return JdbcTemplate.get("数据源name,不传默认采用第一个").selectList(sql.toString(),expDTO,ExpDTO.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* sql语句不固定的分页查询
|
||||
* @param expDTO
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public PageModel<ExpDTO> selectPageList2(ExpDTO expDTO) throws Exception {
|
||||
StringBuffer sql = new StringBuffer();
|
||||
|
||||
// 这里根据expDTO里的参数,判断,然后拼接sql就好了
|
||||
|
||||
// 创建传参,实际开发中,这个对象需要从外面传进来,因为page和pageSize是活的,不能写死
|
||||
PageParamModel pageParamModel = PageParamModel.getPageParamModel(1,100);
|
||||
pageParamModel.setParam(expDTO);
|
||||
return JdbcTemplate.get().selectPageList(sql.toString(),pageParamModel,ExpDTO.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.mars.exp.core.dto;
|
||||
|
||||
/**
|
||||
* 这里面尽量用包装器类型,比如用Integer不用int,
|
||||
* 因为包装器类型可以赋值为null,这样如果不需要被sql用到的时候,可以赋值为null,免被带入到sql
|
||||
*/
|
||||
public class ExpDTO {
|
||||
|
||||
/**
|
||||
* 这里的字段名必须跟查询语句中的字段名 一模一样(如果取了别名就跟别名一样)
|
||||
* 如果是增删改操作,并且你用的是框架自带的方法,那么必须跟数据表的字段名一模一样
|
||||
* 如果数据库是user_name 那么这里也必须是user_name,不可以是userName
|
||||
*/
|
||||
private Integer id;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.mars.exp.core.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mars.common.annotation.bean.MarsAop;
|
||||
import com.mars.common.annotation.bean.MarsBean;
|
||||
import com.mars.common.annotation.bean.MarsWrite;
|
||||
import com.mars.exp.api.ExpApi;
|
||||
import com.mars.exp.api.vo.ExpVO;
|
||||
import com.mars.exp.core.aop.ExpAop;
|
||||
import com.mars.exp.core.dao.ExpDAO;
|
||||
import com.mars.server.server.request.model.MarsFileUpLoad;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* service层
|
||||
*/
|
||||
@MarsBean("expApiService")
|
||||
public class ExpApiService implements ExpApi {
|
||||
|
||||
/**
|
||||
* 为了让大家可以快速的跑起来,所以本示例没有连接数据库
|
||||
* 所以自然也就不会调用dao的方法了,这里注入进来的,只是为了演示IOC的用法
|
||||
*/
|
||||
@MarsWrite
|
||||
private ExpDAO expDAO;
|
||||
|
||||
/**
|
||||
* 这个方法上加了aop监听
|
||||
* @param expVO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@MarsAop(className = ExpAop.class)
|
||||
public List<ExpVO> expGetRequest(ExpVO expVO) {
|
||||
// 打印expDAO,如果不为null就说明已经注入了
|
||||
System.out.println(expDAO);
|
||||
|
||||
// 打印接收到的参数,看是否接收成功
|
||||
System.out.println(expVO.getName());
|
||||
System.out.println(JSON.toJSONString(expVO.getNames()));
|
||||
|
||||
// 返回数据
|
||||
return getExpResultData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ExpVO> expPostRequest(ExpVO expVO) {
|
||||
// 打印expDAO,如果不为null就说明已经注入了
|
||||
System.out.println(expDAO);
|
||||
|
||||
// 打印接收到的参数,看是否接收成功
|
||||
System.out.println(expVO.getName());
|
||||
System.out.println(JSON.toJSONString(expVO.getNames()));
|
||||
|
||||
// 返回数据
|
||||
return getExpResultData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String expUploadRequest(ExpVO expVO) {
|
||||
// 打印expDAO,如果不为null就说明已经注入了
|
||||
System.out.println(expDAO);
|
||||
|
||||
// 打印接收到的参数,看是否接收成功
|
||||
MarsFileUpLoad marsFileUpLoad = expVO.getMarsFileUpLoad();
|
||||
if(marsFileUpLoad == null){
|
||||
return "上传失败";
|
||||
}
|
||||
System.out.println(marsFileUpLoad.getFileName());
|
||||
System.out.println(marsFileUpLoad.getInputStream());
|
||||
|
||||
return "上传成功";
|
||||
}
|
||||
|
||||
/**
|
||||
* 这是把返回数据写死了,不然你们还得搭环境,连接数据库
|
||||
* @return
|
||||
*/
|
||||
private List<ExpVO> getExpResultData(){
|
||||
List<ExpVO> list = new ArrayList<>();
|
||||
|
||||
ExpVO exp = new ExpVO();
|
||||
exp.setName("hello world");
|
||||
list.add(exp);
|
||||
|
||||
exp = new ExpVO();
|
||||
exp.setName("The world dies");
|
||||
list.add(exp);
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.mars.exp.test;
|
||||
|
||||
import com.mars.common.annotation.bean.MarsWrite;
|
||||
import com.mars.common.annotation.junit.MarsTest;
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
import com.mars.exp.ExpStart;
|
||||
import com.mars.exp.api.vo.ExpVO;
|
||||
import com.mars.exp.core.service.ExpApiService;
|
||||
import com.mars.junit.MarsJunit;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 单元测试
|
||||
*/
|
||||
@MarsTest(startClass = ExpStart.class)
|
||||
public class ExpApiServiceTest extends MarsJunit {
|
||||
|
||||
@MarsWrite("expApiService")
|
||||
private ExpApiService expApiService;
|
||||
|
||||
/**
|
||||
* 返回配置文件
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public MarsConfig getMarsConfig() {
|
||||
return new ExpTestConfig();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpPostRequest(){
|
||||
ExpVO expVO = new ExpVO();
|
||||
expVO.setName("单测");
|
||||
expVO.setNames(new String[]{"单测1","单测2"});
|
||||
List<ExpVO> list = expApiService.expPostRequest(expVO);
|
||||
|
||||
// 这只是一个示例,演示一下单测怎么用,所以就偷个懒直接打印了,实际是要对返回值做校验的
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.mars.exp.test;
|
||||
|
||||
import com.mars.common.base.config.MarsConfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 更多配置项,请到官网查看官方文档
|
||||
*/
|
||||
public class ExpTestConfig extends MarsConfig {
|
||||
|
||||
/**
|
||||
* 配置数据源
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Properties> jdbcProperties() {
|
||||
List<Properties> list = new ArrayList<>();
|
||||
|
||||
// 用的是阿里巴巴的 druid数据源,其他属性可自行查阅
|
||||
Properties properties = new Properties();
|
||||
properties.put("name","dataSource");
|
||||
properties.put("url","");
|
||||
properties.put("username","");
|
||||
properties.put("password","");
|
||||
properties.put("driverClassName","com.mysql.jdbc.Driver");
|
||||
|
||||
list.add(properties);
|
||||
|
||||
// 如果要多个数据源,add多个到list即可
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user