概述
Spring JDBC Template 是 Spring Framework 提供的一个简化 JDBC 操作的模板类。它封装了一些常见的 JDBC 操作,使得开发者在使用 JDBC 时能够更加便捷、简洁,同时也提供了异常处理和资源管理等功能。
导入pom 使用C3P0作为数据源
<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>org.example</groupId><artifactId>spring-jdbc-demo1</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging> <name>spring-jdbc-demo1</name><url>http://maven.apache.org</url> <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties> <dependencies> <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>6.0.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>6.0.13</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/c3p0/c3p0 --><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!-- Spring JDBC --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.5</version></dependency> <!-- Spring Transaction (如果需要事务支持) --><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.3.10</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies> </project>
普通纯java代码 查询操作
ComboPooledDataSource dataSource =new ComboPooledDataSource();dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db2");dataSource.setUser("root");dataSource.setPassword("11111");JdbcTemplate jdbcTemplate =new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);int row = jdbcTemplate.update("INSERT INTO user values (?,?,?)", "110", "xl11x", 105);System.out.println(row);
Xml配置 springJDBCTemplate(简化纯java代码)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.Driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"></property></bean> </beans>``
Properties文件配置
jdbc.Driver =com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/db2 jdbc.user = root jdbc.password = 111111
测试 插入
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);int row =jdbcTemplate.update("INSERT INTO user values (?,?,?)", "1110", "llx", 1105);System.out.println(row);
测试 查询(重要)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);List<User> query = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<User>(User.class));System.out.println(query);
new BeanPropertyRowMapper<User>(User.class)
这个API里面的泛型填的是你要查询的封装而成的实体类 参数是实体类的字节码文件
返回的是一个list集合
实体类
package domain; public class User {private String sno;private String name;private int point; public User(String sno, String name, int point) {this.sno = sno;this.name = name;this.point = point;} public User() {} public String getSno() {return sno;} public void setSno(String sno) {this.sno = sno;} public String getName() {return name;} public void setName(String name) {this.name = name;} public int getPoint() {return point;} public void setPoint(int point) {this.point = point;} @Overridepublic String toString() {return "User{" +"sno='" + sno + '\'' +", name='" + name + '\'' +", point=" + point +'}';} }