springboot使用使用JdbcTemplate完成对数据库的增删改查功能完成对数据库的增删改查功能
主要介绍了springboot使用JdbcTemplate完成对数据库的增删改查功能,需要的朋友可以参考下
首先新建一个简单的数据表,通过操作这个数据表来进行演示
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依赖及连接类
<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文件夹下
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字段
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
}
评论0