import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
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.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import com.atguigu.bean.Employee;
public class TxTest {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = ioc.getBean(JdbcTemplate.class);
NamedParameterJdbcTemplate njt = ioc.getBean(NamedParameterJdbcTemplate.class);
/**
* 实验1:测试数据源
* @throws SQLException
*/
@Test
public void testDataSource() throws SQLException {
DataSource bean = ioc.getBean(DataSource.class);
System.out.println(bean.getConnection());
}
/**
* 实验2:更新一条数据
*/
@Test
public void testUpdate() {
String sql = "update employee set salary = ? where emp_id= ?";
int update = jdbcTemplate.update(sql, 10000, 3);
System.out.println(update);
}
/**
* 实验3:批量插入
*/
@Test
public void testBatch() {
String sql = "insert into employee(emp_name, salary)values(? , ?)";
List list = new ArrayList<>();
list.add(new Object[]{"张三", 1800});
list.add(new Object[]{"赵武", 1300});
list.add(new Object[]{"二娜", 1100});
list.add(new Object[]{"王麻子", 1100});
list.add(new Object[]{"赵四", 1900});
jdbcTemplate.batchUpdate(sql, list);
}
/**
* 实验4:查询emp_id=5的数据库记录,封装为一个Java对象返回
*/
@Test
public void test1() {
String sql = "select emp_id,emp_name,salary from employee where emp_id = ?";
//使用无参构造器不行
//注意后边要写BeanPropertyRowMapper(Class mappedClass)
RowMapper rowMapper = new BeanPropertyRowMapper<>(Employee.class);
Employee object = jdbcTemplate.queryForObject(sql, rowMapper, 5);
System.out.println(object);
}
/**
* 实验5:查询salary>4000的数据库记录,封装为List集合返回
*/
@Test
public void test2() {
String sql = "select emp_id,emp_name,salary from employee where salary > ?";
RowMapper rowMapper = new BeanPropertyRowMapper<>(Employee.class);
List query = jdbcTemplate.query(sql, rowMapper, 4000);
System.out.println(query);
}
/**
* 实验6:查询最大salary
*/
@Test
public void test3() {
String sql = "select max(salary) from employee";
Double object = jdbcTemplate.queryForObject(sql, Double.class);
System.out.println(object);
}
/**
* 实验7:使用带有具名参数的SQL语句插入一条员工记录,并以Map形式传入参数值
*/
@Test
public void test4() {
String sql = "insert into employee (emp_id,emp_name,salary) values(:emp_id, :emp_name, :salary)";
//创建一个map集合分别给emp_id, emp_name,salary
Map map = new HashMap<>();
map.put("emp_id", null);
map.put("emp_name", "张三");
map.put("salary", 123.2);
//不用将map包装也可以
//System.out.println(map);
//MapSqlParameterSource source = new MapSqlParameterSource(map);
njt.update(sql, map);
}
//实验8:重复实验7,以SqlParameterSource形式传入参数值
@Test
public void test5() {
String sql = "insert into employee (emp_id,emp_name,salary) values(:emp_id, :emp_name, :salary)";
Employee emp = new Employee(null, "赵六", 1234.3);
//同样是给具名参数赋值
BeanPropertySqlParameterSource bpsps = new BeanPropertySqlParameterSource(emp);
njt.update(sql, bpsps);
}
}