SORM 框架的实现
Posted 周无极
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SORM 框架的实现相关的知识,希望对你有一定的参考价值。
SORM框架
1 核心框架:
Query接口 负责查询(对外提供读物的核心类)
QueryFactory类 负责根据配置信息创建query对象
Typeconverto接口 负责类型转换
TableContext 类 负责获取管理数据库所有表结构和类结构的关系,并可以根据表结构生成类结构,
DBManager 类 根据配置信息,维持连接对象的管理增加连接池功能
工具类
JDBCUtil 封裝常用JDBC操作 StringUtil 封裝常用字符串操作
JavaFileUtil封裝java文件操作 ReflectUtil 封裝常用反射操作
client QueryFactory
<interface> DB2Query
query OracleQuery
mysqlQuery
TableContext
DBManager <interface> OracleTypeConvertor
TypeConvertor mysqlTypeConvertor
核心bean ,封裝相关数据
ColumnInfo 封裝表中一个字段的信息(字段类型、字段名、键类型)
Configuration 封裝配置文件信息
TableInfo 封裝一张表的信息
2、针对SORM框架的说明:
核心思想:使用简单、性能高、极易上手!
配置文件:
模卡使用资源文件、后期项目复杂后可以增加xml文件配置和注解。
类名有标明生成、只有受罪大写有区别,其他无区别
java对象的属性有表中字段生成,完全对应
目前,只支持表中只有一个主键,联合主键不支持
3.代码区
package com.bjsxt.po; import java.sql.*; import java.util.*; public class Dept { private Integer id; private String address; private String dname; public Integer getId(){ return id; } public String getAddress(){ return address; } public String getDname(){ return dname; } public void setId(Integer id){ this.id=id; } public void setAddress(String address){ this.address=address; } public void setDname(String dname){ this.dname=dname; } }
package com.bjsxt.po; import java.sql.*; import java.util.*; public class Emp { private Integer id; private java.sql.Date birthday; private Integer deptid; private String empname; private Integer age; private Double bonus; private Double salary; public Integer getId(){ return id; } public java.sql.Date getBirthday(){ return birthday; } public Integer getDeptid(){ return deptid; } public String getEmpname(){ return empname; } public Integer getAge(){ return age; } public Double getBonus(){ return bonus; } public Double getSalary(){ return salary; } public void setId(Integer id){ this.id=id; } public void setBirthday(java.sql.Date birthday){ this.birthday=birthday; } public void setDeptid(Integer deptid){ this.deptid=deptid; } public void setEmpname(String empname){ this.empname=empname; } public void setAge(Integer age){ this.age=age; } public void setBonus(Double bonus){ this.bonus=bonus; } public void setSalary(Double salary){ this.salary=salary; } }
package com.bjsxt.sorm.bean; /** * 封装表中一个字段的信息 * @author gaoiqi www.sxt.cn * @version 0.8 */ public class ColumnInfo { /** * 字段名称 */ private String name; /** * 字段的数据类型 */ private String dataType; /** * 字段的键类型(0:普通键,1:主键 2:外键) */ private int keyType; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDataType() { return dataType; } public void setDataType(String dataType) { this.dataType = dataType; } public int getKeyType() { return keyType; } public void setKeyType(int keyType) { this.keyType = keyType; } public ColumnInfo(String name, String dataType, int keyType) { super(); this.name = name; this.dataType = dataType; this.keyType = keyType; } public ColumnInfo() { } }
package com.bjsxt.sorm.bean; /** * 管理配置信息 * @author gaoqi www.sxt.cn * */ public class Configuration { /** * 驱动类 */ private String driver; /** * jdbc的url */ private String url; /** * 数据库的用户名 */ private String user; /** * 数据库的密码 */ private String pwd; /** * 正在使用哪个数据库 */ private String usingDB; /** * 项目的源码路径 */ private String srcPath; /** * 扫描生成java类的包(po的意思是:Persistence object持久化对象) */ private String poPackage; private String queryClass; private int poolMinSize; private int poolMaxSize; public String getQueryClass() { return queryClass; } public void setQueryClass(String queryClass) { this.queryClass = queryClass; } public int getPoolMinSize() { return poolMinSize; } public void setPoolMinSize(int poolMinSize) { this.poolMinSize = poolMinSize; } public int getPoolMaxSize() { return poolMaxSize; } public void setPoolMaxSize(int poolMaxSize) { this.poolMaxSize = poolMaxSize; } public Configuration() { } public Configuration(String driver, String url, String user, String pwd, String usingDB, String srcPath, String poPackage) { super(); this.driver = driver; this.url = url; this.user = user; this.pwd = pwd; this.usingDB = usingDB; this.srcPath = srcPath; this.poPackage = poPackage; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUsingDB() { return usingDB; } public void setUsingDB(String usingDB) { this.usingDB = usingDB; } public String getSrcPath() { return srcPath; } public void setSrcPath(String srcPath) { this.srcPath = srcPath; } public String getPoPackage() { return poPackage; } public void setPoPackage(String poPackage) { this.poPackage = poPackage; } }
package com.bjsxt.sorm.bean; /** * 封装了java属性和get、set方法的源代码 * @author gaoqi * */ public class JavaFieldGetSet { /** * 属性的源码信息。如:private int userId; */ private String fieldInfo; /** * get方法的源码信息.如:public int getUserId(){} */ private String getInfo; /** * set方法的源码信息.如:public void setUserId(int id){this.id = id;} */ private String setInfo; @Override public String toString() { System.out.println(fieldInfo); System.out.println(getInfo); System.out.println(setInfo); return super.toString(); } public String getFieldInfo() { return fieldInfo; } public void setFieldInfo(String fieldInfo) { this.fieldInfo = fieldInfo; } public String getGetInfo() { return getInfo; } public void setGetInfo(String getInfo) { this.getInfo = getInfo; } public String getSetInfo() { return setInfo; } public void setSetInfo(String setInfo) { this.setInfo = setInfo; } public JavaFieldGetSet(String fieldInfo, String getInfo, String setInfo) { super(); this.fieldInfo = fieldInfo; this.getInfo = getInfo; this.setInfo = setInfo; } public JavaFieldGetSet() { } }
package com.bjsxt.sorm.bean; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 存储表结构的信息 * @author gaoqi * */ public class TableInfo { /** * 表名 */ private String tname; /** * 所有字段的信息 */ private Map<String,ColumnInfo> columns; /** * 唯一主键(目前我们只能处理表中有且只有一个主键的情况) */ private ColumnInfo onlyPriKey; /** * 如果联合主键,则在这里存储 */ private List<ColumnInfo> priKeys; public List<ColumnInfo> getPriKeys() { return priKeys; } public void setPriKeys(List<ColumnInfo> priKeys) { this.priKeys = priKeys; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public Map<String, ColumnInfo> getColumns() { return columns; } public void setColumns(Map<String, ColumnInfo> columns) { this.columns = columns; } public ColumnInfo getOnlyPriKey() { return onlyPriKey; } public void setOnlyPriKey(ColumnInfo onlyPriKey) { this.onlyPriKey = onlyPriKey; } public TableInfo(String tname, Map<String, ColumnInfo> columns, ColumnInfo onlyPriKey) { super(); this.tname = tname; this.columns = columns; this.onlyPriKey = onlyPriKey; } public TableInfo() { } public TableInfo(String tname,List<ColumnInfo> priKeys, Map<String, ColumnInfo> columns ) { super(); this.tname = tname; this.columns = columns; this.priKeys = priKeys; } }
package com.bjsxt.sorm.core; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public interface CallBack { public abstract Object doExecute(Connection conn, PreparedStatement ps, ResultSet rs); }
package com.bjsxt.sorm.core; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import com.bjsxt.sorm.bean.Configuration; import com.bjsxt.sorm.pool.DBConnPool; /** * 根据配置信息,维持连接对象的管理(增加连接池功能) * @author Administrator * */ public class DBManager { private static Configuration conf; private static DBConnPool pool; static { //静态代码块 Properties pros = new Properties(); try { pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties")); } catch (IOException e) { e.printStackTrace(); } conf = new Configuration(); conf.setDriver(pros.getProperty("driver")); conf.setPoPackage(pros.getProperty("poPackage")); conf.setPwd(pros.getProperty("pwd")); conf.setSrcPath(pros.getProperty("srcPath")); conf.setUrl(pros.getProperty("url")); conf.setUser(pros.getProperty("user")); conf.setUsingDB(pros.getProperty("usingDB")); conf.setQueryClass(pros.getProperty("queryClass")); conf.setPoolMaxSize(Integer.parseInt(pros.getProperty("poolMaxSize"))); conf.setPoolMinSize(Integer.parseInt(pros.getProperty("poolMinSize"))); System.out.println(TableContext.class); } /*public static Connection getConn(){ try { Class.forName(conf.getDriver()); return DriverManager.getConnection(conf.getUrl(), conf.getUser(),conf.getPwd()); //直接建立连接,后期增加连接池处理,提高效率!!! } catch (Exception e) { e.printStackTrace(); return null; } }*/ public static Connection getConn() { if (pool == null) { pool = new DBConnPool(); } return pool.getConnection(); } public static Connection createConn() { try { Class.forName(conf.getDriver()); return DriverManager.getConnection(conf.getUrl(), conf.getUser(), conf.getPwd()); } catch (Exception e) { e.printStackTrace(); }return null; } public static void close(ResultSet rs, Statement ps, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } pool.close(conn); } public static void close(Statement ps, Connection conn) { try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } pool.close(conn); } public static void close(Connection conn) { pool.close(conn); } /** * 返回Configuration对象 * @return */ public static Configuration getConf(){ return conf; } }
package com.bjsxt.sorm.core; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.List; import com.bjsxt.po.Dept; import com.bjsxt.po.Emp; import com.bjsxt.sorm.bean.ColumnInfo; import com.bjsxt.sorm.bean.TableInfo; import com.bjsxt.sorm.utils.JDBCUtils; import com.bjsxt.sorm.utils.ReflectUtils; import com.bjsxt.vo.EmpVO; /** * 负责针对Mysql数据库的查询 * @author gaoqi * */ public class MySqlQuery extends Query { public static void testDML(){ Emp e = new Emp(); e.setEmpname("lily"); e.setBirthday(new java.sql.Date(System.currentTimeMillis())); e.setAge(30); e.setSalary(3000.8); e.setId(1); // new MySqlQuery().delete(e); // new MySqlQuery().insert(e); new MySqlQuery().update(e,new String[]{"empname","age","salary"}); } public static void testQueryRows(){ List<Emp> list = new MySqlQuery().queryRows("select id,empname,age from emp where age>? and salary<?", Emp.class, new Object[]{10,5000}); for(Emp e:list){ System.out.println(e.getEmpname()); } String sql2 = "select e.id,e.empname,salary+bonus \'xinshui\',age,d.dname \'deptName\',d.address \'deptAddr\' from emp e " +"join dept d on e.deptId=d.id "; List<EmpVO> list2 = new MySqlQuery().queryRows(sql2, EmpVO.class, null); for(EmpVO e:list2){ System.out.println(e.getEmpname()+"-"+e.getDeptAddr()+"-"+e.getXinshui()); } } public static void main(String[] args) { /* Number obj = (Number)new MySqlQuery().queryValue("select count(*) from emp where salary>?",new Object[]{1000}); Number obj = new MySqlQuery().queryNumber("select count(*) from emp where salary>?",new Object[]{1000}); System.out.println(obj.doubleValue());*/ //查询信息 List<Emp> emps=new MySqlQuery().queryRows("select * from emp", Emp.class, new Object[0]); for (Emp emp : emps) { System.out.println(emp.toString()); //Emp [id=1, birthday=2017-11-15, deptid=1, empname=周无极, age=12, bonus=20.0, salary=1200.0] } /*Number number=(Number)new MySqlQuery().queryValue("select count(*) from emp ",null); System.out.println(number.toString());*/ //添加信息 /* Dept dept=new Dept(); dept.setAddress("南京路"); dept.setDname("采购部"); new MySqlQuery().insert(dept);*/ } @Override public Object queryPagenate(int pageNum, int size) { return null; } }
package com.bjsxt.sorm.core; /** * mysql数据类型和java数据类型的转换 * @author gaoqi * */ public class MySqlTypeConvertor implements TypeConvertor { @Override public String databaseType2JavaType(String columnType) { //varchar-->String if("varchar".equalsIgnoreCase(columnType)||"char".equalsIgnoreCase(columnType)){ return "String"; }else if("int".equalsIgnoreCase(columnType) ||"tinyint".equalsIgnoreCase(columnType) ||"smallint".equalsIgnoreCase(columnType) ||"integer".equalsIgnoreCase(columnType) ){ return "Integer"; }else if("bigint".equalsIgnoreCase(columnType)){ return "Long"; }else if("double".equalsIgnoreCase(columnType)||"float".equalsIgnoreCase(columnType)){ return "Double"; }else if("clob".equalsIgnoreCase(columnType)){ return "java.sql.CLob"; }else if("blob".equalsIgnoreCase(columnType)){ return "java.sql.BLob"; }else if("d以上是关于SORM 框架的实现的主要内容,如果未能解决你的问题,请参考以下文章