1. 程式人生 > >JdbcTemplate和宣告式事務(Spring框架)

JdbcTemplate和宣告式事務(Spring框架)

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<Object[]> 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<T> mappedClass)         RowMapper<Employee> 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<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);         List<Employee> 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<String, Object> 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);     } }