SQL映射文件的配置参数
一. 列名和Bean名不一致时---(ResultMap)
1:可以用起别名,在查询语句时候 lastname as last_name
2: 用 resultMap标签
1. type为Bean所在的根目录(此图为别名后)
2. id为了后面引入resultMap属性, resultMap一个为标签,另一个为属性(引入)
3. 里面的id为主键的名字
4. result为普通列名、
二: 一对一查询---(ResultMap)
1. ResultMap里面的association(关联另一张表,一对一)
<!-- 使用association定义关联的单个对象的封装规则 --> <resultMap type="cn.nyist.mybaitis.bean.Employee" id="GGG"> <id column="id" property="id"/> <result column="last_name" property="last_name"/> <result column="gender" property="gender"/> <!-- Association可以指定JavaBean对象 property=“对象名字例如:dept” 指定哪个属性是联合对象 JavaType:指定这个属性的对象l类型(JavaBean的地址) //这个地方不能省略 --> <association property="dept" javaType="cn.nyist.mybaitis.bean.Department"> <id column="did" property="id"/> <result column="depart_name" property="depart_name"/> </association> </resultMap> <select id="EmpAndeDeptByID2" resultMap="GGG"> SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id, d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>
三: Setting设置
1.0 setting (在主配置文件设置,并且在别名前面)
<!-- 自动注入resultMap的属性。resultMap时 不用一个一个导入不需要的-->
<settings> <setting name="autoMappingBehavior" value="FULL"/> </settings>