更多的修改意味着更多的风险,为了减少这方面的风险,同时为了减少配置的工作量,所以在最新的项目中采用了Fluent NHibernate中的Automapping。 要做到Automapping,就一定要定义好严格的命名规范,然后按照规范编写Automapping规则,实现自动化的映射。 (比如Course和Student是多对多关系,那么产生的中间表表名为COURSE_STUDENT) 对于枚举,在数据库中使用tinyint也就是一个Byte来存储,枚举在Automapping中作为UserType 下面就来编写Automapping的转换规则,首先对String写一个扩展方法,实现CostCenter到COST_CENTER的转换: static string ToDatabaseName(this mappings.IncludeBase(includeBaseType); } mappings.Conventions.Setup(GetConventions());//指定了Automapping
配置 准备工作完成,今天我们要说下FN的AutoMapping,之前呢我们都是用了FluentMapping进行手动的Map映射,如果可以的话,请大家还是使用之前的映射方式,因为AutoMapping有很多契约 .BuildSessionFactory(); } 这是我们手动映射,Mapping()中指定了用FluentMappings进行映射,我们只要把它改成AutoMapping No,无需映射了,因为我们用了AutoMapping,一切交给FN吧,我们接下来只要编写测试代码即可。 这个其实就是AutoMapping在处理映射的时候,都是按照一定的规则去映射的,细心的朋友可能发现,我们的索引字段的字段名都是Id,这就是AutoMapping的约定,我们也可以自己来进行配置。 今天介绍了AutoMapping简单的介绍,但在使用中您要时刻注意,AutoMapping是有很多限制的,比如Id,比如Component等,当然我们可以通过重写DefaultAutomappingConfiguration
--一对一--> <resultMap id="oneToOneResultMap" type="Order" autoMapping="true"> <! --一对多--> <resultMap id="oneToManyResultMap" type="Order" autoMapping="true"> <! --映射user子对象--> <association property="user" javaType="User" autoMapping="true"> < --多对多--> <resultMap id="manyToManyResultMap" type="Order" autoMapping="true"> <! --映射user子对象--> <association property="user" javaType="User" autoMapping="true"> <
自动映射开关 mybatis中自动映射主要有2种配置,一种是全局的配置,对应用中所有的resultMap起效,这个是在mybatis配置文件中进行设置的;另外一种是通过resultMap的autoMapping mybatis判断某个resultMap是否开启自动映射配置的时候,会先查找自身的autoMapping属性,如果这个属性设置值了,就直接用这个属性的值,如果resultMap元素的autoMapping 结果解释 由于mybatis全局配置中将autoMappingBehavior的值置为了NONE,表示全局自动映射被关闭了,而resultMapper中的orderModelMap4没有配置autoMapping 结果解释 orderModelMap5中没有指定autoMapping属性,所以自动映射会走全局配置的规则,即PARTIAL,会进行自动映射。 autoMapping使用 上面我们有说过,当在resultMap中指定了autoMapping属性之后,这个resultMap的自动映射就受autoMapping属性的控制,和mybatis中全局映射配置
-- #查询学生的信息,并且包括学生的学习的课程 --> <resultMap type="Student" id="studentAndCourseResultMap" autoMapping= -- 如果是多个表组合autoMapping默认是false,如果不想所有字段都写,要显示声明autoMapping="true" --> <! -- 一个映射调用另一个映射文件的操作是,使用"命名空间.操作ID" --> <association property="address" column="aid" autoMapping= -- #查询学生的信息,并且包括学生的学习的课程 --> <resultMap type="Student" id="studentAndCourseResultMap" autoMapping= -- 如果是多个表组合autoMapping默认是false,如果不想所有字段都写,要显示声明autoMapping="true" --> <!
将列名自动映射为属性 Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); // 解析 type ATTLIST resultMap id CDATA #REQUIRED type CDATA #REQUIRED extends CDATA #IMPLIED autoMapping (true|false 2.5、获取继承结果集和自动映射 String extend = resultMapNode.getStringAttribute("extends"); Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); 这个两个属性都是在配置 XML 的时候可有可无的。 extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping
resultMap> 会把列名和属性名进行绑定,该节点一共有 4 个属性: id :表示该 resultMap,供其他的语句调用 type:表示其对应的pojo类型,可以使用别名,也可以使用全限定类名 autoMapping javaType")))); // extends 属性 String extend = resultMapNode.getStringAttribute("extends"); // autoMapping 属性 Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping"); // 从注册的类型管理器里面查找对应的类型 > type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping resultMap ResultMap resultMap = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping
--查询订单,查询订单对应的产品--> <resultMap id="findAll_product" type="com.yh.pojo.Orders" autoMapping="true"> *******resultMap:findByIdMap 定义********************--> <resultMap id="findByIdMap" type="Order" autoMapping -- 映射到产品表 --> <association property="product" autoMapping="true"> <id property= -- 映射到会员表 --> <association property="member" autoMapping="true"> <id property=" M:N 多对多的关系 --> <collection property="travellers" ofType="Traveller" autoMapping="true">
<resultMap id="selectspuSaleAttrListCheckBySkuMap" type="com.auguigu.gmall.bean.PmsProductSaleAttr" autoMapping collection property="pmsProductSaleAttrValueList" ofType="com.auguigu.gmall.bean.PmsProductSaleAttrValue" autoMapping <resultMap id="selectspuSaleAttrListCheckBySkuMap" type="com.auguigu.gmall.bean.PmsProductSaleAttr" autoMapping type是我们多重集合中最外层的实体对象,之后我们就需要定义该实体对象的主键即可,column指的是我们定义的返回数据中的字段名,property则是指的是我们在实体类中定义的主键,剩下的字段我们通过autoMapping collection property="pmsProductSaleAttrValueList" ofType="com.auguigu.gmall.bean.PmsProductSaleAttrValue" autoMapping
通过添加autoMapping属性可以忽略自动映射等级配置,你可以启用或者禁用自动映射指定的ResultMap。 <resultMap id="userResultMap" type="User" autoMapping="false"> <result property="password" column="
namespace 命名空间 唯一的--> <mapper namespace="com.teng.mapper.StuMapper"> <resultMap id="stuMap" type="Stu" autoMapping --多对一关联--> <association property="teacher" column="teacher_id" javaType="Teacher" autoMapping --一对多的第一种写法,一般考虑到性能,不会这样实现--> <resultMap id="TeacherMap" type="Teacher" autoMapping="true"> < collection property="stus" ofType="Stu" column="id" autoMapping="true"> </collection> </resultMap join stu s on t.id = s.teacher_id </select> <resultMap type="Teacher" id="teacherMaps" autoMapping
--登录认证--> <resultMap id="ByUserNameAnRole" type="UserInfo" autoMapping="true"> <id property="id" column --映射role--> <collection property="roles" ofType="Role" javaType="List" autoMapping="true"> <id property --用户详情--> <resultMap id="userInfoById" type="UserInfo" autoMapping="true"> <id column="id --用户对应的角色--> <collection property="roles" ofType="Role" javaType="List" autoMapping="true"> --角色对应的资源--> <collection property="permissions" ofType="Permission" javaType="List" autoMapping
--查询订单,查询订单对应的产品--> <resultMap id="orders" type="Orders" autoMapping="true"> <id column=" --查询订单详情--> <resultMap id="findByIdresultMap" type="Orders" autoMapping="true"> <! --映射Product--> <association property="product" javaType="Product" autoMapping="true"> --映射member--> <association property="member" javaType="Member" autoMapping="true"> --映射traveller--> <collection property="travellers" javaType="List" ofType="Traveller" autoMapping
= declaredField.getAnnotation(FieldAutoMapping.class); if(null == autoMapping){ continue; } String methodName = autoMapping.method(); String type = autoMapping.type(); Method getMethod = clazz.getDeclaredMethod(methodName); continue; } String methodName = autoMapping.method(); String type = autoMapping.type(); Method getMethod = clazz.getDeclaredMethod(methodName);
-- autoMapping = true 自动映射: 当javabean的属性名和结果集的字段名一致, 自动映射 --> <resultMap id= "myorder" type="orders" autoMapping="true"> <! 和结果集之间的映射 注意 最左原则 --> <association property="user" javaType="user" autoMapping -mapper.dtd"> <mapper namespace="com.lijw.dao.UserMapper"> <resultMap id="myuser" type="user" autoMapping result property="role_desc" column="role_desc"/> <collection property="list" ofType="user" autoMapping
--一对多的第一种写法,一般考虑到性能,不会这样实现--> <resultMap id="TeacherMap" type="Teacher" autoMapping="true"> < collection property="stus" ofType="Stu" column="id" autoMapping="true"> </collection> </resultMap join stu s on t.id = s.teacher_id </select> <resultMap type="Teacher" id="teacherMaps" autoMapping
MetaObject metaObject, String columnPrefix) throws SQLException { List<UnMappedColumnAutoMapping> autoMapping autoMapping.isEmpty()) { for (UnMappedColumnAutoMapping mapping : autoMapping) { final
=============================== Walterlv1Vo -> Walterlv1Dao (Destination member list) Walterlv.Demo.AutoMapping.Models.Walterlv1Vo -> Walterlv.Demo.AutoMapping.Models.Walterlv1Dao (Destination member list) Unmapped properties: Description
id = #{id} </select> <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.Item" autoMapping = #{id} </select> <resultMap id="Item" type="me.chanjar.mybatislearn.inherit.resultmap.ItemEx" autoMapping
因此我们在确认没有重复名称之后就可以手动设置autoMapping="true",覆盖MyBatis的全局配置。 --嵌套查询关联文章表--> <resultMap id="articleMap" type="Article" autoMapping="true"> <id property="id" column --嵌套查询关联文章表--> <resultMap id="articleMap" type="Article" autoMapping="true"> <result property="createTime create_time FROM comment WHERE article = #{id} </select> <resultMap id="commentMap" type="Comment" autoMapping --嵌套查询关联文章表--> <resultMap id="articleMap" type="Article" autoMapping="true"> <id property="id" column