package com.ww.test; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; 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.support.rowset.SqlRowSet; import com.ww.dao.GirlDao; public class JTest { private ClassPathXmlApplicationContext c; //private JdbcTemplate jdbcTemplate; @Test public void test() { // 增删改---update // String sql = "insert into appuser values(sq_test.nextval, ?, ?, ?, ?, ?)"; // int a = jdbcTemplate.update(sql, "xiaohuang", "1122233", "小黄", "男", new // Date()); // System.out.println(a); // 查询 // String sql = "select * from appuser a where a.password=? and a.username like ?"; // List<Map<String, Object>> list = jdbcTemplate.queryForList(sql); // for (Map<String, Object> map : list) { // System.out.println(map); // } // 查询表中某一列的数据, 一般限定Integer或者String // List<String> list = jdbcTemplate.queryForList(sql, String.class); // for (String s : list) { // System.out.println(s); // } // 只能查询一条记录 // Map<String, Object> map = jdbcTemplate.queryForMap(sql, 268); // System.out.println(map); /* * SqlRowSet srs = jdbcTemplate.queryForRowSet(sql); srs.next() */ // jdbcTemplate的持久化操作 // (这行是错误的)AppUser user = jdbcTemplate.queryForObject(sql, AppUser.class, 268); // RowMapper<AppUser> rowMapper = new RowMapper<AppUser>() { // @Override // public AppUser mapRow(ResultSet rs, int i) throws SQLException { // System.out.println(i); // AppUser u = new AppUser(); // u.setCreatetime(rs.getTimestamp("createtime")); // u.setIds(rs.getInt("ids")); // u.setPassword(rs.getString("password")); // u.setUsername(rs.getString("username")); // u.setRealname(rs.getString("realname")); // u.setSex(rs.getString("sex")); // return u; // } // }; // AppUser user = jdbcTemplate.queryForObject(sql, rowMapper, 268); // RowMapper<AppUser> bprm = // new BeanPropertyRowMapper<AppUser>(AppUser.class); // // List<AppUser> users = // jdbcTemplate.query(sql, bprm, "111", "%xiao%"); // // System.out.println(users); // spring声明式事务 GirlDao girldao = c.getBean(GirlDao.class); int a = girldao.deleteGirl("cc"); } @Before public void setUp() throws Exception { c = new ClassPathXmlApplicationContext("conf/spring.xml"); //jdbcTemplate = (JdbcTemplate) c.getBean("jdbcTemplate"); } @After public void tearDown() throws Exception { c.close(); } }