1.mave 下载相关jar
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
2.写 实体类,Dao接口,Dao的实现类
//账户持久层实现类
public class Account1DaoImpl implements IAccount1Dao {
//JdbcTemplate 数据库操作类对象,作为属性。通过xml配置注入
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//id单查
public Account1 findById(int id) {
List<Account1> accs = jdbcTemplate.query("select * from account1 where id = ?",
new BeanPropertyRowMapper<Account1>(Account1.class),id);
return accs.isEmpty()?null:accs.get(0);
}
//名字查询操作
public Account1 findById(String name) {
List<Account1> accs = jdbcTemplate.query("select * from account1 where name = ?",
new BeanPropertyRowMapper<Account1>(Account1.class),name);
if(accs.isEmpty()){
return null;
}
if(accs.size()>1){
throw new RuntimeException("结果不唯一");
}
return accs.get(0);
}
//修改操作
public void modify(Account1 acc) {
jdbcTemplate.update("update account1 set name = ?,money = ? where id = ?;",
acc.getName(),acc.getMoney(),acc.getId());
}
}
3.写bean.xml 配置文件,把实现类中的jdbcTemplate属性,通过IOC容器注入
<?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">
<!--配置Dao实现类中的 jdbcTemplate 做(增删改查) 操作对象属性 添加进IOC容器-->
<bean id = "account1Dao" class="wuwei.dao.impl.Account1DaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--把数据源的4个属性,添加进jdbcTemplate对象中-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置spring内置数据源-->
<bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 配置spring内置连接数据库的4个基本信息 ,注意spring内置的 name 名是固定的字符。-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>
4.测试操作
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccount1Dao account1Dao = ac.getBean("account1Dao",IAccount1Dao.class);
//增加测试
Account1 as = account1Dao.findById(1);
System.out.println(as);
//修改操作
account1Dao.modify(new Account1(2,"孜然",500));
System.out.println("修改成功");
}
}
知识拓展:
当拥有多个实现类的时候,为解决代码冗余,实现类中的jdbcTemplate 可以不写,直接使用spring内部封装的,让实现类继承 JdbcDaoSupport 就可以直接实现。之后把bean.xml 配置文件改为 数据源就可以,也就是不用再IOC注入 jdbcTemplate
//账户持久层实现类
public class Account1DaoImpl extends JdbcDaoSupport implements IAccount1Dao {
// //JdbcTemplate 数据库操作类对象,作为属性。通过xml配置注入
// private JdbcTemplate jdbcTemplate;
// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
// this.jdbcTemplate = jdbcTemplate;
// }
//id单查
public Account1 findById(int id) {
List<Account1> accs = super.getJdbcTemplate().query("select * from account1 where id = ?",
new BeanPropertyRowMapper<Account1>(Account1.class),id);
return accs.isEmpty()?null:accs.get(0);
}
//名字查询操作
public Account1 findById(String name) {
List<Account1> accs = super.getJdbcTemplate().query("select * from account1 where name = ?",
new BeanPropertyRowMapper<Account1>(Account1.class),name);
if(accs.isEmpty()){
return null;
}
if(accs.size()>1){
throw new RuntimeException("结果不唯一");
}
return accs.get(0);
}
//修改操作
public void modify(Account1 acc) {
super.getJdbcTemplate().update("update account1 set name = ?,money = ? where id = ?;",
acc.getName(),acc.getMoney(),acc.getId());
}
}
<?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">
<!--配置Dao实现类中的 jdbcTemplate 做(增删改查) 操作对象属性 添加进IOC容器-->
<bean id = "account1Dao" class="wuwei.dao.impl.Account1DaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--<!–把数据源的4个属性,添加进jdbcTemplate对象中–>-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource"></property>-->
<!--</bean>-->
<!--配置spring内置数据源-->
<bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 配置spring内置连接数据库的4个基本信息 ,注意spring内置的 name 名是固定的字符。-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
</beans>