mybatis之联合查询

假定我们已经有相应的bean和配置基础文件ok

我值说一个关键注意的地方:见下方的注解

mybatis 插入数据,返回自增主键,useGeneratedKeys="true"keyProperty="id"(javaBean属性)keyColumn="ID"(数据库字段名)

#{?}---?,property为对应的javaBean(module)属性,column对应数据库的字段名;


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="student">
 <sql id="studentColumn">
  fid,
  fname,
  fbirthday
 </sql>
 <select id="getAll" resultType="studentBean" resultMap="studentResultMap">
  select stu.fid fid,
   stu.fname fname,
   dept.fid deptid,
   dept.fname deptname,
   stu.fbirthday
  from t_student stu
   inner join t_afei_dept dept on dept.fid = stu.fdept
 </select>
 
 <select id="getByID" resultType="studentBean" resultMap="studentResultMap">
  select stu.fid fid,
   stu.fname fname,
   dept.fid deptid,
   dept.fname deptname,
   stu.fbirthday
  from t_student stu
   inner join t_afei_dept dept on dept.fid = stu.fdept
  where stu.fid = #{id}
 </select>
 
 <insert id="add" parameterType="studentBean">
  insert into t_student(fid, fname, fbirthday) values
   ( newbosid('SFSAFSDF'),
    #{name, jdbcType=VARCHAR},
    #{birthday}
   )
 </insert>
 
 <delete id="deleteById" parameterType="int">
  delete from t_student where fid = #{id}
 </delete>
 
 <update id="updateStudent" parameterType="studentBean">
  update t_student t set t.fname = #{name}, t.fbirthday = #{birthday}
  where t.fid = #{id}
 </update>
 
 <!-- 数据库字段和Bean对象字段映射 -->
 <resultMap type="studentBean" id="studentResultMap">
  <id property="id" column="fid"/>
  <result property="name" column="fname"/>
  <result property="birthday" column="fbirthday"/>
  
  <!-- 联合查询 property中的名字应该和deptBean这个名字一致
   column中的名字应该和查询时的别名一样 -->
  <association property="dept" column="fdept" javaType="deptBean">
   <id property="id" column="deptid"/>
   <result property="name" column="deptname"/>
   <result property="num" column="deptnum"/>
  </association>
 </resultMap>
 </mapper>
更多mybatis信息请参考: http://www.mybatis.org/core/zh/index.html

你可能感兴趣的:(mybatis之联合查询)