mybatis 个人笔记
来源:互联网 发布:淳萃洗发水 知乎 编辑:程序博客网 时间:2024/06/05 03:11
mapper.xml映射关系
ResultMap : 结果集映射配置,colume可以来源于一张表或者多张表,而ResultMap的type必须是一个包含完整属性的实体类。
extends : 新的ResultMap 继承 已有的 ResultMap 属性,类似java继承关系,通常本ResultMap 需要添加其他属性,成为不同集合。
id: 数据库id
result : property 类属性 ,column 数据库字段, javaType java类属性,jdbcType 数据库字段类型, typeHandler 类型转换器(可自定义)
select : 本页抽出一个结果集,可以是List,HashMap,HashSet ; 也可以调用其他Mapper的方法,注意引用时填写正确的Mapper路径名称。
一对一用association
一对多用collection
<resultMap type="com.camelot.core.domain.CmsConfig" id="CmsConfigResultMap" extends="ABCResultMap"><id property="id" column="config_id"/><result property="contextPath" column="context_path"/><result property="port" column="port"/><association property="m_markConfig" column="config_id" select="getMarkConfigByCfgId"/><association property="m_markConfig" column="config_id" select="com.camelot.mybatis.dao.core.CmsSiteDAO.findById"/><collection property="registerItems" column="config_id" select="findRegisterItemsByCfgId"/></resultMap><select id="getCmsConfigBySiteId" resultMap="CmsConfigResultMap" >select *fromjc_configwhereconfig_id = #{cfgId}</select><resultMap type="com.camelot.core.domain.MarkConfig" id="MarkConfigResultMap"><result property="on" column="mark_on"/><result property="minWidth" column="mark_width"/><result property="minHeight" column="mark_height"/><result property="imagePath" column="mark_image"/><result property="content" column="mark_content"/><result property="size" column="mark_size"/><result property="color" column="mark_color"/><result property="alpha" column="mark_alpha"/><result property="pos" column="mark_position"/><result property="offsetX" column="mark_offset_x"/><result property="offsetY" column="mark_offset_y"/></resultMap><select id="getMarkConfigByCfgId" resultMap="MarkConfigResultMap">SELECTmark_on,mark_width,mark_height,mark_image,mark_content,mark_size,mark_color,mark_alpha,mark_position,mark_offset_x,mark_offset_yFROMjc_configwhereconfig_id = #{cfgId}</select><resultMap type="java.util.HashMap" id="CmsConfigAttrResultMap"><result property="name" column="attr_name"/><result property="value" column="attr_value"/></resultMap><select id="findCmsConfigAttrByCfgId" resultMap="CmsConfigAttrResultMap" >select attr_name,attr_valuefromjc_config_attrwhereconfig_id = #{cfgId}</select>
若UserMenu类中有User类做属性 private User user,那么mapper.xml中用方式 #{user.id} 获取user的属性与 user_id 对应
<insert id="save" parameterType="com.camelot.cms.domain.assist.CmsUserMenu">insert into jc_user_menu (menu_name,menu_url,priority,user_id)values (#{name},#{url},#{priority},#{user.id})</insert>
追加1:公共查询字段的sql 、 以及公共条件判断的sql 可以预封装,在需要的地方直接 <include refid="***"> 引用,【试用于多处调用的sql,相比select * 更直观易读】
<!-- 查询字段公共sql --><sql id="selectAllColumns"><![CDATA[SELECT friendlink_id, site_id, friendlinkctg_id, site_name, domain, logo, email, description, views, is_enabled, priority FROM jc_friendlink record]]></sql><sql id="where_fragement">where 1=1<if test="entity!=null"><if test = "entity.friendlinkId != null and entity.friendlinkId != ''"><![CDATA[ and record.friendlink_id = #{entity.friendlinkId} ]]></if><if test = "entity.siteId != null and entity.siteId != ''"><![CDATA[ and record.site_id = #{entity.siteId} ]]></if><if test = "entity.friendlinkctgId != null and entity.friendlinkctgId != ''"><![CDATA[ and record.friendlinkctg_id = #{entity.friendlinkctgId} ]]></if><if test = "entity.siteName != null and entity.siteName != ''"><![CDATA[ and record.site_name = #{entity.siteName} ]]></if><if test = "entity.domain != null and entity.domain != ''"><![CDATA[ and record.domain = #{entity.domain} ]]></if><if test = "entity.logo != null and entity.logo != ''"><![CDATA[ and record.logo = #{entity.logo} ]]></if><if test = "entity.email != null and entity.email != ''"><![CDATA[ and record.email = #{entity.email} ]]></if><if test = "entity.description != null and entity.description != ''"><![CDATA[ and record.description = #{entity.description} ]]></if><if test = "entity.views != null and entity.views != ''"><![CDATA[ and record.views = #{entity.views} ]]></if><if test = "entity.isEnabled != null and entity.isEnabled != ''"><![CDATA[ and record.is_enabled = #{entity.isEnabled} ]]></if><if test = "entity.priority != null and entity.priority != ''"><![CDATA[ and record.priority = #{entity.priority} ]]></if></if></sql><!-- 分页查询数据集合--><select id="queryJcFriendlinkDTOListByPage" resultMap="jcFriendlinkDTOMapper" ><include refid="selectAllColumns"/><include refid="where_fragement" /><if test="pager!=null"><include refid="pagination_tail" /></if></select>
0 0
- mybatis 个人笔记
- Mybatis学习之个人笔记(一)
- mybatis和hibernate的配置----个人笔记
- [个人笔记]mybatis-spring使用步骤
- 个人学习笔记--MyBatis-的搭建及第一个程序
- 个人学习笔记--MyBatis官方推荐DAO开发方案
- Mybatis映射文件sql语句注意事项(个人笔记)
- Mybatis个人总结
- SpringMVC-Mybatis 个人总结
- Mybatis的个人经验
- Mybatis部分 个人总结
- Mybatis框架个人总结
- 个人学习笔记----基于Spring4.3.1+mybatis+postgresql+maven搭建的个人用调度平台(一)
- 个人学习笔记----基于Spring4.3.1+mybatis+postgresql+maven搭建的个人用调度平台(二)
- 个人学习笔记----基于Spring4.3.1+mybatis+postgresql+maven搭建的个人用调度平台(三)
- 个人学习笔记----基于Spring4.3.1+mybatis+postgresql+maven搭建的个人用调度平台(四)
- 个人笔记
- 个人笔记
- log4j.additivity用法和例子
- 枚举类完美解读
- Pixhawk之姿态解算篇(4)_补充篇
- python 定时执行脚本,通过设置定时任务执行python 脚本
- day1
- mybatis 个人笔记
- 使用Spring Security保护web应用安全
- ListView中条目指定位置停留和动画效果
- ZOJ 3780 - Paint the Grid Again
- jQuery源码解读一
- Unity 单例与实例的区别
- CentOS vim插件——Doxygen注释插件(用命令生成各种注释)
- FOJ Problem 2214 Knapsack problem
- 霍夫曼编码和解码