首先新建一个简单的数据表,通过操作这个数据表来进行演示
|
1
2
3
4
5
6
7
8
|
DROP TABLE IF EXISTS `items`;
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`detail` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
|
引入JdbcTemplate的maven依赖及连接类
|
1
2
3
4
5
6
7
8
9
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
|
在application.properties文件配置mysql的驱动类,数据库地址,数据库账号、密码信息,application.properties新建在src/main/resource文件夹下
|
1
2
3
4
5
6
7
8
9
10
11
|
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
|
新建一个实体类,属性对应sql字段
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package org.amuxia.start;
public class Items {
private Integer id;
private String title;
private String name;
private String detail;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public Items() {
super();
// TODO Auto-generated constructor stub
}
public Items(Integer id, String title, String name, String detail) {
super();
this.id = id;
this.title = title;
this.name = name;
this.detail = detail;
}
@Override
public String toString() {
return "Items [id=" + id + ", id=\"codetool\">
|



