见贤思齐焉,孔老夫子诚不欺我。
第一 你得见----------------------------你都不知道有这个项目,那就什么都不用说了
井鼃不可以语于海者,拘于虚也;夏虫不可以语于冰者,笃于时也;曲士不可以语于道者,束于教也。
第二 那个工程必须真的贤-----------跟臭棋篓子下棋,越下越臭
第三 你得思-------------------------------把自己的糟糕的代码拉出来,跟"贤明"的代码比一比。别嫌丢人,哪个程序员不是从hello world起步的?
(上面的教程相当全,初级使用我认为只看前三节即可)
@Test public void testBeanHandlerByDBSource() { QueryRunner qr = new QueryRunner(new MyDBSource()); Student student = qr.query("select * from student where xh='02'", new BeanHandler<>( Student.class)); System.out.println(student.getXm() + " " + student.getXh()+" "+student.getBirth()); }其中MyDBSource只是一个获取数据源的工具类而已。
public class MyDBSource implements DataSource { private static String driverClassName = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3306/webexample"; private static String userName = "root"; private static String passWord = "root"; @Override public Connection getConnection() throws SQLException { try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return DriverManager.getConnection(url, userName, passWord); } }我们看看QueryRunner的构造函数
public class QueryRunner { private Connection connection; public QueryRunner(){ } public QueryRunner(DataSource ds){ try { connection=ds.getConnection(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //... }现在看重头戏,就是QueryRunner的一系列query方法
public class QueryRunner { public <T> T query(String sql, ResultSetHandler<T> rsh) { if (connection!=null) { return query(connection, sql, rsh); }else { System.out.println("connection is null!"); return null; } } public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh) { return query(conn, sql, new Object[]{}, rsh); } public <T> T query(Connection conn, String sql, Object[] params, ResultSetHandler<T> rsh){ T object=null; try{ PreparedStatement ps=conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { setParams(ps, i,params); } ResultSet rs=ps.executeQuery(); object=rsh.handle(rs); }catch (Exception e) { e.printStackTrace(); } return object; } } //为了方便,我这里只模拟了String与date型的数据 public void setParams(PreparedStatement ps,int i,Object[] params){ try { if (params[i] instanceof String) { ps.setString(i+1, (String) params[i]); } if (params[i] instanceof java.util.Date) { ps.setDate(i+1, (Date) params[i]); } } catch (Exception e) { e.printStackTrace(); } }
object=rsh.handle(rs);在junit中,我们给QueryRunner里面注入的就是BeanHandler类。
public class BeanHandler<T> implements ResultSetHandler<T>{ private Class<T> type; public BeanHandler(Class<T> type) this.type=type; } @Override public T handle(ResultSet rs) throws SQLException { T o=null; try { while (rs.next()) { o=BeanUtils.creatBean(rs, type); } } catch (Exception e) { e.printStackTrace(); } return o; } }
//BeanUtils.java public class BeanUtils { public static <T> T creatBean(ResultSet rs, Class<T> type) { T o = null; try { o = type.newInstance(); Field[] field = type.getDeclaredFields(); for (int i = 0; i < field.length; i++) { String filedName = field[i].getName(); Class<?> filedType = field[i].getType(); String methodName = "set" + filedName.substring(0, 1).toUpperCase() + filedName.substring(1); Method m = type.getDeclaredMethod(methodName, filedType); Object value=setValue(filedType,rs,filedName); m.invoke(o, value); } } catch (Exception e) { e.printStackTrace(); } return o; } public static Object setValue( Class<?> filedType, ResultSet rs, String filedName){ Object value=null; try { if (filedType.getName().equals("java.lang.String")) { value = rs.getString(filedName); } if (filedType.getName().equals("java.util.Date")) { value = rs.getDate(filedName); } } catch (Exception e) { e.printStackTrace(); } return value; } }
上面的代码,怎么说呢,我也不嫌贻笑大方。
慢慢来嘛#
下面的是对dbutis源码浅层次的分析:
http://blog.csdn.net/dlf123321/article/details/45172429
接下来,我就来比较一下,看看人家有多么牛逼,我自己有多么low
原文地址:http://blog.csdn.net/dlf123321/article/details/45173505