#Spring JdbcTemplate - 增删改查(CRUD) @FDDLC
项目结构:
pom.xml的内容:
<?xml version="1.0" encoding="UTF-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>org.example</groupId> <artifactId>P033_JdbcTemplate_CRUD</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.8.RELEASE</version> </dependency> </dependencies> </project>
MySQL数据库:
database:test port:3306 table:id_name
bean.xml的内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF8&serverTimezone=Asia/Shanghai"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean class="org.springframework.jdbc.core.JdbcTemplate" id="template"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
Account.java的内容:
package cn.liuxingchang; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
Main.java的内容:
package cn.liuxingchang; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; public class Main { public static void main(String[] args) { //ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); JdbcTemplate template = context.getBean("template", JdbcTemplate.class); /* //1、update:插入、修改、删除 template.update("insert into account(id, name, money) values(?, ?, ?)", 5, "Zhou", 500); template.update("update account set money = ? where id = ?", 456, 4); template.update("delete from account where id = ?", 3); */ //2、query:查询多行、查询一行 /* List<Account> accounts = template.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class)); for (Account account: accounts) { System.out.println(account); } */ //上面和下面功能一样,推荐使用上面! List<Account> accounts = template.query("select * from account", new AccountRowMapper()); for(Account account: accounts) { System.out.println(account); } List<Account> account = template.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), 1); System.out.println("查询一行:" + account.get(0)); //3、queryForObject:查询一行一列,如:count Integer count = template.queryForObject("select count(*) from account", Integer.class); System.out.println(count); } } //这里定义一个类,用于测试自定义RowMapper。其实完全没必要! class AccountRowMapper implements RowMapper<Account> { public Account mapRow(ResultSet resultSet, int i) throws SQLException { Account account = new Account(); account.setId(resultSet.getInt("id")); account.setName(resultSet.getString("name")); account.setMoney(resultSet.getDouble("money")); return account; } }