JdbcTemplate实验

实验1:测试数据源
@Test
public void test() throws SQLException {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource bean = ioc.getBean(DataSource.class);
System.out.println(bean.getConnection());
}

实验2:将emp_id=5的记录的salary字段更新为1300.00【更新操作】
public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private JdbcTemplate template=ioc.getBean(JdbcTemplate.class);

@Test
public void test01(){
//实验2:将emp_id=5的记录的salary字段更新为1300.00
String sql = "UPDATE employee SET salary = ? WHERE emp_id = ?";
template.update(sql, 1300,5);//第一个是sql语句,后面的按着顺序传入参数即可,这个update方法是接收的可变参数!
}
}
从上述实验中就可以看到,该操作不用我们自己再去获取数据库连接信息了,而是直接传递sql语句及其参数!


实验3:批量插入
public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private JdbcTemplate template=ioc.getBean(JdbcTemplate.class);
@Test
public void testBatch(){
String sql="INSERT INTO employee(`emp_name`,`salary`) VALUES(?,?)";
//执行sql语句需要传递的参数
// Object[][] params = new Object[3][2];
// params[0] = new Object[]{"Tom2015",1000};
// params[1] = new Object[]{"Tom2016",2000};
// params[2] = new Object[]{"Tom2017",3000};
//
List<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[]{"Tom2015",1000});
list.add(new Object[]{"Tom2016",2000});
list.add(new Object[]{"Tom2017",3000});

template.batchUpdate(sql, list);
}
}

实验4:查询emp_id=5的数据库记录,封装为一个Java对象返回
分析:封装为一个对象返回的话,首先我们需要有一个与数据表对应的实体类!

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private JdbcTemplate template=ioc.getBean(JdbcTemplate.class);

@Test
public void test01(){
//需要注意的是:sql语句中的别名要与对应实体类的属性名保持一致!
String sql = "SELECT emp_id AS empId,emp_name AS empName,salary FROM employee WHERE emp_id=?";

//RowMapper是一个接口,这里我们使用其子类
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
//最后一个参数是可变参数,用于向sql语句中依次传递参数!
Employee employee = template.queryForObject(sql, rowMapper, 5);
System.out.println(employee);
}
}

实验5:查询salary>4000的数据库记录,封装为List集合返回
public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private JdbcTemplate template=ioc.getBean(JdbcTemplate.class);

@Test
public void test01(){
//需要注意的是:sql语句中的别名要与对应实体类的属性名保持一致!
String sql = "SELECT emp_id AS empId,emp_name AS empName,salary FROM employee WHERE salary > ?";

//RowMapper是一个接口,这里我们使用其子类
RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
//该query方法查询出来的是一个list列表,query方法的最后一个参数是可变参数!
List<Employee> list = template.query(sql, rowMapper, 4000);

for (Employee employee : list) {
System.out.println(employee);
}
}
}

//从上面可以看出,查询结果是一个实体还是一个list列表是靠template对象的不同方法实现的!


实验6:查询最大salary
public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private JdbcTemplate template=ioc.getBean(JdbcTemplate.class);

@Test
public void test01(){
String sql = "SELECT MAX(salary) FROM employee";
//需要指定返回值的类型,而且类型必须是包装类型
Double maxSalary = template.queryForObject(sql, Double.class);
System.out.println(maxSalary);
}
}

实验7:使用带有具名参数的SQL语句插入一条员工记录,并以Map形式传入参数值
具名参数:是指基于名称的,前面我们使用的都是用?作为占位符,然后是使用基于位置的!
如果要使用具名参数的sql语句就必须在spring配置文件中配置NamedParameterJdbcTemplat这个模板类,而不能使用
原来的JdbcTemplate,因为JdbcTemplate不能完成这样的任务!

<!-- 加载properties文件中 信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.passowrd}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
</bean>

<!-- 配置JdbcTemplate对应的bean, 并装配dataSource数据源属性-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- 为了执行带有具名参数的SQL语句,需要配置NamedParameterJdbcTemplate -->
<!-- 该NamedParameterJdbcTemplate类没有无参构造器,需要传入JdbcTemplate对象或者数据源对象[DataSource] -->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<!-- 不能使用property标签配置哦 -->
<constructor-arg ref="jdbcTemplate"></constructor-arg>
</bean>

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
String sql="INSERT INTO employee(`emp_name`,`salary`) VALUES(:paramName,:paramSalary)";
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("paramName","张学友" );
paramMap.put("paramSalary",1000);

namedJdbcTemplate.update(sql, paramMap);
}
}

实验8:重复实验7,以SqlParameterSource形式传入参数值

public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
String sql="INSERT INTO employee(`emp_name`,`salary`) VALUES(:empName,:salary)";
//该BeanPropertySqlParameterSource类构造器需要一个对象参数,该对象参数是一个封装了sql语句参数的对象!
//此时要求对象的属性名要和sql中的参数名保持一致!这里我们使用Employee对象来完成
Employee employee= new Employee(null, "郭富城", 1500);
//以实体对象的形式封装具名参数和值
SqlParameterSource source = new BeanPropertySqlParameterSource(employee);

namedJdbcTemplate.update(sql, source);
}
}

实验9:创建JdbcTemplateDao,自动装配JdbcTemplate对象
1.创建dao类:
@Repository
public class JdbcTemplateDao {
@Autowired
private JdbcTemplate jdbcTemplate;

public void update(String sql,Object ...args){
jdbcTemplate.update(sql, args);
}
}
2.配置spring的配置文件
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.neuedu.dao"></context:component-scan>
<!-- 加载properties文件中 信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.passowrd}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
</bean>

<!-- 配置JdbcTemplate对应的bean, 并装配dataSource数据源属性-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>

3.测试该dao
public class TestDataSource {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private NamedParameterJdbcTemplate namedJdbcTemplate = ioc.getBean(NamedParameterJdbcTemplate.class);

@Test
public void test01(){
JdbcTemplateDao dao = ioc.getBean(JdbcTemplateDao.class);
String sql = "INSERT INTO employee(`emp_name`,`salary`) VALUES(?,?)";
dao.update(sql, "比尔盖茨",10000000);

}
}

原文地址:https://www.cnblogs.com/lc-java/p/7460018.html