Spring JdbcTemplate
1.Spring JdbcTemplate基本使用
1.1 基本概述
它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装,spring框架为我们提供了很多的操作模板类,例如:操作关系型数据的Jdbc Template和HibernateTempalte,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。
1.2 JdbcTemplate开发步骤
1.导入Spring-jdbc和spring-tx坐标
<!--JdbcTemplate-->
<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>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
2.创建数据库表和实体
//创建数据库表
create table account(string name,double money);
//java代码中创建表的实体
public class Account{
private String name;
private double money;
get 和set方法 tostring方法
...
}
3.创建JdbcTemplate对象
4.执行数据库操作
//测试
@Test
public void test(){
//创建数据源对象
ComboPoolDataSource datasource = new ComboPoolDataSource();
datasource.setDriverClass("com.mysql.jdbc.Driver")
datasource.setJdbcUrl("jdbc:mysql://localhost:3306/test")
datasource.setUser("root");
datasource.setPassword("2019");
JdbcTemplate jdbctemplate = new JdbcTemplate();
//设置数据源对象
jdbctemplate.setDataSource(datasource);
//增删改
int row1 = jdbctemplate.update(sql);
//查
jdbctemplate.query(sql);
}
上述步骤太过繁琐,我们可以将JdbcTemplate的创建交给spring容器
//applicationContext.xml
//数据源datasource
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://locallhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password value= "2019"></property>
</bean>
//jdbcTemplate
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="datasource" value="datasource"></property>
</bean>
//java代码
public void jdbcTest(){
ApplicationContext app= new ApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
...
}
上述还可以对jdbc的配置文件进行抽取
//新建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://locallhost:3306/test
jdbc.username=root
jdbc.password=2019
//在applicationContext.xml中加载jdbc.properties文件
//命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
//加载
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
//替换写死的代码
com.mysql.jdbc.Driver 替换为${jdbc.driver}
jdbc:mysql://locallhost:3306/test 替换为 ${jdbc.url}
...
测试:
//
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestCURD{
@Autowired
private JdbcTemplate jdbc;
@Test
public void testUpdate(){
//更新语句
jdbc.update(sql);
...
//查询多个对象 BeanPropertyMapper<Account>(Account.class)中Account为实体类
List<Account> list jdbc.query("select * from account",new BeanPropertyMapper<Account>(Account.class));
//查询一个对象
Account account = jdbc.queryForObject("select * from account where name=?",new BeanPropertyMapper<Account>(Account.class),"zhangsan")
}
}