转自csdn
http://topic.csdn.net/u/20081102/09/2d46728d-fc23-47bc-b6da-e4638eb0d298.html
备忘:
- package hl.ghost.dao;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.RowMapper;
- public class Session implements RowMapper {
- private JdbcTemplate jdbcTemplate;
- private Class clazz;
- public int save(Object target, String table) {
- StringBuilder sb = new StringBuilder("insert into ");
- List list = new ArrayList();
- sb.append(table);
- sb.append(" (");
- for (Field field : target.getClass().getDeclaredFields()) {
- Object result = this.getObjectValue(field,target);
- sb.append(field.getName()).append(",");
- list.add(result);
- }
- sb.replace(sb.length() - 1, sb.length(), ")");
- sb.append(" values(");
- for (int i = 0; i < target.getClass().getDeclaredFields().length; i++) {
- sb.append("?,");
- }
- sb.replace(sb.length() - 1, sb.length(), ")");
- return this.jdbcTemplate.update(sb.toString(), list.toArray());
- }
- public int delete(Object target, String table) {
- try {
- return this.jdbcTemplate.update("delete from " + table
- + " where id=?", new Object[] { getObjectValue(target.getClass()
- .getDeclaredField("id"),target) });
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- public List <Object> selectObjectsByProperties(Object target, String table)
- {
- clazz=target.getClass();
- StringBuilder sb = new StringBuilder("select * from " + table
- + " where 1=1 ");
- List <Object> list = new ArrayList <Object>();
- if (null != target) {
- for (Field field : target.getClass().getDeclaredFields()) {
- Object result = this.getObjectValue(field,target);
- if (result != null) {
- sb.append(" and ").append(field.getName()).append("=? ");
- list.add(result);
- }
- }
- }
- return this.jdbcTemplate.query(sb.toString(), list.toArray(), this);
- }
- public Object mapRow(ResultSet rs, int arg1) throws SQLException {
- Object object = null;
- try {
- object = clazz.getConstructor(null).newInstance();
- for (Field field : clazz.getDeclaredFields()) {
- Method method = clazz.getMethod("set"
- + new StringBuffer(field.getName()).replace(0, 1, field
- .getName().substring(0, 1).toUpperCase()),
- new Class[] { field.getType() });
- method.invoke(object, new Object[] { rs.getObject(field
- .getName())});
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return object;
- }
- public Object getObjectValue(Field field,Object target) {
- Object result = null;
- try {
- Method method = target.getClass().getMethod(
- "get"
- + new StringBuffer(field.getName()).replace(0, 1,
- field.getName().substring(0, 1)
- .toUpperCase()), null);
- result = method.invoke(target,null);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
- this.jdbcTemplate = jdbcTemplate;
- }
- }
- 这是建立在jdbcTemplate上的ormapping,只要你的类的属性与数据表的字段名字一样,且有id这个主键,那么就可以用这个Session做增删查,比如你有个表 users(id,uname,upassword) ,类User(id,uname,upassword).
- 那么你就可以用上面的session对users这个表做增删改查,
- User user=new User();
- user.setId(1);
- user.setUname("zhangsan");
- user.setUpassword("password");
- session.save(user,"users");//增加
- session.delete(user,"users");//删除
- //他会跟据你user的属性的值进行组合查询,下面是按name,password的组合查询。
- User user=new User();
- user.setUpassword("password");
- user.setUname("zhangsan");
- session.selectObjectsByProperties(user,"users");