大搜车orm框架设计思路
kncb5891
8年前
<h2>orm基本概念</h2> <p>ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候,就不需要再去和复杂的SQL语句打交道,只需简单的操作对象的属性和方法。</p> <p>简单轻量的orm框架,对于提高整个团队的开发效率及代码可维护性具有很大的意义,下面介绍一下大搜车基于mybatis开发的orm框架开发出发点及基本的设计思路,希望能够给大家带来一些启发。</p> <h2>开发自己的orm框架的起因</h2> <p>大搜车的线上业务是从2013年6月份开始的,那时候我们做的业务是寄售业务,最多的业务场景是集中在后台业务逻辑处理及报表统计上面,开始的时候,我们的数据库操作是基于mybatis的底层来做的,mybatis本身是java领域最常用的一个轻量半自动数据库orm持久层框架,从oobject-relational-mapping的角度来说,它主要是解决数据返回层的java类映射的问题。从开发模式上,mybatis是把开发人员的sql放到xml配置文件中,任何对表的操作,事实还是需要自行写大量的sql来进行操作的。</p> <p>在我们开始使用mybatis的过程中,虽然业务上看起来还不是很复杂,但是事实上开发人员在编写sql的过程中,已经开始突显很多问题:</p> <ul> <li>由于业务一直在迭代,我们的表结构经常需要做一些调整,但是在调整表结构的时候,开发人员经常会漏调整部分sql中的字段,导致部分逻辑出现问题</li> <li>由于业务逻辑的关系,有的表字段有好几十个,编写插入及查询sql时,很考验开发人员的细心程度,而且写sql的工作量也比较大,不同人写的sql方法,风格各异。</li> <li>那时候我们后台管理系统中,大量使用了join的操作,从整体系统的可维护性来说,对一个表进行查询操作的sql落在不同xml文件中,系统极难维护,对系统后续的调整和升级埋下了很多隐患。 下面是我们当时一个典型的sql例子:</li> </ul> <pre> <code class="language-sql"><select id="queryCarBasicInfo" resultType="com.souche.vo.carbasic.CarBasicVO"> SELECT a.id,a.car_brand AS brand,a.car_series AS series,a.car_model AS model,a.first_register_date AS register, a.license_plate_province AS province,a.license_plate_city AS city,a.car_mileage AS mileage,a.flag AS flag, a.car_new_price AS carNewPrice,a.car_now_price AS carNowPrice,a.insure_expire_date AS insureExpireDate, a.year_check_date AS yearCheckDate,a.car_status AS carStatus, a.car_input_user AS USER,a.car_deadline AS deadline, a.car_parking_space AS carParkingSpace,a.car_source AS carSource,a.car_trans_fee AS carTransFee,a.car_store AS carStore, a.lushi_describe AS lushiDescribe,a.engine_describe AS engineDescribe,a.body_trim_describe AS bodyTrimDescribe, a.painting_describe AS paintingDescribe,a.date_create AS dateCreate,a.date_update AS dateUpdate,a.souche_number AS soucheNumber, a.date_up AS dateUp,a.date_down AS dateDown, a.date_trade AS dateTrade,a.car_discount AS carDiscount, b.car_level AS carLevel,b.car_age AS carAge, c.picture_url_big AS carPicture, f.car_transmission_type as carTransmissionType, h.begin_date AS flashBeginDate,h.end_date AS flashEndDate FROM car_basic_info a LEFT JOIN car_library_level b ON a.id=b.carId LEFT JOIN car_pictures c ON a.id=c.carId <if test="label != null"> left join carlibrary_label_info g on a.id=g.carId and other_label is null </if> left join carview_basic_parameter f on a.id=f.carId LEFT JOIN index_car h ON a.id=h.car_id AND h.type ='3' AND h.date_delete is NULL WHERE a.date_delete IS NULL AND a.car_status='zaishou' <if test="brand != null"> AND a.car_brand = #{brand} </if> <if test="series != null"> AND a.car_series = #{series} </if> <if test="model != null"> AND f.car_model = #{model} </if> <if ……> ... </if> </select></code></pre> <p>当时从整个业务逻辑sql的编写上,事实上我们虽然还是处于最早期的阶段,但是sql的维护上我认为我们已经开始有明显趋于混乱的迹象。为了尽量从根源上解决问题,我们开始尝试寻找一种更好的真正的orm策略,我们先思考了自己开发的基本方案,同时也对hibernate做了初步的调研,也想过是否直接使用hibernate来替换掉mybatis,但是最终还是决定自己开发。没有直接采用hibernate,主要是基于下面的几点考虑:</p> <ol> <li>hibernate配置上,还是很难真正达到极简或完全不需要配置的程度</li> <li>hibernate的使用上过于灵活,实现同一种目的可能会有很多种不同的写法,使用上不规范的话,可能会带来极大的性能问题,而且事实上我们整体要往hibernate上迁的话,业务sql上要做不少改动。</li> <li>我们团队没有完全吃透hibernate的人</li> </ol> <p>而从另一方面,mybatis本身透露给外面的接口会简单得多,所以我们决定基于mybatis进行二次开发。我们希望自研的orm策略需要含有下面的基本原则:</p> <ol> <li>能够规范开发人员编写普通业务的sql,不需要直接写sql(或写很少的sql),能够极大简化开发人员编写业务sql的时间</li> <li>开发人员在使用时,调试上比较便利,而且不容易出错。</li> <li>原有的业务sql,能够比较便利地迁移到新的orm框架上面</li> <li>对一条数据进行insert/update时,把创建时间/更新时间自动填入默认的数据库字段中</li> <li>尽量基于mybatis进行二次开发</li> </ol> <h2>基本设计思路</h2> <p>下面说一下大搜车orm的基本设计思路:</p> <ol> <li>基于annotation自动进行or-mapping映射的操作,在对java bean有操作需求时,加载对应的table,自动去映射表结构与java bean的关系。</li> <li>定义几个通用的mybatis数据库操作,把所有对数据库表的增删改查操作,都映射到这几个mybatis配置文件里定义的sql上去。</li> <li>对mybatis的返回结果,默认使用map进行传递,我们再自行做map2Object的java bean转化,从而保证与mybatis内部的传递及对外部接口返回的透明。</li> </ol> <p>下面是我们定义的通用mybatis sql:</p> <pre> <code class="language-sql"><mapper namespace="orm"> <select id="ALL_TABLE" resultType="string"> show tables </select> <select id="TABLE_SCHEMA" resultType="com.souche.optimus.dao.mapping.TableColumn"> desc ${table} </select> <insert id="insert" useGeneratedKeys="true" keyProperty="__pid"> ${sql} </insert> <select id="update" resultType="HashMap"> ${sql} </select> <select id="delete" resultType="HashMap"> ${sql} </select> <select id="select" resultType="HashMap"> ${sql} </select> </mapper></code></pre> <h3>数据表关系映射</h3> <p>在要操作的object中,加上@SqlTable映射,表示某一个java bean与表的映射关系。另外在做具体映射时,基于下面的规则进行表与数据字段的映射:</p> <ol> <li>如果bean中的字段上有定义@SqlColumn的声明,则使用表中的字段名与该字段做为映射关系。</li> <li>如果bean中对应的的字段没有定义@SqlColumn,则把javaObject中的字段名及表中的字段名都同步做同样的normalize,再进行字段比对。normalize的规则是去除掉下划线及中划线,然后同步转化为大写字母。如果两个的字段名一样,则认为它们是匹配上的。<br> 如java bean中有dateCreate字段,数据库中有date_create字段,则它们都normalize成DATECREATE,这样就表示javabean中的dateCreate与数据库中的date_create是映射上的</li> </ol> <p>下面是一个简单的映射例子:</p> <pre> <code class="language-sql">CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` varchar(45) DEFAULT NULL, `numbers` varchar(45) DEFAULT NULL, `date_create` timestamp NULL DEFAULT NULL, `date_update` timestamp NULL DEFAULT NULL, `date_delete` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `date_create` (`date_create`) ); @SqlTable("test") public class TestDO implements Serializable { private Long id; // 对应表中的主键id private String userId; // 对应表中的字段user_id @SqlField("numbers") private String nnn; // 对应numbers字段 private Timestamp dateCreate; // 对应date_create字段 private Timestamp dateUpdate; private Timestamp dateDelete; ... get/set函数 }</code></pre> <h3>基本实现策略</h3> <pre> <code class="language-sql">@Override public <E> List<E> findListByQuery(QueryObj query, Class<? extends E> cls) { Map<String, Object> queryMap = Maps.newHashMap(); // 构建查询的sql生成器 SqlBuilder sqlBuilder = new QuerySqlBuilder(query, cls); // queryMap中,产生sql语句及要送入mybatis中的查询参数 sqlBuilder.buildSql(queryMap); // 执行mybatis中的orm.select语句,得到返回结果 List<Map<String, Object>> retMaps = sqlSession.selectList("orm.select", queryMap); List<E> rets = new ArrayList<E>(); for(Map<String, Object> map : retMaps) { // 把返回结果映射为cls类 E ret = SqlAnnotationResolver.convertToObject(map, cls); if(ret != null) { rets.add(ret); } } return rets; }</code></pre> <p>上面这段代码片断,是产生查询语句的代码片断,从代码片断中可以基本看出我们的实现思路:</p> <ol> <li>构建一个map,用于传递mybatis中的上下文</li> <li>对于不同的数据库查询操作,构建不同的SqlBuilder,所有sqlBuilder执行完以后,在map对象中,会产生一个key为"sql"的值,用于表示要执行的sql语句。另外map对象中,还会包含要传递进入mybatis中的参数列表</li> <li>执行完mybatis以后,把返回的结构转化为需要的对象。</li> </ol> <h2>基本使用例子</h2> <p>假设要操作一个用户表,表的基本定义及javabean定义是</p> <pre> <code class="language-sql">CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id', `phone` varchar(20) NOT NULL DEFAULT '' COMMENT '电话', `name` varchar(64) DEFAULT NULL COMMENT '姓名', `age` int(5) DEFAULT '0' COMMENT '年龄', `sallary` float(12) DEFAULT '0' COMMENT '薪水', `height` float(5) DEFAULT NULL comment '身高', `weight` float(5) DEFAULT NULL comment '体重', `date_create` datetime DEFAULT NULL, `date_update` datetime DEFAULT NULL, PRIMARY KEY (`id`), unique KEY (`phone`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';</code></pre> <p>要对该表进行操作,只需要定义一个java bean及声明:</p> <pre> <code class="language-sql">@SqlTable("user") public class UserDO extends BaseVo { private Integer id; //ID private String phone; private String name; //姓名 private Integer age; //年龄 private Date birthday; //生日 private BigDecimal sallary; //薪水 private Double height; //身高 private Float weight; //体重 private Timestamp dateCreate; private Timestamp dateUpdate; }</code></pre> <h3>基本的增删改查</h3> <p>下面以用户表的简单操作为例子,说明一下orm的基本用法。</p> <p>插入一条新的用户</p> <pre> <code class="language-sql">UserDO user = new UserDO(); user.setPhone("13735914821"); user.setName("hello world"); user.setAge(20); user.setHeight(1.75); user.setWeight(65.1f); int id = basicDao.insert(user); // 返回的用户id 产生的sql: insert into user(name, age, height, weight, phone, date_create, date_update) values (#{name}, #{age}, #{height}, #{weight}, #{phone}, #{dateCreate}, #{dateUpdate})</code></pre> <p>查询主键id为4的用户</p> <pre> <code class="language-sql">UserDO u = basicDao.findObjectByPrimaryKey(4, UserDO.class); 上面的语句产生的sql为: select a.name as name, a.id as id, a.age as age, a.birthday as birthday, a.sallary as sallary, a.height as height, a.weight as weight, a.phone as phone, a.date_create as dateCreate, a.date_update as dateUpdate from user a where a.id = #{id}</code></pre> <p>根据电话查询用户</p> <pre> <code class="language-sql">UserDO sample = new UserDO(); sample.setPhone("13735914822"); UserDO ret = basicDao.findObjectBySample(sample, UserDO.class); 产生的sql为: select a.name as name, a.id as id, a.age as age, a.birthday as birthday, a.sallary as sallary, a.height as height, a.weight as weight, a.phone as phone, a.date_create as dateCreate, a.date_update as dateUpdate from user a where 1=1 and a.phone = #{phone} order by a.date_create desc limit 0, 1</code></pre> <p>根据特定条件使用like查询</p> <pre> <code class="language-sql">查询年龄为20岁,且手机号以137开头,且体重大于60公斤的用户: String key = "137"; UserDO sample = new UserDO(); sample.setAge(20); QueryObj queryObj = new QueryObj(sample); queryObj.addQuery("weight >= #{weight}", 60); queryObj.addQuery("phone like #{query}", key + "%"); List<UserDO> user = basicDao.findListByQuery(queryObj, UserDO.class); 产生的sql: select a.name as name, a.id as id, a.age as age, a.birthday as birthday, a.sallary as sallary, a.height as height, a.weight as weight, a.phone as phone, a.date_create as dateCreate, a.date_update as dateUpdate from user a where 1=1 and a.age = #{age} and ((a.weight >= #{_weight}) and (a.phone like #{_query})) order by a.date_create desc limit 0, 1000 其中,查询map中,age为20, _weight为60, _query为137%</code></pre> <h3>join查询的使用说明</h3> <p>大搜车orm框架中,对表的join操作策略,设计得是相对比较简洁的一个点,下面以一个简单的例子,来说明一下orm join的用法</p> <p>假设在上面用户表的基础上增加一个用户操作记录表,表的结构及java bean的定义如下:</p> <pre> <code class="language-sql">CREATE TABLE `user_operator` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) COMMENT '用户id', `operate_type` varchar(20) DEFAULT NULL COMMENT '用户操作类型', `app_name` varchar(20) DEFAULT '' COMMENT 'app name', `op_time` datetime default null comment '用户操作时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户操作表'; 用户操作记录bean: @SqlTable("user_operator") public class UserOpteratorDO { private Integer id; //ID private Integer userId; // user id private String operateType; // 操作类型 private String appName; // 应用名 private Timestamp opTime; // 操作时间 ... // getter setter } 用户操作记录传输dto: public class UserOpteratorDTO { private UserDO user; private UserOpteratorDO operator; public void setUser(UserDO user) { this.user = user; } public UserDO getUser() { return user; } public UserOpteratorDO getOperator() { return operator; } public void setOperator(UserOpteratorDO operator) { this.operator = operator; } }</code></pre> <p>下面要查询用户操作类型为add或者click,操作时间为2017-03-20 00:00 ~ 2017-03-21 10:00,且用户的手机号以"137"开头的所有用户操作记录</p> <pre> <code class="language-sql">// 以UserDO为主表进行查询 QueryObj queryObj = new QueryObj(new UserDO()); // 对主表加上phone的like语句 queryObj.addQuery("UserDO.phone like #{phone}", "137%"); // 加上对表UserOpteratorDO的join操作,join操作为inner join,表的级联以主表的id和operator表的user_id作为关联主键 JoinParam joinParam = new JoinParam(UserOpteratorDO.class, JoinType.INNER_JOIN, new JoinPair("id", "userId")); queryObj.addJoinParam(joinParam); // 加上操作类型列表 List<String> typeList = new ArrayList<String>(); typeList.add("add"); typeList.add("click"); // 对UserOpteratorDO表的查询语句 queryObj.addQuery("UserOpteratorDO.operateType in #{type}", typeList); queryObj.addQuery("UserOpteratorDO.opTime >= #{startTime}", "2017-03-20 00:00"); queryObj.addQuery("UserOpteratorDO.opTime <= #{endTime}", "2017-03-20 10:00"); // 操作1,返回结果为UserOpteratorDTO类型 List<UserOpteratorDTO> operators = basicDao.findListByQuery(queryObj, UserOpteratorDTO.class); 返回的sql语句为: select a.name as __user_name, a.id as __user_id, a.age as __user_age, a.birthday as __user_birthday, a.sallary as __user_sallary, a.height as __user_height, a.weight as __user_weight, a.phone as __user_phone, a.date_create as __user_dateCreate, a.date_update as __user_dateUpdate, b.id as __operator_id, b.user_id as __operator_userId, b.operate_type as __operator_operateType, b.app_name as __operator_appName, b.op_time as __operator_opTime from user a INNER JOIN user_operator b on a.id = b.user_id where 1=1 and ((a.phone like #{_phone}) and (b.operate_type in ('add','click')) and (b.op_time >= #{_startTime}) and (b.op_time <= #{_endTime})) order by a.date_create desc limit 0, 1000 其中,_phone的值为137%, _startTime的值为 2017-03-20 00:00 操作2,最后一步查询操作,如果返回结果为UserDO类型 // generate query List<UserDO> operators = basicDao.findListByQuery(queryObj, UserDO.class); 对应的sql为: select a.name as name, a.id as id, a.age as age, a.birthday as birthday, a.sallary as sallary, a.height as height, a.weight as weight, a.phone as phone, a.date_create as dateCreate, a.date_update as dateUpdate from user a INNER JOIN user_operator b on a.id = b.user_id where 1=1 and ((a.phone like #{_phone}) and (b.operate_type in ('add','click')) and (b.op_time >= #{_startTime}) and (b.op_time <= #{_endTime})) order by a.date_create desc limit 0, 1000 操作3,最后一步的查询操作,如果返回的结果为UserOperateDO类型 // generate query List<UserOpteratorDO> operators = basicDao.findListByQuery(queryObj, UserOpteratorDO.class); 对应的sql为: select a.id as id, b.user_id as userId, b.operate_type as operateType, b.app_name as appName, b.op_time as opTime from user a INNER JOIN user_operator b on a.id = b.user_id where 1=1 and ((a.phone like #{_phone}) and (b.operate_type in ('add','click')) and (b.op_time >= #{_startTime}) and (b.op_time <= #{_endTime})) order by a.date_create desc limit 0, 1000</code></pre> <p> </p> <p>来自:https://blog.souche.com/da-sou-che-ormkuang-jia-she-ji-si-lu/</p> <p> </p>