Java Spring-JdbcTemplate增删改查
2017-11-11 21:13:13
Spring 框架中提供了对持久层技术支持的类 :
- JDBC : org.springframework.jdbc.core.support.JdbcDaoSupport
- Hibernate 3.0 : org.springframework.orm.hibernate3.support.HibernateDaoSupport
- iBatis : org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
增加,删除和修改的操作还是比较容易的,因为,并不需要有返回值。
增加,删除和修改的代码:
// User public class User { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } } // UserDao public class UserDao extends JdbcDaoSupport { public void add(User user){ String sql = "insert into user values(?,?)"; getJdbcTemplate().update(sql,user.getId(),user.getName()); } public void delete(User user){ String sql = "delete from user where id=?"; getJdbcTemplate().update(sql, user.getId()); } public void update(User user){ String sql = "update user set name=? where id=?"; getJdbcTemplate().update(sql, user.getName(), user.getId()); } } // 测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config4.xml") public class Jdbc3 { @Resource(name = "userdao") private UserDao userdao; @Test public void demo(){ User user= new User(); user.setName("刘亦菲"); user.setId(001); // userdao.add(user); //user.setName("胡歌"); //userdao.update(user); userdao.delete(user); } }
查找操作是相对比较麻烦的,因为这里一般是有返回值的。
针对返回值为基本类型的,可以比较方便的进行转换;
针对返回值是对象的,需要自定义转换方法;
查询代码:
// UserDao public class UserDao extends JdbcDaoSupport { public void add(User user){ String sql = "insert into user values(?,?)"; getJdbcTemplate().update(sql,user.getId(),user.getName()); } public void delete(User user){ String sql = "delete from user where id=?"; getJdbcTemplate().update(sql, user.getId()); } public void update(User user){ String sql = "update user set name=? where id=?"; getJdbcTemplate().update(sql, user.getName(), user.getId()); } public int findCount(){ String sql = "select count(*) from user"; return this.getJdbcTemplate().queryForObject(sql,Integer.class); } public String findNameByID(int id){ String sql = "select name from user where id = ?"; return getJdbcTemplate().queryForObject(sql,String.class,id); } public User findUserByID(int id){ String sql = "select * from user where id = ?"; return getJdbcTemplate().queryForObject(sql, new MyRowMap(), id); } public List<User> findAll(){ String sql = "select * from user"; return getJdbcTemplate().query(sql,new MyRowMap()); } // 匿名内部类进行自定义的类型转换 class MyRowMap implements RowMapper<User>{ /** * * @param resultSet:结果集,里面包含所有的结果 * @param i:行号 * @return 将每一行的数据自行转成需要的对象的数据类型 * @throws SQLException */ @Override public User mapRow(ResultSet resultSet, int i) throws SQLException { User user = new User(); user.setName(resultSet.getString("name")); user.setId(resultSet.getInt("id")); return user; } } } // 查询 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config4.xml") public class Jdbc3 { @Resource(name = "userdao") private UserDao userdao; @Test public void demo(){ User user= new User(); user.setName("章子怡"); user.setId(002); userdao.add(user); } @Test public void demo2(){ System.out.println(userdao.findCount()); List<User> ls = userdao.findAll(); for(User user:ls){ System.out.println(user); } } }