1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| package com.xiaoguan.test;
import com.xiaoguan.bean.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class JDBCTemplateTest { @Test public void testCallback(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="select id,real_name,age from t_user where id=?"; User u = jdbcTemplate.execute(sql, new PreparedStatementCallback<User>() { @Override public User doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { User user = new User(); ps.setInt(1, 2); ResultSet resultSet = ps.executeQuery(); if (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("real_name"); int age = resultSet.getInt("age"); user.setId(id); user.setAge(age); user.setRealName(name); } return user; } }); System.out.println(u);
} @Test public void testBatchDelete(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="delete from t_user where id=?"; Object[] objects1={5}; Object[] objects2={6}; Object[] objects3={7}; List<Object[]> list=new ArrayList<>(); list.add(objects1); list.add(objects2); list.add(objects3); int[] ints = jdbcTemplate.batchUpdate(sql, list); System.out.println(Arrays.toString(ints));
} @Test public void testBatchUpdaate(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="update t_user set real_name=?,age=? where id=?"; Object[] objects1={"小明1",18,4}; Object[] objects2={"小明2",19,5}; Object[] objects3={"小明3",20,6}; List<Object[]> list=new ArrayList<>(); list.add(objects1); list.add(objects2); list.add(objects3); int[] ints = jdbcTemplate.batchUpdate(sql,list); System.out.println(Arrays.toString(ints)); } @Test public void testBatchInsert(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="insert into t_user (real_name,age) values(?,?)"; Object[] objects1={"小花1",31}; Object[] objects2={"小花2",32}; Object[] objects3={"小花3",33}; Object[] objects4={"小花4",34}; List<Object[]> list=new ArrayList<>(); list.add(objects1); list.add(objects2); list.add(objects3); list.add(objects4); int[] count = jdbcTemplate.batchUpdate(sql, list); System.out.println(Arrays.toString(count)); } @Test public void testQueryOneValue(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="select count(1) from t_user"; Integer total = jdbcTemplate.queryForObject(sql, int.class); System.out.println(total);
} @Test public void testSelectAll(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="select id,real_name,age from t_user "; List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); System.out.println(users); } @Test public void testSelectOne(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="select id,real_name,age from t_user where id=?"; User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class),2); System.out.println(user); } @Test public void testDelete(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="delete from t_user where id=?"; int count = jdbcTemplate.update(sql, 1); System.out.println(count); } @Test public void testUpdate(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="update t_user set real_name=?,age=? where id =?"; int count = jdbcTemplate.update(sql, "小关", 22, 1); System.out.println(count); } @Test public void testInsert(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql="insert into t_user (real_name,age) values(?,?)"; int count = jdbcTemplate.update(sql, "王五", 32); System.out.println(count); } @Test public void testDataSource(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); System.out.println(jdbcTemplate); } }
|