JdbcTemplate基础知识
1)JdbcTemplate初见
JdbcTemplate是对繁琐的原生JdbcAPI对象的封装。
Spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate。
2)JdbcTemplate的简单开发步骤
先配置DataSource数据源对象,再将其注入JdbcTemplate模板对象,之后利用该模板对象进行数据库的诸多操作。
3)JdbcTemplate需要导入的Maven坐标
<!-- 下面的Maven坐标可以参考一下 >_< -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
JdbcTemplate开发(不使用Spring)
① 利用4个参数配置DataSource数据源对象
② 利用配置好的DataSource数据源对象配置JdbcTemplate模板对象
③ 即可使用
public class JdbcTemplateTest {
@Test
public void test() throws PropertyVetoException {
// 创建DataSource数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/loliDB");
dataSource.setUser("root");
dataSource.setPassword("123456");
// 创建JdbcTemplate模板对象
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(dataSource);
// 使用
int row = template.update("insert into account values(?, ?)", "Hana", 10);
System.out.println(row);
}
}
JdbcTemplate开发(使用Spring)
我们发现,无论是配置DataSource,还是配置Template,代码使用的都是setXxx方法——立即想到依赖注入
jdbc.properties ---------------------------------------------------------------------------
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/loliDB
jdbc.username=root
jdbc.password=123456
applicationContext.xml --------------------------------------------------------------------
<!-- 从外部加载properties资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- DataSource数据源对象 -->
<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.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- JdbcTemplate模板对象 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
test.JdbctemplateTest ---------------------------------------------------------------------
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbctemplateTest {
@Autowired
private JdbcTemplate template;
@Test
public void test() {
int row = template.update("insert into account values(?, ?)", "Hana", 10);
System.out.println(row);
}
}
JdbcTemplate的使用
1)update——增删改都用它!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate template;
@Test
public void test1() {
template.update("insert into lolihouse values(?, ?)", "Hana", 8);
}
@Test
public void test2() {
template.update("delete from lolihouse where name = ?", "Hana");
}
@Test
public void test3() {
template.update("update lolihouse set age = ? where name = ?", 11, "Alice");
}
}
2)query——各种各样的查询!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate template;
@Test
// 查询结果为出对象列表
public void test4() {
List<Loli> loliList = template.query("select * from lolihouse", new BeanPropertyRowMapper<Loli>(Loli.class));
System.out.println(loliList);
}
@Test
// 查询结果为一个对象
public void test5() {
Loli loli = template.queryForObject("select * from lolihouse where name = ?", new BeanPropertyRowMapper<Loli>(Loli.class), "Alice");
System.out.println(loli);
}
@Test
// 查询结果为统计个数
public void test6() {
Integer count = template.queryForObject("select count(*) from lolihouse", Integer.class);
System.out.println(count);
}
}