项目结构:

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>P034_dao_JdbcTemplate</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>
<!--注意:spring-jdbc和spring-context有一部分依赖相同,易导致冲突,因此要让二者版本相同-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>
</project>
MySQL数据库:
database:test port:3306 table:account

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>
<bean class="cn.liuxingchang.dao.impl.AccountDaoImpl" id="accountDao">
<property name="jdbcTemplate" ref="template"></property>
</bean>
</beans>
Account.java的内容:
package cn.liuxingchang.dao;
import cn.liuxingchang.domain.Account;
public interface AccountDao {
Account findAccountById(Integer id);
Account findAccountByName(String name);
void update(Account account);
}
AccountDao.java的内容:
package cn.liuxingchang.dao;
import cn.liuxingchang.domain.Account;
public interface AccountDao {
Account findAccountById(Integer id);
Account findAccountByName(String name);
void update(Account account);
}
AccountDaoImpl.java的内容:
package cn.liuxingchang.dao.impl;
import cn.liuxingchang.dao.AccountDao;
import cn.liuxingchang.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate template;
public void setJdbcTemplate(JdbcTemplate template) {
this.template = template;
}
public Account findAccountById(Integer id) {
List<Account> accounts = template.query("select * from account where id = ?",
new BeanPropertyRowMapper<Account>(Account.class), id);
if(accounts.isEmpty()) {
return null;
} else {
return accounts.get(0);
}
}
public Account findAccountByName(String name) {
List<Account> accounts = template.query("select * from account where name = ?",
new BeanPropertyRowMapper<Account>(Account.class), name);
if(accounts.isEmpty()) {
return null;
} else if(accounts.size() > 1) {
throw new RuntimeException("结果不唯一!");
} else {
return accounts.get(0);
}
}
public void update(Account account) {
template.update("update account set name = ?, money = ? where id = ?",
account.getName(), account.getMoney(), account.getId());
}
}
Main.java的内容:
package cn.liuxingchang;
import cn.liuxingchang.dao.AccountDao;
import cn.liuxingchang.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
AccountDao accountDao = context.getBean("accountDao", AccountDao.class);
//System.out.println(accountDao.findAccountById(1));
//System.out.println(accountDao.findAccountByName("Zhao"));
Account account = accountDao.findAccountById(1);
account.setMoney(111.0);
accountDao.update(account);
}
}
本文展示了如何使用 Spring 的 JdbcTemplate 模块连接 MySQL 数据库并执行 CRUD 操作。通过配置 bean.xml 文件设置数据源和模板,然后在 AccountDaoImpl 类中实现具体的数据操作方法。
385

被折叠的 条评论
为什么被折叠?



