基于maven的SpringBoot实例工程
包含内容: 源码,全套工具
作者QQ1420527913
本实例使用eclipse mars工具基于maven搭建的一个springboot项目, 本项目分为三层,第一层提供接口,供外部调用, 第二层为mybatis层,实现对数据库的增删改查(数据库为oracle); 第三层为工具层,提供常用算法类,常用数据类型处理类和通讯处理类; springboot的分页使用了pagehelper
本项目配置环境: eclipse mars; jdk1.8; oracle11g
1. 外部接口层:
实例代码对外提供web路径 /queryStudent, 供用户根据用户studentId查询指定的学生
参数类型为 json格式, 代码执行过程中,使用日志打印进行业务数据的跟踪处理
/**
* 查询学生
* http://localhost:9901/slmloan/extinter/queryStudent {"studentId":"1"}
Content-Type: application/json;charset=UTF-8
* @param inJson
* @return
*/
@ResponseBody
@RequestMapping(value = "/queryStudent")
public JSONObject queryStudent(@RequestBody JSONObject inJson) throws SlmloanException {
JSONObject rtn = new JSONObject();
try {
logger.info(inJson, "查询学生信息,请求报文=" + inJson.toJSONString());
StudentDto dto=inJson.toJavaObject(StudentDto.class);
rtn=service.queryStudent(dto);
//获取还款记录查询结果集
logger.info(inJson, "查询学生信息end,返回报文=" + rtn.toJSONString());
} catch (Exception e) {
logger.error(inJson, "出错", e);
throw new SlmloanException("出错,具体错误信息:" + e.getMessage());
}
return rtn;
}定义一个Application类,springboot的启动只需启动此类的main函数即可
@RestController
@RequestMapping("/slmloan/extinter")
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
/*@EnableDiscoveryClient*/
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}2. mybatis数据库业务处理
定义一个StudentMapper.java类和一个同名的xml文件StudentMapper.xml
数据库的分页使用pagehelper实现,只需传递相应的页数和每页显示的条数,便能自动实现分页查询
StudentMapper.java:
@Mapper
public interface StudentMapper extends BaseMapper<StudentDto> {
/**
* 根据学生id查找学生信息
* @param dto
* @return
*/
public StudentDto getStudentById(StudentDto dto);
/**
* 根据学生信息查找学生列表信息
* @param dto
* @return
*/
public List<StudentDto> getStudentList(StudentDto dto);
}StudentMapper.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.jsjn.slmloan.mybatis.mapper.StudentMapper">
<resultMap id="studentResultMap" type="StudentDto" >
<result column="student_id" property="studentId" jdbcType="VARCHAR" />
<result column="student_name" property="studentName" jdbcType="VARCHAR" />
<result column="telephone" property="telephone" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
</resultMap>
<select id="getStudentById" parameterType="StudentDto" resultMap="studentResultMap">
select a.student_id,
a.student_name,
a.telephone,
a.address
from student a
where 1 = 1
<if test="studentId !=null and studentId !=''">
and a.student_id = #{studentId}
</if>
</select>
<select id="getStudentList" parameterType="StudentDto" resultMap="studentResultMap">
select a.student_id,
a.student_name,
a.telephone,
a.address
from student a
</select>
</mapper>创建oracle数据库表, 建立十条数据
