springboot整合spring jdbc
目录结构
x
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
<modelVersion>4.0.0</modelVersion>
5
<parent>
6
<groupId>org.springframework.boot</groupId>
7
<artifactId>spring-boot-starter-parent</artifactId>
8
<version>2.4.2</version>
9
<relativePath/> <!-- lookup parent from repository -->
10
</parent>
11
<groupId>com.example</groupId>
12
<artifactId>springboot-jdbc</artifactId>
13
<version>0.0.1-SNAPSHOT</version>
14
<name>springboot-jdbc</name>
15
<description>Demo project for Spring Boot</description>
16
<properties>
17
<java.version>1.8</java.version>
18
</properties>
19
<dependencies>
20
<dependency>
21
<groupId>org.springframework.boot</groupId>
22
<artifactId>spring-boot-starter-web</artifactId>
23
</dependency>
24
25
<dependency>
26
<groupId>org.springframework.boot</groupId>
27
<artifactId>spring-boot-starter-test</artifactId>
28
<scope>test</scope>
29
</dependency>
30
31
<!--lombok用来简化实体类:需要安装lombok插件-->
32
<dependency>
33
<groupId>org.projectlombok</groupId>
34
<artifactId>lombok</artifactId>
35
</dependency>
36
37
<!--SpringBoot 添加jdbc支持-->
38
<dependency>
39
<groupId>org.springframework.boot</groupId>
40
<artifactId>spring-boot-starter-jdbc</artifactId>
41
</dependency>
42
43
<dependency>
44
<groupId>com.alibaba</groupId>
45
<artifactId>fastjson</artifactId>
46
<version>1.2.28</version>
47
</dependency>
48
49
<!-- 引入本地oracle6 jar包(因oracle收费,maven无法直接引入oracle jar)-->
50
<dependency>
51
<groupId>com.oracle</groupId>
52
<artifactId>ojdbc6</artifactId>
53
<version>11.2.0.1.0</version>
54
<scope>system</scope>
55
<systemPath>${project.basedir}/src/main/resources/lib/ojdbc6.jar</systemPath>
56
</dependency>
57
58
</dependencies>
59
60
<build>
61
<plugins>
62
<plugin>
63
<groupId>org.springframework.boot</groupId>
64
<artifactId>spring-boot-maven-plugin</artifactId>
65
<configuration>
66
<includeSystemScope>true</includeSystemScope>
67
</configuration>
68
</plugin>
69
<!--修改 maven plugin 版本-->
70
<plugin>
71
<groupId>org.apache.maven.plugins</groupId>
72
<artifactId>maven-resources-plugin</artifactId>
73
<version>3.1.0</version>
74
</plugin>
75
<!--添加maven配置跳过测试-->
76
<plugin>
77
<groupId>org.apache.maven.plugins</groupId>
78
<artifactId>maven-surefire-plugin</artifactId>
79
<configuration>
80
<skipTests>true</skipTests>
81
</configuration>
82
</plugin>
83
</plugins>
84
85
</build>
86
87
</project>
88
application.properties基础配置
1
13
1
#设置当前应用的名称
2
#spring.application.name=springboot
3
#配置tomcat端口号
4
server.port=8888
5
6
#多环境配置,测试,生产用不同的配置文件
7
#spring.profiles.active=test
8
# 数据源oracle
9
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
10
spring.datasource.username=gdkjbm
11
spring.datasource.password=eams
12
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
13
控制层
1
51
1
package com.springboot.controller;
2
3
import com.springboot.service.ExamInfoService;
4
import com.springboot.util.R;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.web.bind.annotation.GetMapping;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RestController;
9
10
import java.util.List;
11
import java.util.Map;
12
13
@RestController
14
@RequestMapping("/test")
15
public class IndexController {
16
17
@Autowired
18
private ExamInfoService examInfoService;
19
20
@GetMapping("/ok")
21
public R test1(){
22
return R.ok();
23
}
24
25
@GetMapping("/error")
26
public R test2(){
27
return R.error();
28
}
29
30
/**
31
* 返回 list Map
32
* @return
33
*/
34
@GetMapping("/list")
35
public R list(){
36
List<Map<String, Object>> list = examInfoService.getExamInfoAll();
37
return R.ok().data("list", list);
38
}
39
40
/**
41
* 返回 Map
42
* @return
43
*/
44
@GetMapping("/map")
45
public R map(){
46
List<Map<String, Object>> list = examInfoService.getExamInfoAll();
47
Map<String, Object> map = list.get(0);
48
return R.ok().data(map);
49
}
50
51
}
业务层(service)
1
51
1
package com.springboot.controller;
2
3
import com.springboot.service.ExamInfoService;
4
import com.springboot.util.R;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.web.bind.annotation.GetMapping;
7
import org.springframework.web.bind.annotation.RequestMapping;
8
import org.springframework.web.bind.annotation.RestController;
9
10
import java.util.List;
11
import java.util.Map;
12
13
@RestController
14
@RequestMapping("/test")
15
public class IndexController {
16
17
@Autowired
18
private ExamInfoService examInfoService;
19
20
@GetMapping("/ok")
21
public R test1(){
22
return R.ok();
23
}
24
25
@GetMapping("/error")
26
public R test2(){
27
return R.error();
28
}
29
30
/**
31
* 返回 list Map
32
* @return
33
*/
34
@GetMapping("/list")
35
public R list(){
36
List<Map<String, Object>> list = examInfoService.getExamInfoAll();
37
return R.ok().data("list", list);
38
}
39
40
/**
41
* 返回 Map
42
* @return
43
*/
44
@GetMapping("/map")
45
public R map(){
46
List<Map<String, Object>> list = examInfoService.getExamInfoAll();
47
Map<String, Object> map = list.get(0);
48
return R.ok().data(map);
49
}
50
51
}
1
15
1
package com.springboot.service;
2
3
import com.springboot.entity.ExamInfo;
4
5
import java.util.List;
6
import java.util.Map;
7
8
public interface ExamInfoService {
9
10
List<Map<String, Object>> getExamInfoAll();
11
12
ExamInfo getExamInfo(String uuid);
13
14
}
15
持久层(dao)
1
33
1
package com.springboot.dao.impl;
2
3
import com.springboot.dao.ExamInfoDao;
4
import com.springboot.entity.ExamInfo;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.jdbc.core.JdbcTemplate;
7
import org.springframework.stereotype.Repository;
8
9
import java.util.List;
10
import java.util.Map;
11
12
@Repository
13
public class ExamInfoDaoImpl implements ExamInfoDao {
14
15
// 注入JdbcTemplate对象
16
@Autowired
17
private JdbcTemplate jdbcTemplate;
18
19
@Override
20
public List<Map<String, Object>> getExamInfoAll() {
21
String sql = " select * from exam_info ";
22
// 返回列表,这里需要注意的是,返回的Map其中的每个entry封装了列名以及对应的值
23
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
24
return maps;
25
}
26
27
@Override
28
public ExamInfo getExamInfo(String uuid) {
29
return null;
30
}
31
32
}
33
1
15
1
package com.springboot.dao;
2
3
import com.springboot.entity.ExamInfo;
4
5
import java.util.List;
6
import java.util.Map;
7
8
public interface ExamInfoDao {
9
10
List<Map<String, Object>> getExamInfoAll();
11
12
ExamInfo getExamInfo(String uuid);
13
14
}
15
实体类
1
134
1
package com.springboot.entity;
2
3
import java.util.Date;
4
5
public class ExamInfo {
6
7
private String uuid;
8
9
private String examYear;
10
11
private String examBatchCode;
12
13
private String examCategoryCode;
14
15
private String examCategoryName;
16
17
private String examDate;
18
19
private String examTime;
20
21
private String examPlace;
22
23
private String examAddress;
24
25
private String creator;
26
27
private Date createTime;
28
29
private String notes;
30
31
@Override
32
public String toString() {
33
return "ExamInfo [uuid=" + uuid + ", examYear=" + examYear + ", examBatchCode=" + examBatchCode
34
+ ", examCategoryCode=" + examCategoryCode + ", examCategoryName=" + examCategoryName + ", examDate="
35
+ examDate + ", examTime=" + examTime + ", examPlace=" + examPlace + ", examAddress=" + examAddress
36
+ ", creator=" + creator + ", createTime=" + createTime + ", notes=" + notes + "]";
37
}
38
39
public String getUuid() {
40
return uuid;
41
}
42
43
public void setUuid(String uuid) {
44
this.uuid = uuid == null ? null : uuid.trim();
45
}
46
47
public String getExamYear() {
48
return examYear;
49
}
50
51
public void setExamYear(String examYear) {
52
this.examYear = examYear == null ? null : examYear.trim();
53
}
54
55
public String getExamBatchCode() {
56
return examBatchCode;
57
}
58
59
public void setExamBatchCode(String examBatchCode) {
60
this.examBatchCode = examBatchCode == null ? null : examBatchCode.trim();
61
}
62
63
public String getExamCategoryCode() {
64
return examCategoryCode;
65
}
66
67
public void setExamCategoryCode(String examCategoryCode) {
68
this.examCategoryCode = examCategoryCode == null ? null : examCategoryCode.trim();
69
}
70
71
public String getExamCategoryName() {
72
return examCategoryName;
73
}
74
75
public void setExamCategoryName(String examCategoryName) {
76
this.examCategoryName = examCategoryName == null ? null : examCategoryName.trim();
77
}
78
79
public String getExamDate() {
80
return examDate;
81
}
82
83
public void setExamDate(String examDate) {
84
this.examDate = examDate == null ? null : examDate.trim();
85
}
86
87
public String getExamTime() {
88
return examTime;
89
}
90
91
public void setExamTime(String examTime) {
92
this.examTime = examTime == null ? null : examTime.trim();
93
}
94
95
public String getExamPlace() {
96
return examPlace;
97
}
98
99
public void setExamPlace(String examPlace) {
100
this.examPlace = examPlace == null ? null : examPlace.trim();
101
}
102
103
public String getExamAddress() {
104
return examAddress;
105
}
106
107
public void setExamAddress(String examAddress) {
108
this.examAddress = examAddress == null ? null : examAddress.trim();
109
}
110
111
public String getCreator() {
112
return creator;
113
}
114
115
public void setCreator(String creator) {
116
this.creator = creator == null ? null : creator.trim();
117
}
118
119
public Date getCreateTime() {
120
return createTime;
121
}
122
123
public void setCreateTime(Date createTime) {
124
this.createTime = createTime;
125
}
126
127
public String getNotes() {
128
return notes;
129
}
130
131
public void setNotes(String notes) {
132
this.notes = notes == null ? null : notes.trim();
133
}
134
}
工具类
1
66
1
package com.springboot.util;
2
3
import lombok.Data;
4
5
import java.util.HashMap;
6
import java.util.Map;
7
j
8
//统一返回结果的类
9
@Data
10
public class R {
11
12
private Boolean success;
13
14
private Integer code;
15
16
private String message;
17
18
private Map<String, Object> data = new HashMap<String, Object>();
19
20
//把构造方法私有
21
private R() {}
22
23
//成功静态方法
24
public static R ok() {
25
R r = new R();
26
r.setSuccess(true);
27
r.setCode(20000);
28
r.setMessage("成功");
29
return r;
30
}
31
32
//失败静态方法
33
public static R error() {
34
R r = new R();
35
r.setSuccess(false);
36
r.setCode(20001);
37
r.setMessage("失败");
38
return r;
39
}
40
41
public R success(Boolean success){
42
this.setSuccess(success);
43
return this;
44
}
45
46
public R message(String message){
47
this.setMessage(message);
48
return this;
49
}
50
51
public R code(Integer code){
52
this.setCode(code);
53
return this;
54
}
55
56
public R data(String key, Object value){
57
this.data.put(key, value);
58
return this;
59
}
60
61
public R data(Map<String, Object> map){
62
this.setData(map);
63
return this;
64
}
65
}
66
1
23
1
package com.springboot.util;
2
3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import org.springframework.http.HttpStatus;
5
import org.springframework.http.MediaType;
6
7
import javax.servlet.http.HttpServletResponse;
8
import java.io.IOException;
9
10
public class ResponseUtil {
11
12
public static void out(HttpServletResponse response, R r) {
13
ObjectMapper mapper = new ObjectMapper();
14
response.setStatus(HttpStatus.OK.value());
15
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
16
try {
17
mapper.writeValue(response.getWriter(), r);
18
} catch (IOException e) {
19
e.printStackTrace();
20
}
21
}
22
}
23