package springjdbc.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JdbcTest {
ApplicationContext ctx = null;
JdbcTemplate jdbcTemplate = null;
NamedParameterJdbcTemplate namedParameterJdbcTemplate ;
{
ctx = new ClassPathXmlApplicationContext("springjdbc/application.xml");
jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
namedParameterJdbcTemplate = ctx.getBean(NamedParameterJdbcTemplate.class);
}
//具名参数SqlParameterSource
@Test
public void testNamedParameterJdbcTemplate2() {
String sql = "INSERT INTO TABLE1(id,name) VALUES(:id,:name)";
Emp emp = new Emp();
emp.setId(2);
emp.setName("b");
SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(emp);
namedParameterJdbcTemplate.update(sql, sqlParameterSource);
}
//具名参数Map,对参数起名字,不用对应位置
@Test
public void testNamedParameterJdbcTemplate() {
String sql = "INSERT INTO TABLE1(id,name) VALUES(:id,:name)";
Map paraMap = new HashMap<>();
paraMap.put("id", 1);
paraMap.put("name", "a");
namedParameterJdbcTemplate.update(sql, paraMap);
}
//单个列的值
@Test
public void testQueryObject1() {
String sql = "SELECT count(id) FROM table1";
long count = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(count);
}
//得到结果的集合
@Test
public void testQueryList() {
String sql = "SELECT id,name FROM table1 WHERE id > ?";
RowMapper rowMapper = new BeanPropertyRowMapper<>(Emp.class);
List list = jdbcTemplate.query(sql, rowMapper, 5);
System.out.println(list);
}
//RowMapper映射查询结果集
//不支持级联属性
@Test
public void testQueryObject() {
String sql = "SELECT id,name FROM table1 WHERE id = ?";
RowMapper rowMapper = new BeanPropertyRowMapper<>(Emp.class);
Emp emp = jdbcTemplate.queryForObject(sql, rowMapper, 6);
System.out.println(emp);
}
/*
//批量更新,需要一个Object[]数组
@Test
public void testBatchUpdate() {
String sql = "INSERT INTO TABLE1(id,name) VALUES(?,?)";
List batchArgs = new ArrayList<>();
batchArgs.add(new Object[]{5,"e"});
batchArgs.add(new Object[]{6,"f"});
batchArgs.add(new Object[]{7,"g"});
batchArgs.add(new Object[]{8,"h"});
jdbcTemplate.batchUpdate(sql, batchArgs);
}
//更新
@Test
public void testUpate() {
String sql = "INSERT INTO TABLE1 VALUES(?, ?)";
jdbcTemplate.update(sql, 9, "i");
}
*/
//DataSource
@Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}
}
package springjdbc.test;
public class Emp {
private Integer id;
private String name;
public Emp() {
}
public Emp(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package springjdbc.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class EmpDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public Emp getName(Integer id) {
String sql = "select name from table1 where id = ?";
RowMapper rowMapper = new BeanPropertyRowMapper<>();
Emp emp = jdbcTemplate.queryForObject(sql, rowMapper, id);
return emp;
}
}
jdbc.user=root
jdbc.password=abcd1234
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
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">