我们为什么要修改呢,这是因为我们查询的时候,我们有时候需要连表查询,例如我们需要查询出员工表的信息(emp_id,emp_name...)与此同时,我们还想查询出该员工所在的部门(dept_name)。此时,生成mapper文件查询的只是表中已有的字段。

所以我们可以在EmployeeMapper.xml中添加两个方法,

同时我们还要为Employee.java添加一个Department属性,别忘记增加setter和getter。

最后我们需要修改EmployeeMapper.xml文件,增加两个连表查询语句:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.atguigu.crud.dao.EmployeeMapper">
  4. <resultMap id="BaseResultMap" type="com.atguigu.crud.bean.Employee">
  5. <!--
  6. WARNING - @mbg.generated
  7. This element is automatically generated by MyBatis Generator, do not modify.
  8. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  9. -->
  10. <id column="emp_id" jdbcType="INTEGER" property="empId" />
  11. <result column="emp_name" jdbcType="VARCHAR" property="empName" />
  12. <result column="gender" jdbcType="CHAR" property="gender" />
  13. <result column="email" jdbcType="VARCHAR" property="email" />
  14. <result column="d_id" jdbcType="INTEGER" property="dId" />
  15. </resultMap>
  16. <resultMap type="com.atguigu.crud.bean.Employee" id="WithDeptResultMap">
  17. <id column="emp_id" jdbcType="INTEGER" property="empId" />
  18. <result column="emp_name" jdbcType="VARCHAR" property="empName" />
  19. <result column="gender" jdbcType="CHAR" property="gender" />
  20. <result column="email" jdbcType="VARCHAR" property="email" />
  21. <result column="d_id" jdbcType="INTEGER" property="dId" />
  22. <association property="department" javaType="com.atguigu.crud.bean.Department">
  23. <id column="dept_id" property="deptId"/>
  24. <result column="dept_name" property="deptName"/>
  25. </association>
  26. </resultMap>
  27. <sql id="Example_Where_Clause">
  28. <!--
  29. WARNING - @mbg.generated
  30. This element is automatically generated by MyBatis Generator, do not modify.
  31. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  32. -->
  33. <where>
  34. <foreach collection="oredCriteria" item="criteria" separator="or">
  35. <if test="criteria.valid">
  36. <trim prefix="(" prefixOverrides="and" suffix=")">
  37. <foreach collection="criteria.criteria" item="criterion">
  38. <choose>
  39. <when test="criterion.noValue">
  40. and ${criterion.condition}
  41. </when>
  42. <when test="criterion.singleValue">
  43. and ${criterion.condition} #{criterion.value}
  44. </when>
  45. <when test="criterion.betweenValue">
  46. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  47. </when>
  48. <when test="criterion.listValue">
  49. and ${criterion.condition}
  50. <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
  51. #{listItem}
  52. </foreach>
  53. </when>
  54. </choose>
  55. </foreach>
  56. </trim>
  57. </if>
  58. </foreach>
  59. </where>
  60. </sql>
  61. <sql id="Update_By_Example_Where_Clause">
  62. <!--
  63. WARNING - @mbg.generated
  64. This element is automatically generated by MyBatis Generator, do not modify.
  65. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  66. -->
  67. <where>
  68. <foreach collection="example.oredCriteria" item="criteria" separator="or">
  69. <if test="criteria.valid">
  70. <trim prefix="(" prefixOverrides="and" suffix=")">
  71. <foreach collection="criteria.criteria" item="criterion">
  72. <choose>
  73. <when test="criterion.noValue">
  74. and ${criterion.condition}
  75. </when>
  76. <when test="criterion.singleValue">
  77. and ${criterion.condition} #{criterion.value}
  78. </when>
  79. <when test="criterion.betweenValue">
  80. and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
  81. </when>
  82. <when test="criterion.listValue">
  83. and ${criterion.condition}
  84. <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
  85. #{listItem}
  86. </foreach>
  87. </when>
  88. </choose>
  89. </foreach>
  90. </trim>
  91. </if>
  92. </foreach>
  93. </where>
  94. </sql>
  95. <sql id="Base_Column_List">
  96. <!--
  97. WARNING - @mbg.generated
  98. This element is automatically generated by MyBatis Generator, do not modify.
  99. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  100. -->
  101. emp_id, emp_name, gender, email, d_id
  102. </sql>
  103. <sql id="WithDept_Column_List">
  104. e.emp_id, e.emp_name, e.gender, e.email, e.d_id,d.dept_id,d.dept_name
  105. </sql>
  106. <!-- 带部门的两个查询 -->
  107. <select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
  108. select
  109. <if test="distinct">
  110. distinct
  111. </if>
  112. <include refid="WithDept_Column_List" />
  113. from tbl_emp e left join tbl_dept d on e.d_id=d.dept_id
  114. <if test="_parameter != null">
  115. <include refid="Example_Where_Clause" />
  116. </if>
  117. <if test="orderByClause != null">
  118. order by ${orderByClause}
  119. </if>
  120. </select>
  121. <!-- 带部门的主键查询 -->
  122. <select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap">
  123. select
  124. <include refid="WithDept_Column_List" />
  125. from tbl_emp e left join tbl_dept d on e.d_id=d.dept_id where emp_id = #{empId,jdbcType=INTEGER}
  126.  
  127. </select>
  128. <select id="selectByExample" parameterType="com.atguigu.crud.bean.EmployeeExample" resultMap="BaseResultMap">
  129. <!--
  130. WARNING - @mbg.generated
  131. This element is automatically generated by MyBatis Generator, do not modify.
  132. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  133. -->
  134. select
  135. <if test="distinct">
  136. distinct
  137. </if>
  138. <include refid="Base_Column_List" />
  139. from tbl_emp
  140. <if test="_parameter != null">
  141. <include refid="Example_Where_Clause" />
  142. </if>
  143. <if test="orderByClause != null">
  144. order by ${orderByClause}
  145. </if>
  146. </select>
  147. <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  148. <!--
  149. WARNING - @mbg.generated
  150. This element is automatically generated by MyBatis Generator, do not modify.
  151. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  152. -->
  153. select
  154. <include refid="Base_Column_List" />
  155. from tbl_emp
  156. where emp_id = #{empId,jdbcType=INTEGER}
  157. </select>
  158. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  159. <!--
  160. WARNING - @mbg.generated
  161. This element is automatically generated by MyBatis Generator, do not modify.
  162. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  163. -->
  164. delete from tbl_emp
  165. where emp_id = #{empId,jdbcType=INTEGER}
  166. </delete>
  167. <delete id="deleteByExample" parameterType="com.atguigu.crud.bean.EmployeeExample">
  168. <!--
  169. WARNING - @mbg.generated
  170. This element is automatically generated by MyBatis Generator, do not modify.
  171. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  172. -->
  173. delete from tbl_emp
  174. <if test="_parameter != null">
  175. <include refid="Example_Where_Clause" />
  176. </if>
  177. </delete>
  178. <insert id="insert" parameterType="com.atguigu.crud.bean.Employee">
  179. <!--
  180. WARNING - @mbg.generated
  181. This element is automatically generated by MyBatis Generator, do not modify.
  182. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  183. -->
  184. insert into tbl_emp (emp_id, emp_name, gender,
  185. email, d_id)
  186. values (#{empId,jdbcType=INTEGER}, #{empName,jdbcType=VARCHAR}, #{gender,jdbcType=CHAR},
  187. #{email,jdbcType=VARCHAR}, #{dId,jdbcType=INTEGER})
  188. </insert>
  189. <insert id="insertSelective" parameterType="com.atguigu.crud.bean.Employee">
  190. <!--
  191. WARNING - @mbg.generated
  192. This element is automatically generated by MyBatis Generator, do not modify.
  193. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  194. -->
  195. insert into tbl_emp
  196. <trim prefix="(" suffix=")" suffixOverrides=",">
  197. <if test="empId != null">
  198. emp_id,
  199. </if>
  200. <if test="empName != null">
  201. emp_name,
  202. </if>
  203. <if test="gender != null">
  204. gender,
  205. </if>
  206. <if test="email != null">
  207. email,
  208. </if>
  209. <if test="dId != null">
  210. d_id,
  211. </if>
  212. </trim>
  213. <trim prefix="values (" suffix=")" suffixOverrides=",">
  214. <if test="empId != null">
  215. #{empId,jdbcType=INTEGER},
  216. </if>
  217. <if test="empName != null">
  218. #{empName,jdbcType=VARCHAR},
  219. </if>
  220. <if test="gender != null">
  221. #{gender,jdbcType=CHAR},
  222. </if>
  223. <if test="email != null">
  224. #{email,jdbcType=VARCHAR},
  225. </if>
  226. <if test="dId != null">
  227. #{dId,jdbcType=INTEGER},
  228. </if>
  229. </trim>
  230. </insert>
  231. <select id="countByExample" parameterType="com.atguigu.crud.bean.EmployeeExample" resultType="java.lang.Long">
  232. <!--
  233. WARNING - @mbg.generated
  234. This element is automatically generated by MyBatis Generator, do not modify.
  235. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  236. -->
  237. select count(*) from tbl_emp
  238. <if test="_parameter != null">
  239. <include refid="Example_Where_Clause" />
  240. </if>
  241. </select>
  242. <update id="updateByExampleSelective" parameterType="map">
  243. <!--
  244. WARNING - @mbg.generated
  245. This element is automatically generated by MyBatis Generator, do not modify.
  246. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  247. -->
  248. update tbl_emp
  249. <set>
  250. <if test="record.empId != null">
  251. emp_id = #{record.empId,jdbcType=INTEGER},
  252. </if>
  253. <if test="record.empName != null">
  254. emp_name = #{record.empName,jdbcType=VARCHAR},
  255. </if>
  256. <if test="record.gender != null">
  257. gender = #{record.gender,jdbcType=CHAR},
  258. </if>
  259. <if test="record.email != null">
  260. email = #{record.email,jdbcType=VARCHAR},
  261. </if>
  262. <if test="record.dId != null">
  263. d_id = #{record.dId,jdbcType=INTEGER},
  264. </if>
  265. </set>
  266. <if test="_parameter != null">
  267. <include refid="Update_By_Example_Where_Clause" />
  268. </if>
  269. </update>
  270. <update id="updateByExample" parameterType="map">
  271. <!--
  272. WARNING - @mbg.generated
  273. This element is automatically generated by MyBatis Generator, do not modify.
  274. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  275. -->
  276. update tbl_emp
  277. set emp_id = #{record.empId,jdbcType=INTEGER},
  278. emp_name = #{record.empName,jdbcType=VARCHAR},
  279. gender = #{record.gender,jdbcType=CHAR},
  280. email = #{record.email,jdbcType=VARCHAR},
  281. d_id = #{record.dId,jdbcType=INTEGER}
  282. <if test="_parameter != null">
  283. <include refid="Update_By_Example_Where_Clause" />
  284. </if>
  285. </update>
  286. <update id="updateByPrimaryKeySelective" parameterType="com.atguigu.crud.bean.Employee">
  287. <!--
  288. WARNING - @mbg.generated
  289. This element is automatically generated by MyBatis Generator, do not modify.
  290. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  291. -->
  292. update tbl_emp
  293. <set>
  294. <if test="empName != null">
  295. emp_name = #{empName,jdbcType=VARCHAR},
  296. </if>
  297. <if test="gender != null">
  298. gender = #{gender,jdbcType=CHAR},
  299. </if>
  300. <if test="email != null">
  301. email = #{email,jdbcType=VARCHAR},
  302. </if>
  303. <if test="dId != null">
  304. d_id = #{dId,jdbcType=INTEGER},
  305. </if>
  306. </set>
  307. where emp_id = #{empId,jdbcType=INTEGER}
  308. </update>
  309. <update id="updateByPrimaryKey" parameterType="com.atguigu.crud.bean.Employee">
  310. <!--
  311. WARNING - @mbg.generated
  312. This element is automatically generated by MyBatis Generator, do not modify.
  313. This element was generated on Thu Oct 18 13:46:16 CST 2018.
  314. -->
  315. update tbl_emp
  316. set emp_name = #{empName,jdbcType=VARCHAR},
  317. gender = #{gender,jdbcType=CHAR},
  318. email = #{email,jdbcType=VARCHAR},
  319. d_id = #{dId,jdbcType=INTEGER}
  320. where emp_id = #{empId,jdbcType=INTEGER}
  321. </update>
  322. </mapper>

现在,我们就能查询出带部门信息的员工表了。

SSM_CRUD新手练习(4)修改生成的mapper.xml映射文件的更多相关文章

  1. Mybatis学习--Mapper.xml映射文件

    简介 Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 映射文件中有很多属性,常用的就是parameterType(输入类型 ...

  2. Mybatis中的Mapper.xml映射文件sql查询接收多个参数

    ​ 我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收 ...

  3. Mapper.xml映射文件

    查询订单关联查询用户: 使用resultType,ordersCustom可以通过继承orders获得其属性,再添加我们需要的用户字段. 使用resultMap,orders表中通过封装user对象来 ...

  4. MyBatis 逆向工程——根据数据表自动生成model、xml映射文件、mapper接口

    MyBatis Generator(MBG)的使用 MBG可以根据数据表生成对应的model.xml映射文件.mapper接口,只是简单的生成,还需要根据需求修改. 1.下载jar包 https:// ...

  5. MyBatis - 3.Mapper XML映射文件

    SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): cache – 给定命名空间的缓存配置. cache-ref – 其他命名空间缓存配置的引用. resultMap – 是最复杂也是 ...

  6. mybatis Mapper XML 映射文件

    传送门:mybatis官方文档 Mapper XML 文件详解 一. 数据查询语句 1. select <select id="selectPerson" parameter ...

  7. MyBatis学习存档(3)——mapper.xml映射文件

    MyBatis 真正的强大在于映射语句,专注于SQL,功能强大,SQL映射的配置却是相当简单 所以我们来看看映射文件的具体结构 一.xml节点结构 mapper为根节点 - namespace命名空间 ...

  8. 在mapper.xml映射文件中添加中文注释报错

    问题描述: 在写mapper.xml文件时,想给操作数据库语句添加一些中文注释,添加后运行报如下错误: 思考 可能是写了中文注释,编译器在解析xml文件时,未能成功转码,从而导致乱码.但是文件开头也采 ...

  9. 整合mybaties 逆向生成 pojo mapper.xml

    第一步:配置properties 第二步:放入generatorConfig.xml文件  在总目录下  这个是生成工具 第三步:放入工具类,自动生成用的,  pom里面要加入6个依赖 第四步:运行u ...

随机推荐

  1. python re正则

    一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...

  2. Oracle_PL/SQL(2) 过程控制

    0.检索单行数据0.1使用标量变量接受数据例1: 7788declare v_ename emp.ename%type; v_sal emp.sal%type;begin select ename,s ...

  3. Oracle_SQL(6) 单行函数

    一.单行函数1.定义:对表或视图的查询时,针对每行记录返回一个值的函数.2.用途:用于select语句,where条件3.分类: 数值函数 Number Functions 字符函数(返回字符) Ch ...

  4. OpenSource.SerializationLibrary

    1. Cap'n Proto protocol buffer的主要作者之一创建的新项目.其主页描述Cap'n Proto的性能比PB快很多. http://kentonv.github.io/capn ...

  5. 洛谷1288 取数游戏II

    原题链接 因为保证有\(0\)权边,所以整个游戏实际上就是两条链. 很容易发现当先手距离\(0\)权边有奇数条边,那么必胜. 策略为:每次都将边上权值取光,逼迫后手向\(0\)权边靠拢.若此时后手不取 ...

  6. 关于LCA的倍增解法的笔记

    emmmmm近日刚刚学习了LCA的倍增做法,写一篇BLOG来加强一下印象w 首先 何为LCA? LCA“光辉”是印度斯坦航空公司(HAL)为满足印度空军需要研制的单座单发轻型全天候超音速战斗攻击机,主 ...

  7. ubuntu安装jre

    1)登录java官网,下载jre,并解压,解压后的jre文件夹移动到 /usr/lib/java 路径下 2)配置系统环境变量 JAVA_HOME CLASSPATH PATH 打开/etc/envi ...

  8. 社交类APP原型模板分享——QQ

    QQ是一款社交类的APP应用——聊天软件,支持多人群聊以及丰富有趣的娱乐功能. 此模板交互效果很丰富,主要有抽屉侧拉效果,滚动内容界面.标签组切换.选择组件触发按钮状态变化.点击下拉展开列表.点击弹出 ...

  9. [C#.Net]判断文件是否被占用的两种方法

    今天开发产线测试Tool时发现日志文件会几率性的被占用,上网浏览找到最简单的代码(API或者FileStream),在这里抛砖引玉下. 第一种方法:API using System.IO; using ...

  10. 【C#】详解C#委托

    目录结构: contents structure [+] 委托语法 泛型委托 委托链 lambda表达式 揭秘委托 类库中的委托 委托和反射 1.委托语法 本文会详细阐述委托的使用,以及实现,想必读者 ...