通過架構學習(一)3.4節,JDBC完整模闆完成增删改查操作詳細步驟【==總結】
===========Spring JDBC模闆的使用
1.引入必要的Jar包
2.抽取連接配接配置檔案jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_1
jdbc.username=root
jdbc.password=123456
3.配置xml配置檔案applicationContext.xml
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.引入配置檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.引入相應的值 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/> //===========jdbc.properties裡的鍵
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3.配置Spring的JDBC模闆 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>
</beans>
4.代碼完成資料庫的增删改查
/**
* @author Leonard
* JDBC完成增删改查
*/
@RunWith(SpringJUnit4ClassRunner.class)//[email protected]用于測試
@ContextConfiguration("classpath:applicationContext.xml")//=======加載xml配置檔案
public class spring_2 {
@Resource(name="jdbcTemplate")//===========引入jdbc模闆
private JdbcTemplate jdbctemplate;
/*==========增删改查操作=================*/
@Test
public void insert(){
jdbctemplate.update("insert into account values(null,?,?)", "張三",230000d);
}
@Test
public void delete(){
jdbctemplate.update("delete from account where id = ?", 1);
}
@Test
public void update(){
jdbctemplate.update("update account set name = ? , money=? where id = ?", "李四",240000d,7);
}
/*================查詢操作================*/
//1.查詢一個資料
@Test
public void select(){
String name = jdbctemplate.queryForObject("select name from account where id=?", String.class, 5);
System.out.println(name);
}
//2.統計個數
@Test
public void count(){
Long count = jdbctemplate.queryForObject("select count(*) from account", Long.class);
System.out.println("個數======="+count);
}
//3.将查詢的資料封裝到一個對象Account==建立domain層,set,get,重寫toString
@Test
public void obj(){
Account account = jdbctemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
System.out.println("單條資料========"+account);
}
//4.查詢多條資料封裝到對象
@Test
public void objs(){
List<Account> list = jdbctemplate.query("select * from account", new MyRowMapper());
System.out.println("集合清單======="+list);
}
/*=========資料封裝=========================*/
class MyRowMapper implements RowMapper<Account>{
@Override
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
return account;
}
}
}
得到的查詢結果
ccc
單條資料========Account [id=5, name=ccc, money=333333.0]
集合清單=======[Account [id=2, name=bbb, money=222222.0], Account [id=3, name=ccc, money=333333.0], Account [id=4, name=ccc, money=333333.0], Account [id=5, name=ccc, money=333333.0], Account [id=7, name=李四, money=240000.0], Account [id=8, name=hhh, money=20000.0], Account [id=9, name=hhh, money=20000.0], Account [id=10, name=eee, money=555555.0], Account [id=11, name=eee, money=555555.0], Account [id=12, name=eee, money=555555.0], Account [id=13, name=張三, money=230000.0], Account [id=14, name=張三, money=230000.0], Account [id=15, name=張三, money=230000.0], Account [id=16, name=張三, money=230000.0], Account [id=17, name=張三, money=230000.0], Account [id=18, name=張三, money=230000.0], Account [id=19, name=張三, money=230000.0], Account [id=20, name=張三, money=230000.0]]
個數=======18