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

乐辰科技网站建设企业网站推广方法

乐辰科技网站建设,企业网站推广方法,做网站建设多少钱,安徽响应式网站推荐8.1、resultMap处理字段和属性的映射关系 若字段名和实体类中的属性名不一致&#xff0c;则可以通过resultMap设置自定义映射 <!--resultMap&#xff1a;设置自定义映射属性&#xff1a;id&#xff1a;表示自定义映射的唯一标识type&#xff1a;查询的数据要映射的实体类的…

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

若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射

<!--resultMap:设置自定义映射属性:id:表示自定义映射的唯一标识type:查询的数据要映射的实体类的类型子标签:id:设置主键的映射关系result:设置普通字段的映射关系association:设置多对一的映射关系collection:设置一对多的映射关系属性:property:设置映射关系中实体类中的属性名column:设置映射关系中表中的字段名
-->
<resultMap id="userMap" type="User"><id property="id" column="id"></id><result property="userName" column="user_name"></result><result property="password" column="password"></result><result property="age" column="age"></result><result property="sex" column="sex"></result>
</resultMap>
<!--List<User> testMohu(@Param("mohu") String mohu);-->
<select id="testMohu" resultMap="userMap"><!--select * from t_user where username like '%${mohu}%'-->select id,user_name,password,age,sex from t_user where user_name like concat('%',#{mohu},'%')
</select>

若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合Java的规则(使用驼峰)

此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系

  1. 可以通过为字段起别名的方式,保证和实体类中的属性名保持一致
  2. 可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰
<settings><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

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

在这里插入图片描述

List<User> getAllUser();
<select id="getAllUser" resultType="User">select * from t_user
</select>

查询结果:

[User(id=4, userName=admin, password=123456, age=18, gender=男, email=), User(id=5, userName=admin, password=123456, age=18, gender=男, email=)]

8.2、多对一映射处理

场景模拟:

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

8.2.1、级联方式处理映射关系

<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id><result column="ename" property="ename"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><result column="did" property="dept.did"></result><result column="dname" property="dept.dname"></result>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid}
</select>

8.2.2、使用association处理映射关系

<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id><result column="ename" property="ename"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><association property="dept" javaType="Dept"><id column="did" property="did"></id><result column="dname" property="dname"></result></association>
</resultMap>
<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid}
</select>

8.2.3、分步查询

1、查询员工信息

/**
* 通过分步查询查询员工信息
* @param eid
* @return
*/
Emp getEmpByStep(@Param("eid") int eid);
<resultMap id="empDeptStepMap" type="Emp"><id column="eid" property="eid"></id><result column="ename" property="ename"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><!--select:设置分步查询,查询某个属性的值的sql的标识(namespace.sqlId)column:将sql以及查询结果中的某个字段设置为分步查询的条件--><association property="dept" select="com.atguigu.MyBatis.mapper.DeptMapper.getEmpDeptByStep" column="did"></association>
</resultMap><!--Emp getEmpByStep(@Param("eid") int eid);-->
<select id="getEmpByStep" resultMap="empDeptStepMap">select * from t_emp where eid = #{eid}
</select>

2、根据员工所对应的部门id查询部门信息

/**
* 分步查询的第二步: 根据员工所对应的did查询部门信息
* @param did
* @return
*/
Dept getEmpDeptByStep(@Param("did") int did);
<!--Dept getEmpDeptByStep(@Param("did") int did);-->
<select id="getEmpDeptByStep" resultType="Dept">select * from t_dept where did = #{did}
</select>

8.3、一对多映射处理

8.3.1、collection

/**
* 根据部门id查新部门以及部门中的员工信息
* @param did
* @return
*/
Dept getDeptEmpByDid(@Param("did") int did);
<resultMap id="deptEmpMap" type="Dept"><id property="did" column="did"></id><result property="dname" column="dname"></result><!--ofType:设置collection标签所处理的集合属性中存储数据的类型--><collection property="emps" ofType="Emp"><id property="eid" column="eid"></id><result property="ename" column="ename"></result><result property="age" column="age"></result><result property="sex" column="sex"></result></collection>
</resultMap><!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did = emp.did where dept.did = #{did}
</select>

8.3.2、分步查询

1、查询部门信息

/**
* 分步查询部门和部门中的员工
* @param did
* @return
*/
Dept getDeptByStep(@Param("did") int did);
<resultMap id="deptEmpStep" type="Dept"><id property="did" column="did"></id><result property="dname" column="dname"></result><collection property="emps" fetchType="eager" select="com.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid" column="did"></collection>
</resultMap><!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">select * from t_dept where did = #{did}
</select>

备注:javaType和ofType都是用来指定对象类型的,但是javaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型。

2、根据部门id查询部门中的所有员工

/**
* 根据部门id查询员工信息
* @param did
* @return
*/
List<Emp> getEmpListByDid(@Param("did") int did);
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">select * from t_emp where did = #{did}
</select>

分步查询的优点:可以实现延迟加载

但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载

也可以通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType=“lazy(延迟加载)|eager(立即加载)”

更多关于延迟加载的资料可以参考:https://www.cnblogs.com/zbh355376/p/14986307.html

本文章参考B站 【尚硅谷】SSM框架全套教程,MyBatis+Spring+SpringMVC+SSM整合一套通关,仅供个人学习使用,部分内容为本人自己见解,与尚硅谷无关。

http://www.15wanjia.com/news/3755.html

相关文章:

  • 响应式网站建设案例班级优化大师手机版下载
  • 做移动端网站软件开发最好的免费建站网站
  • 有什么手机做网站的长沙网站优化seo
  • 还有哪些网站做产品众筹市场调研方法有哪些
  • 什么网站可以做任务赚钱网站到首页排名
  • 长沙抖音代运营公司北京seo推广外包
  • 有哪些做拎包入住的网站网络营销活动策划方案模板
  • 网站建设培训机构市场调研的基本流程
  • ps做网站效果图关键词优化seo公司
  • 免费咨询服务合同模板上海seo推广服务
  • 在深圳注册公司需要多少钱seo技术服务外包
  • 政府门户网站充分体现了 的建设理念整合营销
  • 做网站流量优化都是什么免费推广网站大全下载
  • 本地环境搭建网站百度竞价排名什么意思
  • 什么网站专门做二手物品大型网站建站公司
  • 行业网站需要如何做360竞价推广怎么做
  • wordpress标题数据表西安seo推广优化
  • 做外贸的怎样才能上国外网站软文之家
  • 建筑资源网站搜索词分析工具
  • 字体设计学习网站营销型网站建设推荐
  • 找美工做网站多少钱盛大游戏优化大师
  • 网站建设项目详情大金seo
  • 手机端网站开发要注意什么设计培训学院
  • 安徽住房和城乡建设部网站首页软件推广平台
  • 域名备案 没有网站吗推广网
  • 做网站前需要准备什么条件百度一下搜索引擎
  • 扁平式网站模板关键词歌曲
  • 鲜花网站建设策划书外贸建站网站推广
  • 沧州网站建设的技术方案衡阳seo优化推荐
  • 做网站用什么牌子电脑热点事件营销案例