标签:传参方式 eth cat contex date 单位 ide logs XML
传参有两种传参方式,一种直接传,一种通过对象传。
查询返回值,可以直接封进对象。
public class springJdbcTest { private static JdbcTemplate jdbcTemplate; private static String uuid; @BeforeClass public static void init(){ ApplicationContext context = new ClassPathXmlApplicationContext("/spring/applicationContext-pool2.xml"); jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate"); uuid=UUID.randomUUID().toString(); } @Test public void add(){ String sql = "insert into t_user_test(uid,loginname,loginpass) values(?,?,?)"; System.out.println(uuid); jdbcTemplate.update(sql, uuid,"test"+System.currentTimeMillis(),"pass"); } @Test public void update(){ String sql = "update t_user_test set loginname=? where uid = ?"; jdbcTemplate.update(sql,new Object[] {"张三",uuid});//两种方式 } //查询 @Test//数量 public void testqueryCount() { String sql = "select count(*) from t_user_test"; int count = jdbcTemplate.queryForObject(sql, Integer.class); System.out.println("count----" +count); } @Test public void testqueryUserList() { String sql = "select * from t_user_test "; List<User> list = jdbcTemplate.query(sql,new UserRowMapper()); //传回List<Map<String, Object>> List<Map<String, Object>> list2 = jdbcTemplate.queryForList(sql); System.out.println(list2); //直接传回user // User user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), id); // System.out.println("testqueryUser--" + user.getName()); System.out.println(list); } @Test public void testqueryUser() { String sql = "select * from t_user_test where uid =? "; User user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), uuid); System.out.println("testqueryUser--" + user.getName()); } class UserRowMapper implements RowMapper<User> { //rs为返回结果集,以每行为单位封装着 @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { // TODO Auto-generated method stub User user = new User(); user.setName(rs.getString("loginname")); // user.setId(rs.getInt("id")); // user.setUsername(rs.getString("username")); // user.setPassword(rs.getString("password")); return user; } } }
标签:传参方式 eth cat contex date 单位 ide logs XML
原文地址:http://www.cnblogs.com/zhangml/p/7260601.html