查询模块
项目目录结构
1、新建maven工程:
Group Id:org.apache.maven.archetypes
Artifact Id:maven-archetype-quickstart
Version:1.1
2、引入依赖:
pom.xml:
<groupId>pers.zhentao</groupId>
<artifactId>QueryRecord</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>QueryRecord</name>
<url>http://maven.apache.org</url>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
3、lambda函数输入DTO
Record.java
/**
* 通讯录实体类
*
* @author zhangzhentao1995@163.com
* 2017-10-30
*/
public class Record {
private Integer addressBookId;
private String name;
private String phoneNumber;
private String address;
private String telephoneNumber;
private String note;
private Date createTime;
private Integer createdBy;
//getters and setters...
}
4、lambda函数输出DTO
ResponseData.java
/**
* 返回消息
*
* @author zhangzhentao1995@163.com
* 2017-10-30
*/
public class ResponseData {
public static final Integer RESPONSE_CODE_SUCCESS = 1000;
public static final Integer RESPONSE_CODE_ERROR = 1001;
public static final Integer RESPONSE_CODE_SYS_EXCEPTION = 1002;
private Integer code;
private String message;
private List<Record> rows;
private Map<Object, Object> map;
public ResponseData(Integer code, String message) {
this.code = code;
this.message = message;
}
public ResponseData(Integer code, List<Record> rows) {
this.rows = rows;
this.code = code;
}
//getters and setters...
}
5、lambda处理程序
LambdaFunctionHandler.java
/**
* Lambda处理程序
*
* @author zhangzhentao1995@163.com
* 2017-10-30
*/
public class LambdaFunctionHandler implements RequestHandler<Record, ResponseData> {
public ResponseData handleRequest(Record input, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("[" + new Date() + "]Query:" + JSONObject.toJSONString(input));
IQueryRecordService service = new QueryRecordServiceImpl();
try {
List<Record> list = service.queryAll();
return new ResponseData(ResponseData.RESPONSE_CODE_SUCCESS,list);
} catch (QueryRecordException e) {
return new ResponseData(ResponseData.RESPONSE_CODE_SYS_EXCEPTION, e.getMsg());
}
}
}
6、查询service接口
IQueryRecordService.java
/**
* 查询服务接口
*
* @author zhangzhentao1995@163.com
* 2017-10-30
*/
public interface IQueryRecordService {
/**
* 查询全部
* @return
*/
public List<Record> queryAll() throws QueryRecordException;
}
7、service实现类
QueryRecordServiceImpl.java
public class QueryRecordServiceImpl implements IQueryRecordService {
/**
* RDS数据库配置信息
*/
private final String driver = "com.mysql.cj.jdbc.Driver";
private String url = null;
private String username = null;
private String password = null;
private final String QUERY_SQL = "select * from address_book";
@Override
public List<Record> queryAll() throws QueryRecordException {
url = System.getenv("DB_URL");
username = System.getenv("USERNAME");
password = System.getenv("PASSWORD");
if(url == null || username == null || password == null) {
throw new QueryRecordException(QueryRecordException.SYS_ERROR_CODE, "db config is null.");
}
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new QueryRecordException(QueryRecordException.SYS_ERROR_CODE, "mysql driver not found.");
}
Connection conn;
try {
conn = DriverManager.getConnection(url, username, password);
PreparedStatement statement = conn.prepareStatement(QUERY_SQL);
ResultSet result = statement.executeQuery();
List<Record> list = new ArrayList<>();
while (result.next()) {
Record record = new Record();
record.setAddressBookId(result.getInt(1));
record.setName(result.getString(2));
record.setPhoneNumber(result.getString(3));
record.setAddress(result.getString(4));
record.setTelephoneNumber(result.getString(5));
record.setNote(result.getString(6));
record.setCreateTime(result.getDate(7));
record.setCreatedBy(result.getInt(8));
list.add(record);
}
conn.close();
return list;
} catch (SQLException e) {
throw new QueryRecordException(QueryRecordException.DB_ERROR_CODE, e.getMessage());
}
}
}
8、查询异常类
QueryRecordException.java
/**
* 查询异常类
*
* @author zhangzhentao1995@163.com
* 2017-10-30
*/
public class QueryRecordException extends Exception {
private static final long serialVersionUID = -3483738766858412487L;
public static final Integer INFO_ERROR_CODE = 1001;
public static final Integer SYS_ERROR_CODE = 2001;
public static final Integer DB_ERROR_CODE = 2002;
private Integer code;
private String msg;
public QueryRecordException(String message) {
super(message);
}
public QueryRecordException(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
//getters and setters...
}
9、maven打包
在项目根目录下,打开命令行,执行命令:
mvn package
执行完成后,项目target文件夹下会出现jar包:XXXX-0.0.1-SNAPSHOT.jar
10、创建Lambda函数,配置处理程序路径,配置环境变量
11、配置测试事件,因为代码逻辑为查询全部,所以这里输入DTO传与不传都一样
12、测试结果
至此,查询模块完成