使用jdbcTemplate操作数据库
spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习成本低,毕竟是jdbc的基础知识。
在pom.xml中添加jdbc模块和mysql数据库驱动
<!--jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
application.yml
spring boot的jdbc模块会加载以下参数,并且根据url可以识别并自动加载mysql驱动,自动创建数据库实例,自动实现连接池。
server:
port: 8088
servlet:
context-path: /
spring:
datasource:
url: jdbc:mysql://localhost:3306/david2018_db?characterEncoding=utf8
username: root
password: 1234
创建t_user表
CREATE TABLE `david2018_db`.`t_user` ( `id` INT NOT NULL AUTO_INCREMENT, `username` VARCHAR(45) NULL, `password` VARCHAR(45) NULL, PRIMARY KEY (`id`));
t_user.java
package com.spring.boot.bean; public class t_user { private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } private String username; private String password; }
t_userDao.java
jdbc模块还会自动创建一个JdbcTemplate实例,可以在程序中直接注入使用,下面的dao实现了两个方法:
update方法:执行增删改操作
queryForList方法:执行查询操作
params:任意数量的数组,配置sql中的?占位符
package com.spring.boot.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class t_userDao { @Autowired JdbcTemplate jdbcTemplate; public int update(){ String sql = "update t_user set username = ? where id = ?"; Object[] params = new Object[]{"boot",1}; return jdbcTemplate.update(sql,params); } public List queryForList(){ String sql = "select * from t_user"; return jdbcTemplate.queryForList(sql); } }
helloService.java
spring boot同样自动配置好了事务,在service上 直接加一个注解即可。
package com.spring.boot.service; import com.spring.boot.dao.t_userDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Transactional //开启事务 @Service public class t_userService { @Autowired private t_userDao dao; public int update() { return dao.update(); } public List queryForList() { return dao.queryForList(); } }
t_userController.java
package com.spring.boot.controller; import com.spring.boot.bean.t_user; import com.spring.boot.service.t_userService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RequestMapping("/t_user") @RestController public class t_userController { @Autowired private t_userService service; @GetMapping("/update") public String update(){ service.update(); return "update"; } @GetMapping("/list") public List<t_user> list(){ return service.queryForList(); } }
输入 http://localhost:8088/t_user/list list 进行测试