目录
一、JdbcTemplate对象。
(1)Spring产生JdbcTemplate对象。
这个是Spring产生JdbcTemplate对象:
jdbc.properties文件: jdbc.Driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/tan jdbc.user=root jdbc.password=tan
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--数据源对象-->
<!--
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/tan"/>
<property name="user" value="root"/>
<property name="password" value="tan"/>
</bean>
-->
<!--加载jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.Driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
public class JdbcTemplateTest {
@Test
//测试JdbcTemplate
public void test1() throws PropertyVetoException {
//获取数据源对象,并设置数据源属性
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/tan");
dataSource.setUser("root");
dataSource.setPassword("tan");
//创建jdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//设置数据源(连接池)对象,知道数据库在哪
jdbcTemplate.setDataSource(dataSource);
//执行操作
int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
System.out.println(row);
}
@Test
//测试Spring产生jdbcTemplate对象
public void test2(){
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("ApplicationContext.xml");
JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
int row = jdbcTemplate.update("insert into account values(?,?)", "zhangsan", 5000);
System.out.println(row);
}
}
(2)JdbcTemplate常用操作。
@RunWith指定测试运行器(Runner) :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
// @Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate(){
jdbcTemplate.update("update account set money = ? where name = ?", 10000, "tom");
}
@Test
public void testDelete(){
jdbcTemplate.update("delete from account where name = ?","tom");
}
@Test
public void testQueryAll(){
List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
@Test
public void testQueryOne(){
//出错,Integer.class这种就可以,因为多个数据需要封装对象
// 原因:需要封装
// Account tom = jdbcTemplate.queryForObject("select * from account where name = ?", Account.class, "tom");
Account tom1 = jdbcTemplate.queryForObject("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
System.out.println(tom1);
}
@Test
public void testQueryCount(){
//不需要封装的数据
Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
}