利用jdbc写的一个类似DBUtils的框架
package com.jdbc.orm.dbutils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import com.jdbc.orm.handler.ResultsetHand; /** * * 功能描述:基于jdbc自己封装的orm小框架 * */ public class DBUtils { public static String username; public static String password; public static String driverClass; public static String url; public static Connection connection; public static PreparedStatement statement; public static ResultSet rs; static { Properties properties = new Properties(); InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(in);// 加载数据库的配置文件 driverClass = properties.get("jdbc.driverclass").toString(); username = properties.get("jdbc.username").toString(); password = properties.get("jdbc.password").toString(); url = properties.get("jdbc.url").toString(); } catch (IOException e) { e.printStackTrace(); try { Class.forName(driverClass);// 加载数据库的驱动程序 } catch (ClassNotFoundException e1) { e1.printStackTrace(); } } } /** * * 功能 :为SQL语句设置参数 * * @param sql * 输入的sql语句 * @param params * 需要的参数 * @throws SQLException */ public static void setParams(String sql, Object... params) throws SQLException { connection = DriverManager.getConnection(url, username, password); statement = connection.prepareStatement(sql); if ( params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { statement.setObject(i + 1, params[i]); } } } /** * * @功能 :根据ID查询实体 * @param sql * @param id * @return 返回结果集 * @throws Exception */ public static ResultSet queryBeanById(String sql, int id) throws Exception { setParams(sql, id); return statement.executeQuery(); } /** * * @功能 :查询功能 * @param sql * @param params * @return 将查询到的结果映射到一个实体中返回 * @throws SQLException */ public static Object query(String sql, ResultsetHand rsh, Object... params) throws SQLException { setParams(sql, params); ResultSet rs = statement.executeQuery(); return rsh.handler(rs); } /** * * @功能 :增删改功能 * @param sql * @param params * @throws SQLException */ public static int update(String sql, Object... params) throws SQLException { setParams(sql, params); return statement.executeUpdate(); } }
package com.jdbc.orm.handler; import java.lang.reflect.Field; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.List; /** * @描述 : 将查询到的结果映射到集合中 * @author 作者 jjkang * @version 创建时间:2017年10月6日 下午8:04:46 */ @SuppressWarnings("rawtypes") public class BeanListHandler implements ResultsetHand { private Class clazz; public BeanListHandler(Class clazz) { this.clazz = clazz; } @SuppressWarnings("unchecked") @Override public Object handler(ResultSet rs) { List list = new ArrayList(); try { ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { Object bean = clazz.newInstance(); for (int i = 0; i < columnCount; i++) { Object object = rs.getObject(i + 1); String name = metaData.getColumnName(i + 1); Field field = clazz.getDeclaredField(name); field.setAccessible(true); field.set(bean, object); } list.add(bean); } return list; } catch (Exception e) { throw new RuntimeException(e); } } }
package com.jdbc.orm.handler; import java.lang.reflect.Field; import java.sql.ResultSet; import java.sql.ResultSetMetaData; /** * @描述 :将查询的结果映射到实体中 * @author 作者 E-mail: jiajunkang@outlook.com * @version 创建时间:2017年10月6日 下午5:45:04 */ public class BeanHandler implements ResultsetHand { private Class clazz;// 将结果映射到的实体 public BeanHandler(Class clazz) { this.clazz = clazz; } @Override public Object handler(ResultSet rs) { try { if (!rs.next()) { return null; } Object bean = clazz.newInstance(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 0; i < columnCount; i++) { String name = metaData.getColumnName(i + 1);// 得出每一列的列名 Object value = rs.getObject(i + 1);// 得到每一列的值 Field field = clazz.getDeclaredField(name);// 利用反射,根据列名得到属性对象 field.setAccessible(true); field.set(bean, value); } return bean; } catch (Exception e) { throw new RuntimeException(e); } } }
package com.jdbc.orm.junit; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.jdbc.orm.bean.Student; import com.jdbc.orm.dbutils.DBUtils; import com.jdbc.orm.handler.BeanHandler; import com.jdbc.orm.handler.BeanListHandler; /** * 描述 : 测试DBUtils * @version 创建时间:2017年10月6日 下午2:40:45 */ public class TestDBUtils { DBUtils dbUtils = new DBUtils(); @Test public void test(){ System.out.println(dbUtils.driverClass); System.out.println(dbUtils.password); System.out.println(dbUtils.url); System.out.println(dbUtils.username); } @Test public void testSetParams() throws SQLException{ String sql = "select name from student where id = ?"; Object params[] = {1}; DBUtils.setParams(sql, params); } @Test public void testQueryBeanById() throws Exception{ String sql = "select id,name,chinese,english,math from student where id = ?"; ResultSet rs=dbUtils.queryBeanById(sql, 1); Student student = new Student(); while(rs.next()){ student.setId(rs.getInt(1)); student.setName(rs.getString(2)); student.setChinese(rs.getDouble(3)); student.setEnglish(rs.getDouble(4)); student.setMath(rs.getDouble(5)); } System.out.println(student); } @Test public void testUpdate() throws SQLException{ String sql = "update student set name=? where id =?"; Object[] params = {"李晓明",1}; int rows = dbUtils.update(sql, params); System.out.println(rows); } @Test public void testQuery() throws Exception{ String sql = "select id,name,chinese,english,math from student where id = ?"; Student student = new Student(); student = (Student) dbUtils.query(sql,new BeanHandler(Student.class), 1); System.out.println(student); } @Test public void testQuery1() throws Exception{ String sql = "select id,name,chinese,english,math from student"; List<Student> list = new ArrayList<>(); list = (List<Student>) dbUtils.query(sql,new BeanListHandler(Student.class), null); System.out.println(list.size()); System.out.println(list); } }