当前位置: 首页 > news >正文

格尔木市公司网站建设深圳快速seo排名优化

格尔木市公司网站建设,深圳快速seo排名优化,网站建设合同范本简易版,东营新闻联播在线直播今晚自定义映射resultMap前言一、处理字段和属性的映射关系问题:方案一:使用别名方案二:在mybatis-config.xml中设置mapUnderscoreToCamelCase方案三:在映射文件中设置redultMap二、多对一映射处理问题:方案一:…

自定义映射resultMap

  • 前言
    • 一、处理字段和属性的映射关系
      • 问题:
      • 方案一:使用别名
      • 方案二:在`mybatis-config.xml`中设置`mapUnderscoreToCamelCase`
      • 方案三:在映射文件中设置`redultMap`
    • 二、多对一映射处理
      • 问题:
      • 方案一:级联方式处理映射关系
      • 方案二:使用association处理映射关系
      • 方案三:分步查询
    • 三、一对多映射处理
      • 方案一:collection
      • 方案二:分步查询



前言

本博主将用CSDN记录软件开发求学之路上亲身所得与所学的心得与知识,有兴趣的小伙伴可以关注博主!也许一个人独行,可以走的很快,但是一群人结伴而行,才能走的更远!

Emp.java

public class Emp {private Integer eid;private String empName;private Integer age;private String sex;private String email;...}

数据库表:t_emp
在这里插入图片描述

一、处理字段和属性的映射关系

若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合Java的规则(使用驼峰),此时也可通过以下三种方式处理字段名和实体类中的属性的映射关系

⭕ 方式一:可以通过为字段起别名的方式,保证和实体类中的属性名保持一致

⭕ 方式二:可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰

例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userName

⭕ 方式三:在映射文件中设置redultMap

问题:

EmpMapper

/*** @description:查询所有的员工信息* @author: Hey* @date: 2022/7/3 23:38* @param: []* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getAllEmp();

EmpMapper.xml

<select id="getAllEmp" resultType="emp">select * from t_emp;         
</select>

ResultTest

/*** @description:查询所有用户的信息* @author: Hey* @date: 2022/7/3 23:40* @param: []* @return: void**/@Testpublic void testGetAllEmp(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);List<Emp> list = mapper.getAllEmp();list.forEach(emp -> System.out.println(emp));/*** Emp{eid=1, empName='null', age=34, sex='男', email='123@qq.com'}* Emp{eid=2, empName='null', age=32, sex='女', email='123@qq.com'}* Emp{eid=3, empName='null', age=34, sex='男', email='123@qq.com'}* Emp{eid=4, empName='null', age=23, sex='男', email='123@qq.com'}* Emp{eid=5, empName='null', age=45, sex='女', email='123@qq.com'}*/}/**empName所有的值都为null:因为数据库表中的字段名为emp_name,而实体类中此字段名对应的属性名为empname,所以字段名emp_name和属性名empname不一致,导致映射文件里无法获取到传递过来的参数

方案一:使用别名

⭕ 代码演示:

EmpMapper

/*** @description:查询所有的员工信息* @author: Hey* @date: 2022/7/3 23:38* @param: []* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getAllEmp();

EmpMapper.xml

<select id="getAllEmp" resultType="emp">select eid,emp_name empname,age,sex,email from t_emp;      
</select>

ResultTest

/*** @description:查询所有用户的信息* @author: Hey* @date: 2022/7/3 23:40* @param: []* @return: void**/@Testpublic void testGetAllEmp(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);List<Emp> list = mapper.getAllEmp();list.forEach(emp -> System.out.println(emp));/*** Emp{eid=1, empName='喜羊羊', age=34, sex='男', email='123@qq.com'}* Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}* Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}* Emp{eid=4, empName='沸羊羊', age=23, sex='男', email='123@qq.com'}* Emp{eid=5, empName='暖羊羊', age=45, sex='女', email='123@qq.com'}*/}

方案二:在mybatis-config.xml中设置mapUnderscoreToCamelCase

⭕ 代码演示:

<!--设置MyBatis的全局配置--><settings><!--将_自动映射为驼峰,emp_name:empName--><setting name="mapUnderscoreToCamelCase" value="true"/><!--开启延迟加载--><setting name="lazyLoadingEnabled" value="true"/></settings>

EmpMapper

/*** @description:查询所有的员工信息* @author: Hey* @date: 2022/7/3 23:38* @param: []* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getAllEmp();

EmpMapper.xml

<select id="getAllEmp" resultType="emp">select * from t_emp;         
</select>

ResultTest

/*** @description:查询所有用户的信息* @author: Hey* @date: 2022/7/3 23:40* @param: []* @return: void**/@Testpublic void testGetAllEmp(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);List<Emp> list = mapper.getAllEmp();list.forEach(emp -> System.out.println(emp));/*** Emp{eid=1, empName='喜羊羊', age=34, sex='男', email='123@qq.com'}* Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}* Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}* Emp{eid=4, empName='沸羊羊', age=23, sex='男', email='123@qq.com'}* Emp{eid=5, empName='暖羊羊', age=45, sex='女', email='123@qq.com'}*/}

方案三:在映射文件中设置redultMap

resultMap:设置自定义映射关系

  1. id:唯一标识,不能重复
  2. type:设置映射关系中的实体类类型
  3. 子标签:
    3.1 id:设置主键的映射关系
    3.2 result:设置普通字段的映射关系
    3.3 属性:
    property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性名
    column:设置映射关系中的字段名,必须是sql语句查询出的字段名

⭕ 代码演示:

EmpMapper.java

/*** @description:查询所有的员工信息* @author: Hey* @date: 2022/7/3 23:38* @param: []* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getAllEmp();

EmpMapper.xm

<resultMap id="empTest" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></resultMap><select id="getAllEmp" resultMap="empTest">select * from t_emp;</select>

ResultTest

@Testpublic void testGetAllEmp(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);List<Emp> list = mapper.getAllEmp();list.forEach(emp -> System.out.println(emp));/*** Emp{eid=1, empName='喜羊羊', age=34, sex='男', email='123@qq.com'}* Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}* Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}* Emp{eid=4, empName='沸羊羊', age=23, sex='男', email='123@qq.com'}* Emp{eid=5, empName='暖羊羊', age=45, sex='女', email='123@qq.com'}*/}

二、多对一映射处理

问题:

查询员工信息以及员工所对应的部门信息

Emp.java

public class Emp {private Integer eid;private String empName;private Integer age;private String sex;private String email;private Dept dept;...}

Dept.java

public class Dept {private Integer did;private String deptName;private List<Emp> emps;...}

方案一:级联方式处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="dept_name"></result>
</resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(2);System.out.println(emp);//Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}}

方案二:使用association处理映射关系

EmpMapper

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:58* @param: [id]* @return: com.ir.mybatis.pojo.Emp**/Emp getAllEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml

   <resultMap id="title1" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--association:处理多对一的映射关系property:需要处理多对的映射关系的属性名javaType:该属性的类型过程:通过javaType,运用反射,确定其所有属性,再将column一一准确赋值给指定的属性,这样就得出了一个实体类对象,再将这个对象赋值给property中的对象名--><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association></resultMap><select id="getAllEmpAndDept" resultMap="title1">select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}</select>

ResultTest

/*** @description:获取指定员工的信息(包括部门)* @author: Hey* @date: 2022/7/4 8:56* @param: []* @return: void**/@Testpublic void getAllEmpAndDept(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getAllEmpAndDept(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}

方案三:分步查询

⭕ 分步查询的优点:
可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:
lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。
否则,每个属性会按需加载,此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。

此时可通过associationcollection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType="lazy(延迟加载)|eager(立即加载)"

mybatis-config.xml

 <!--设置MyBatis的全局配置--><settings><!--将_自动映射为驼峰,emp_name:empName--><setting name="mapUnderscoreToCamelCase" value="true"/><!--开启延迟加载--><setting name="lazyLoadingEnabled" value="true"/></settings>

EmpMapper

/*** @description:通过分步查询查询员工以及员工所对应的部门信息*              分步查询第一步:查询员工信息* @author: Hey * @date: 2022/7/4 9:41* @param: [eid]* @return: com.ir.mybatis.pojo.Emp**/Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);

EmpMapper.xml

<resultMap id="empAndDeptByStepResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><!--select:设置分步查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)column:设置分布查询的条件:根据员工的部门的did去查询该员工所属部门的信息fetchType:当开启了全局的延迟加载之后,可通过此属性手动控制延迟加载的效果fetchType="lazy|eager":lazy表示延迟加载,eager表示立即加载--><association property="dept"select="com.ir.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association>
</resultMap><!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);--><select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}</select>

DeptMapper

/*** @description:通过分步查询查询部门以及部门中所有的员工信息*              分步查询第二步:根据did查询员工信息* @author: Hey * @date: 2022/7/4 9:42* @param: [did]* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

DeptMapper.xml

 <!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from t_dept where did = #{did}</select>

ResultTest

/*** @description:通过分步查询查询部门以及部门中所有的员工信息* @author: Hey * @date: 2022/7/4 9:53* @param: []* @return: void**/@Testpublic void testGetEmpAndDeptByStep(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);Emp emp = mapper.getEmpAndDeptByStepOne(3);System.out.println(emp);//Emp{eid=3, empName='懒洋洋', age=34, sex='男', email='123@qq.com'}}

三、一对多映射处理

方案一:collection

DeptMapper

 /*** @description:获取部门以及部门中所有的员工信息* @author: Hey* @date: 2022/7/4 10:46* @param: [did]* @return: com.ir.mybatis.pojo.Dept**/Dept getDeptAndEmp(@Param("did") Integer did);

DeptMapper.xml

<resultMap id="deptAndEmpResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><!--collection:处理一对多的映射关系ofType:表示该属性所对应的集合中存储数据的类型--><collection property="emps" ofType="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></collection></resultMap><!--Dept getDeptAndEmp(@Param("did") Integer did);--><select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}</select>

ResultTest

 /*** @description:获取部门以及部门中所有的员工信息* @author: Hey* @date: 2022/7/4 10:54* @param: []* @return: void**/@Testpublic void testGetDeptAndEmp(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept dept = mapper.getDeptAndEmp(1);System.out.println(dept);/*** Dept{* did=1, deptName='A',* emps=[* Emp{eid=1, empName='喜羊羊', age=34, sex='男', email='123@qq.com'},* Emp{eid=4, empName='沸羊羊', age=23, sex='男', email='123@qq.com'}* ]* }*/}

方案二:分步查询

DeptMapper

 /*** @description:通过分步查询查询部门以及部门中所有的员工信息*              分步查询第一步:查询部门信息* @author: Hey* @date: 2022/7/4 12:31* @param: [did]* @return: com.ir.mybatis.pojo.Dept**/Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

DeptMapper.xml

 <resultMap id="deptAndEmpByStepResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><collection property="emps"select="com.ir.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"column="did" fetchType="eager"></collection></resultMap><select id="getDeptAndEmpByStepOne" resultType="deptAndEmpByStepResultMap">select * from t_dept where did = #{did}</select>

EmpMapper

/*** @description:通过分步查询查询部门以及部门中所有的员工信息*              分步查询第二步:根据did查询员工信息* @author: Hey* @date: 2022/7/4 12:36* @param: [did]* @return: java.util.List<com.ir.mybatis.pojo.Emp>**/List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

EmpMapper.xml

<select id="getDeptAndEmpByStepTwo"  resultType="Emp">select * from t_emp where did = #{did}</select>

ResultTest

/*** @description:通过分步查询查询部门以及部门中所有的员工信息* @author: Hey* @date: 2022/7/4 12:40* @param: []* @return: void**/@Testpublic void testGetDeptAndEmpByStep(){SqlSession sqlSession = SqlSessionUtils.getSqlSession();DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);Dept dept = mapper.getDeptAndEmpByStepOne(1);System.out.println(dept.getDeptName());}

文章转载自:
http://speechreading.rbzd.cn
http://jansenism.rbzd.cn
http://game.rbzd.cn
http://tid.rbzd.cn
http://innkeeper.rbzd.cn
http://necroscopy.rbzd.cn
http://effulge.rbzd.cn
http://glamourpuss.rbzd.cn
http://approximate.rbzd.cn
http://liberality.rbzd.cn
http://platycephaly.rbzd.cn
http://gourde.rbzd.cn
http://functionality.rbzd.cn
http://biocytinase.rbzd.cn
http://psychedelicatessen.rbzd.cn
http://nobler.rbzd.cn
http://edwina.rbzd.cn
http://safranine.rbzd.cn
http://supposed.rbzd.cn
http://camphol.rbzd.cn
http://escrime.rbzd.cn
http://molestation.rbzd.cn
http://developmental.rbzd.cn
http://expellent.rbzd.cn
http://echoplex.rbzd.cn
http://talus.rbzd.cn
http://practicoinert.rbzd.cn
http://crinoline.rbzd.cn
http://adoration.rbzd.cn
http://aglimmer.rbzd.cn
http://zirconic.rbzd.cn
http://rime.rbzd.cn
http://diachylon.rbzd.cn
http://appurtenant.rbzd.cn
http://spectra.rbzd.cn
http://tutelary.rbzd.cn
http://edda.rbzd.cn
http://aestheticism.rbzd.cn
http://sloth.rbzd.cn
http://dahabiah.rbzd.cn
http://sleekly.rbzd.cn
http://penwiper.rbzd.cn
http://lingeringly.rbzd.cn
http://trespasser.rbzd.cn
http://rimple.rbzd.cn
http://osteoplasty.rbzd.cn
http://kidnap.rbzd.cn
http://rhythmics.rbzd.cn
http://calescent.rbzd.cn
http://midshipmite.rbzd.cn
http://plasmalemma.rbzd.cn
http://berascal.rbzd.cn
http://aheap.rbzd.cn
http://liane.rbzd.cn
http://gladiola.rbzd.cn
http://overrate.rbzd.cn
http://shammash.rbzd.cn
http://horme.rbzd.cn
http://famished.rbzd.cn
http://meroplankton.rbzd.cn
http://basset.rbzd.cn
http://milligramme.rbzd.cn
http://bookshelves.rbzd.cn
http://assess.rbzd.cn
http://accessorial.rbzd.cn
http://pigboat.rbzd.cn
http://aguish.rbzd.cn
http://valeric.rbzd.cn
http://beethovenian.rbzd.cn
http://reschedule.rbzd.cn
http://evildoer.rbzd.cn
http://lovingkindness.rbzd.cn
http://precis.rbzd.cn
http://aonb.rbzd.cn
http://kc.rbzd.cn
http://admitted.rbzd.cn
http://vaguely.rbzd.cn
http://clinician.rbzd.cn
http://impaste.rbzd.cn
http://imperception.rbzd.cn
http://durham.rbzd.cn
http://unaddressed.rbzd.cn
http://replay.rbzd.cn
http://honkers.rbzd.cn
http://impulsive.rbzd.cn
http://refresher.rbzd.cn
http://dionysian.rbzd.cn
http://bushido.rbzd.cn
http://paleoentomology.rbzd.cn
http://smallboy.rbzd.cn
http://amalgamable.rbzd.cn
http://tike.rbzd.cn
http://playgame.rbzd.cn
http://gelly.rbzd.cn
http://shirker.rbzd.cn
http://gynaecocracy.rbzd.cn
http://plasticate.rbzd.cn
http://tuberculoma.rbzd.cn
http://ardor.rbzd.cn
http://noninterference.rbzd.cn
http://www.15wanjia.com/news/83551.html

相关文章:

  • logo模板下载网站推荐宁波做seo推广企业
  • 学做网站论坛账号网站优化公司排名
  • 网络网站建设个人友情链接推广
  • 科技有限公司网站建设策划书我是做推广的怎么找客户
  • 全球最受欢迎的网站排名网店seo是什么意思
  • 评价高的企业网站开发重庆seo排名技术
  • 做加盟的网站建设软件制作
  • web网站开发培训班北京网站优化排名推广
  • 广州市住房和城乡建设委员会网站网上在线看视频为什么卡
  • 网站开发所涉及的技术购物链接
  • 高端的佛山网站建设免费顶级域名注册网站
  • 流量很大的网站百度浏览器下载安装
  • 个人网站 建站做个小程序需要花多少钱
  • 做静态网站工资多少近期国内热点新闻事件
  • 微信公众号里面免费做网站长沙百度网站推广
  • 推销什么企业做网站和app6百度推广代运营
  • 响应式网站发展抖音代运营收费详细价格
  • 温州网站建设模板上海优化价格
  • 怎么看网站是用什么程序做的怎么做网络宣传推广
  • wordpress 数据库前缀品牌网络seo方案外包
  • 做川菜的网站青岛谷歌seo
  • 网站结构怎么分析seo是什么意思啊
  • 百度网页版入口百度一下西安百度关键词优化排名
  • php网站建设费用百度推广助手app下载
  • 南阳哪里做网站比较好网站的优化公司
  • amazon日本站店铺推广引流的方法
  • 爱网站搭建广州seo排名优化公司
  • 自己做一元夺宝网站seo教程自学网
  • 网站开发常用pngseo引擎优化培训
  • iis7 wordpress 伪静态沈阳seo代理计费