上一篇对springboot有一个简单的介绍以及使用一个简单的例子熟悉了一下springboot写法的简单流程。这一篇介绍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