Spring Boot +mybatis +oracle开发restful风格的查询接口
程序员文章站
2024-02-16 10:41:52
...
Spring Boot +mybatis +oracle开发restful风格的查询接口
第一步:创建基础工程
1、在网站 https://start.spring.io/ 根据自己的需要创建基础工程。
第二步:将工程导入idea,然后等待idea下载工程需要的maven依赖
第三步:搭建项目的基础架构
1、创建分类的文件夹
文件夹简介:
bean:存放对象的实体类文件;
dao:存放各种与数据库映射的类
service:返回数据给控制调用
web:控制器,存放各种控制器,来提供数据或者返回界面
mapper:存放sql配置文件
mybatis:存放mybatis配置文件
application.yml:基础配置文件
第四步:各部分代码编写
1、实体类编写 (bean目录下文件)
package com.test.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
*
* Title: User
* Description:用户实体类
* Version:1.0.0
* @author zhangchao
* @date 2020年8月19日
*/
public class User implements Serializable {
/** 证件号码 */
private String certnum;
/** 对比相似度 */
private String similarity;
/** 操作工号 */
private String operid;
/** 来源系统 */
private String origdomain;
/** 操作时间 */
@CreatedDate
@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss")
private Date updatetime;
public User(){
}
/**
* 构造方法
* @param certnum 证件号码
* @param updatetime 操作时间
*/
public User(String certnum, Date updatetime,String similarity,String operid,String origdomain) {
super();
this.certnum = certnum;
this.updatetime = updatetime;
this.similarity = similarity;
this.operid = operid;
this.origdomain = origdomain;
}
public String getCertnum() {
return certnum;
}
public void setCertnum(String certnum) {
this.certnum = certnum;
}
public String getSimilarity() {
return similarity;
}
public void setSimilarity(String similarity) {
this.similarity = similarity;
}
public String getOperid() {
return operid;
}
public void setOperid(String operid) {
this.operid = operid;
}
public String getOrigdomain() {
return origdomain;
}
public void setOrigdomain(String origdomain) {
this.origdomain = origdomain;
}
public Date getUpdatetime() {
return updatetime;
}
public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
}
@Override
public String toString(){
return "Publicsecurityresult{" +
"certnum='" + certnum + '\'' +
", similarty='" + similarity + '\'' +
", operid=" + operid +
", origgomain='" + origdomain + '\'' +
", update_time='" + updatetime +'\'' +
'}';
}
}
2、dao层编写 (dao目录下)
package com.test.dao;
import com.test.bean.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* Title:UserDao
* Description:用户数据处理接口
* @author zhangchao
* @date 2020/8/20
*/
@Mapper
public interface UserDao{
/***
* 根据名称查询用户信息
*/
public User findByCertnum(@Param("certnum") String certnum, @Param("updatetime") String updatetime);
/**
* 查询所有
*/
public List<User> findAll();
}
3、service层编写 (service目录下)
1、service:
2、IMPL
package com.test.service;
import com.test.bean.User;
import org.springframework.stereotype.Service;
import java.util.List;
/***
*
* Title:UserService
* Description:查询接口
* @author zhangchao
* @date 2020/8/20
*
*/
@Service
public interface UserService{
/**
* 根据证件号码查询
* @param certnum,update_time
* @return
*/
public User findinfoByCertnum(String certnum, String updatetime);
/**
* 查询所有
* @return
*
*/
public List<User> findAll();
}
package com.test.service;
import com.test.bean.User;
import com.test.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Title:UserServiceImpl
* Description:用户操作实现类
* @author zhangchao
* @date 2020/8/19
*
*/
@Component
@Service
public class UserServiceImplimplements UserService{
@Autowired
private UserDao userdao;
@Override
public User findinfoByCertnum(String certnum, String updatetime) {
return userdao.findByCertnum(certnum,updatetime);
}
@Override
public List<User> findAll() {
return userdao.findAll();
}
}
4、web层 (web目录下)
package com.test.web;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.test.service.UserService;
import com.test.bean.User;
/**
* Title:com.test.web.UserController
* Description:控制类
* @author zhangchao
* @date 2020/8/19
*/
@RestController
@RequestMapping(value = "/api")
public class UserController {
@Autowired
private UserService userservice;
@PostMapping("/user")
public JSONObject findinfoByCertnum(@RequestBody JSONObject jsonParam){
String certnum = jsonParam.get("certnum").toString();
String updatetime = jsonParam.get("updatetime").toString();
System.out.println(certnum);
System.out.println(updatetime);
System.out.println("开始根据证件号码进行查找~");
JSONObject result = new JSONObject();
if (null == certnum ||null == updatetime) {
result.put("return_code","9999");
result.put("return_msg", "传入证件号码或者日期为空");
return result;
}
Object userinfo = userservice.findinfoByCertnum(certnum,updatetime);
try {
if (null == userinfo) {
result.put("return_code", "9999");
result.put("return_msg", "查询信息为空");
}
result.put("return_code", "0");
result.put("return_msg", "查询成功");
result.put("data", userinfo);
}catch (Exception e){
result.put("return_code","-1");
result.put("return_msg", "方法异常,请重试!");
e.printStackTrace();
}
return result;
}
@PostMapping("/findall")
public JSONObject findAll() {
System.out.println("开始查询全部信息~");
JSONObject result = new JSONObject();
try {
result.put("return_code", "0");
result.put("return_msg", "查询成功");
result.put("data", userservice.findAll());
}catch (Exception e){
result.put("return_code","-1");
result.put("return_msg", "方法异常,请重试");
e.printStackTrace();
}
return result;
}
}
5、mapper文件编写 (mapper目录下)
<?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.test.dao.UserDao">
<select id="findByCertnum" resultType="com.test.bean.User">
(SQL语句)
</select>
<select id="findAll" resultType="com.test.bean.User">
(SQL语句)
</select>
</mapper>
6、mybatis文件编写 (mybatis目录下)
<?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="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
7、application.yml文件
server:
#t容器端口配置
port: 8083
context-path: /springboot
spring:
datasource:
#数据源基本配置
username: root
password: root
url: jdbc:mysql://localhost:3306/new_schema?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
# driver-class-name: oracle.jdbc.driver.OracleDriver
max-idle: 10
max-wait: 1000
min-idle: 5
initial-size: 5
output.ansi.enabled: always
mybatis:
config-location: classpath:mybatis/mybatis.xml
mapper-locations: classpath:mapper/mapper.xml
第五步:测试
1、先测试findbycertnum
2、findall
到这里,一个restful风格的查询接口就写完了,有些数据较为保密,所以打码了,请见谅哈!
源码就不放了,差不多都在这了,如果有问题可以评论留言哈~
新人小白,如果存在不足请大神指正,互相学习,谢谢大家