使用JDBC模板
public class demo01Test {
@Test
public void test(){
//连接spring自带连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//创建jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values (null ,?,?)","张三",200);
}
}
将连接池和jdbc模板交给spring管理
<!--配置内置连接池-->
<bean id="datasource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
IOC的灵活应用,因为类里有set方法用property,配置普通属性
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置jdbc模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
IOC的灵活应用,配置对象属性
<property name="dataSource" ref="datasource01"/>
</bean>
配置DBCP,C3P0连接池
配置dbcp连接池
<bean id="datasource02" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
配置c3p0连接池
<bean id="datasource03" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
配置jdbc模板
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
IOC的灵活应用,配置对象属性
<property name="dataSource" ref="datasource03"/>
</bean>
在spring中引入配置属性文件
<!--引入配置文件-->
第一种方式,不常用
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties"/>
</bean>
第二种方式
<context:property-placeholder location="classpath:jdbc.properties"/>
使用JDBC模板完成CRUD操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class demo02Test {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate;
增
@Test
public void test() {
jdbcTemplate.update("insert into account values (null,?,?)", "水岸东方", 300);
}
删
@Test
public void delete(){
jdbcTemplate.update("delete from account where id= ? ",6);
}
改
@Test
public void update(){
jdbcTemplate.update("update account set name = ? where id= ? ","张无忌",1);
}
查
@Test
public void query(){
String name = jdbcTemplate.queryForObject("select name from account where id= ? ", String.class, 1);
System.out.println(name);
}
统计个数
@Test
public void count(){
Long count = jdbcTemplate.queryForObject("select count(*) from account",Long.class);
System.out.println(count);
}
封装对象
@Test
public void query02(){
Account account = jdbcTemplate.queryForObject("select * from account where id= ?", new MyRowMapper(), 2);
System.out.println(account);
}
@Test
public void query03() {
List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper());
System.out.println(list);
}
class MyRowMapper implements RowMapper<Account>{
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setId(resultSet.getInt("id"));
account.setMoney(resultSet.getDouble("money"));
account.setName(resultSet.getString("name"));
return account;
}
}