学习笔记--JdbcTemplate
1.JdbcTemplate开发步骤
1)添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.13</version>
</dependency>
2)创建数据源对象
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456")
3)设置数据源对象 执行操作
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//设置数据源对象 知道数据在哪
jdbcTemplate.setDataSource(dataSource);
//执行操作
int row = jdbcTemplate.update("insert into account values(?,?)","tom",5000);
System.out.println(row);
代码改进:
显然上面的方法用Spring注入的方法具有更好的结构。
在配置文件ApplicationContext.xml中,首先注入数据库类,再注入jdbc模板对象。
<!-- 配置数据源对象 -->
<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.uername}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- jdbc模板对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
再进行测试:
@Test
//测试Spring产生jdbcTemplate对象
public void test2(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");
int row = jdbcTemplate.update("insert into account values(?,?)","zhangsan",4000);
System.out.println(row);
}
需要注意的是,我们不希望以后修改数据库再去更改xml配置,所以我们将xml中的set属性的变量剥离到properties文件中。
此配置需要在context命名空间下。
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/test jdbc.uername = root jdbc.password = 123456
在xml中配置
<context:property-placeholder location="jdbc.properties"/>
2.JdbcTemplate常用操作
@ContextConfiguration 这个注解通常与 @RunWith(SpringJUnit4ClassRunner.class)联合使用用来测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
}
添加在测试类之上
2.1 更新操作
@Test
public void testUpdate(){
jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
}
2.2 删除操作
@Test
public void testDelete(){
jdbcTemplate.update("delete from account where name=?","tom");
}
2.3 查询全部操作
@Test
public void testQueryAll(){
List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(accountList);
}
jdbcTemplate.query 第一个参数为sql语句,第二个参数是new一个Bean属性映射类,负责将查询到的数据注入<>中的实体类,并强转成xxx.class类型。
返回的是一个集合。
2.4 查询单个操作
@Test
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "zhangsan");
System.out.println(account);
}
jdbcTemplate.queryForObject第一个参数为sql语句,第二参数同2.3,第三个参数是sql中占位符的内容。因为方法是queryForObject 返回的是注入的实体类对象。
2.5 查询数据数量
@Test
public void testQueryCount(){
Long count = jdbcTemplate.queryForObject("select count(*) from account", long.class);
System.out.println(count);
}
因为结果是一个数字,所以不需要使用映射器,第二个参数直接输入强转类型即可。
浙公网安备 33010602011771号