概述:
spring對很多技術(shù)復(fù)雜繁瑣的代碼進(jìn)行封裝,JDBCTemplate就是其中一種
如:操作關(guān)系型數(shù)據(jù)的JdbcTemplate和HibernateTemplate,操作nosql數(shù)據(jù)庫的RedisTemplate,操作消息隊(duì)列的JmsTemplate等等。
步驟:
- 導(dǎo)入spring-jdbc和spring-tx【】坐標(biāo)
<!--JDBCTemplte-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
- 創(chuàng)建數(shù)據(jù)庫表和實(shí)體
- 創(chuàng)建JdbcTemplate對象
@Test
public void test1() throws SQLException {
//創(chuàng)建數(shù)據(jù)源
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("JDBC:mysql://localhost:3306/springtest");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//創(chuàng)建JDBCTemplate對象
JdbcTemplate template=new JdbcTemplate(dataSource);
String sql="insert into account(name,money) values(?,?)";
int count = template.update(sql, "zhangsan", 1200);
System.out.println(count);
}
- 執(zhí)行數(shù)據(jù)庫操作
spring產(chǎn)生模板對象代碼實(shí)現(xiàn)(抽取jdbc.properties)
<?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
">
<!--尋找jdbc.properties配置文件-->
<context:property-placeholder location="classpath:Jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Jdbc基本使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplate_CRUD {
@Autowired
private JdbcTemplate template;
@Test
public void testUpdate(){
int row = template.update("update account set money=? where name=?", 2000, "王五");
}
@Test
public void testDelete(){
int row = template.update("delete from account where name=?", "??");
}
/**
* 查詢多個對象
*/
@Test
public void testQuery(){
List<Account> AccountList = template.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(AccountList);
}
/**
* 查詢多個多項(xiàng)
*/
@Test
public void testQueryOne(){
Account account = template.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "王五");
System.out.println(account);
}
/**
* 聚合函數(shù)查詢
*/
@Test
public void testQueryCount(){
long count = template.queryForObject("select count(*) from account",long.class);
System.out.println(count);
}
}