JDBC模板(JdbcTemplate)是Spring对数据库的操作在JDBC基础上做了封装,建立了一个JDBC存取框架。在Spring Boot应用中,如果使用JdbcTemplate操作数据库,那么只需在pom.xml文件中添加spring-boot-starter-jdbc模块,即可通过@Autowired注解依赖注入JdbcTemplate对象,然后调用JdbcTemplate提供的方法操作数据库。
使用JdbcTemplate操作数据库。 具体实现步骤如下。 1.创建Spring Boot Web应用 2.修改pom.xml文件 3.设置Web应用ch6_5的上下文路径及数据源配置信息 4.创建实体类 5.创建数据访问层 6.创建业务层 7.创建控制器类MyUserController 8.运行
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.JDBC</groupId> <artifactId>SpringBootJdbcTemplate</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!-- 声明项目配置依赖编码格式为 utf-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <fastjson.version>1.2.24</fastjson.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加MySQL依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.13</version><!--$NO-MVN-MAN-VER$ --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 添加jdbc依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
server.port=8089 server.servlet.context-path=/ch6_5 spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?serverTimezone=UTC&autoReconnect=true spring.datasource.username=root spring.datasource.password=admin spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=MYSQL spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jackson.serialization.indent-output=true
package com.ch.ch6_5.entity; public class MyUser { private Integer id; private String username; private String password; 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; } }
package com.ch.ch6_5.repository; import java.util.List; import com.ch.ch6_5.entity.MyUser; public interface MyUserRepository { public int saveUser(MyUser myUser); public int deleteUser(Integer id); public int updateUser(MyUser myUser); public List<MyUser> findAll(); public MyUser findUserById(Integer id); }
package com.ch.ch6_5.repository; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import com.ch.ch6_5.entity.MyUser; @Repository public class MyUserRepositoryImpl implements MyUserRepository { @Autowired private JdbcTemplate jdbcTemplate; @Override public int saveUser(MyUser myUser) { String sql = "insert into user (username, password) values (?,?)"; Object args[] = { myUser.getUsername(), myUser.getPassword() }; return jdbcTemplate.update(sql, args); } @Override public int deleteUser(Integer id) { String sql = "delete from user where id = ? "; Object args[] = { id }; return jdbcTemplate.update(sql, args); } @Override public int updateUser(MyUser myUser) { String sql = "update user set username = ?, password = ? where id = ? "; Object args[] = { myUser.getUsername(), myUser.getPassword(), myUser.getId() }; return jdbcTemplate.update(sql, args); } @Override public List<MyUser> findAll() { String sql = "select * from user "; // 定义一个RowMapper RowMapper<MyUser> rowMapper = new BeanPropertyRowMapper<MyUser>(MyUser.class); return jdbcTemplate.query(sql, rowMapper); } @Override public MyUser findUserById(Integer id) { String sql = "select * from user where id = ? "; Object args[] = { id }; // 定义一个RowMapper RowMapper<MyUser> rowMapper = new BeanPropertyRowMapper<MyUser>(MyUser.class); return jdbcTemplate.queryForObject(sql, args, rowMapper); } }
package com.ch.ch6_5.service; import java.util.List; import com.ch.ch6_5.entity.MyUser; public interface MyUserService { public int saveUser(MyUser myUser); public int deleteUser(Integer id); public int updateUser(MyUser myUser); public List<MyUser> findAll(); public MyUser findUserById(Integer id); }
package com.ch.ch6_5.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ch.ch6_5.entity.MyUser; import com.ch.ch6_5.repository.MyUserRepository; @Service public class MyUserServiceImpl implements MyUserService { @Autowired private MyUserRepository myUserRepository; @Override public int saveUser(MyUser myUser) { return myUserRepository.saveUser(myUser); } @Override public int deleteUser(Integer id) { return myUserRepository.deleteUser(id); } @Override public int updateUser(MyUser myUser) { return myUserRepository.updateUser(myUser); } @Override public List<MyUser> findAll() { return myUserRepository.findAll(); } @Override public MyUser findUserById(Integer id) { return myUserRepository.findUserById(id); } }
package com.ch.ch6_5.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ch.ch6_5.entity.MyUser; import com.ch.ch6_5.service.MyUserService; @RestController public class MyUserController { @Autowired private MyUserService myUserService; @RequestMapping("/saveUser") public int saveUser(MyUser myUser) { return myUserService.saveUser(myUser); } @RequestMapping("/deleteUser") public int deleteUser(Integer id) { return myUserService.deleteUser(id); } @RequestMapping("/updateUser") public int updateUser(MyUser myUser) { return myUserService.updateUser(myUser); } @RequestMapping("/findAll") public List<MyUser> findAll() { return myUserService.findAll(); } @RequestMapping("/findUserById") public MyUser findUserById(Integer id) { return myUserService.findUserById(id); } }
package com.ch.ch6_5; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Ch65Application { public static void main(String[] args) { SpringApplication.run(Ch65Application.class, args); } }