<typeAliases> <package name="com.entity"/> </typeAliases>3.传入多个对象可以通过@Parm注解给参数起别名:
public void show(@Param("") String name, String pwd);4.模糊查找:
select * from student where stuName like concat('%',#{stuName},'%')5.如果只是用MyBatis,那么每写一个Mapper文件要在核心配置文件里面加载:
<mappers> <mapper resource="com/mapper/StudentMapper.xml"></mapper> <mapper resource="com/mapper/GradeMapper.xml"></mapper> </mappers>6.结果集映射:实体类属性如果跟数据库字段名不一样,则默认匹配失败,可以
1.给字段起别名:SELECT stuName as '姓名' FROM `student` 2.使用resultMap <resultMap type="Student" id="BaseMap"> <id column="stuId" property="stuId"/> <result column="stuName" property="stuName"/> <result column="stuAge" property="stuAge"/> <result column="stuBirth" property="stuBirth"/> </resultMap> 7.sql映射文件中,resultMap可以继承,也可以引用 <resultMap type="Student" id="studentResultMap" extends="BaseMap"> </resultMap> <resultMap type="Grade" id="GradeMap" extends="BaseMap"> <collection property="students" ofType="Student" resultMap="com.dao.StudentDAO.BaseMap </resultMap>MyBatis下载地址 中文文档8,查询返回的是个普通实体类使用resultType,否则用resultMap 9.多对一的映射方式,association标签
<resultMap type="Student" id="studentResultMap" extends="BaseMap"> <association property="grade" javaType="Grade"> <id column="gradeId" property="gradeId"/> <result column="gradeName" property="gradeName"/> </association> </resultMap>10.一对多映射,collection标签 11.动态sql
1 if标签: 2 <select id="findActiveBlogLike" resultType="Blog"> 4 SELECT * FROM BLOG WHERE 1 = 1 5 <if test="title != null"> 6 AND title like #{title} 7 </if> 8 <if test="author != null and author.name != null"> 9 AND author_name like #{author.name} 10 </if> 11 </select> 12 13 choose、when、otherwise标签:传入了 “title” 就按 “title” 查找,传入了 “author” 14 就按 “author” 查找的情形。若两者都没有传入,就返回标记为 featured 的 BLOG 15 16 <select id="findActiveBlogLike" resultType="Blog"> 18 SELECT * FROM BLOG WHERE state = ‘ACTIVE’ 19 <choose> 20 <when test="title != null"> 21 AND title like #{title} 22 </when> 23 <when test="author != null and author.name != null"> 24 AND author_name like #{author.name} 25 </when> 26 <otherwise> 27 AND featured = 1 28 </otherwise> 29 </choose> 30 </select> 31 32 trim、where、set标签 33 34 where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。 35 而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。 36 <select id="findActiveBlogLike" resultType="Blog"> 38 SELECT * FROM BLOG 39 <where> 40 <if test="state != null"> 41 state = #{state} 42 </if> 43 <if test="title != null"> 44 AND title like #{title} 45 </if> 46 <if test="author != null and author.name != null"> 47 AND author_name like #{author.name} 48 </if> 49 </where> 50 </select> 51 52 set 元素会动态地在行首插入 SET 关键字, 53 并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的) 54 55 <update id="updateAuthorIfNecessary"> 56 update Author 57 <set> 58 <if test="username != null">username=#{username},</if> 59 <if test="password != null">password=#{password},</if> 60 <if test="email != null">email=#{email},</if>61 <if test="bio != null">bio=#{bio}</if> 62 </set> 63 where id=#{id} 64 </update> 65 66 foreach,collection:如果传入的是数组,用array,如果是list,用list 67 68 <select id="selectPostIn" resultType="domain.blog.Post"> 69 SELECT * 70 FROM POST P 71 WHERE ID in 72 <foreach item="item" index="index" collection="list" 73 open="(" separator="," close=")"> 74 #{item} 75 </foreach> 76 </select>
12.共同的sql语句可以放在一个sql标签中,用include引用
<sql id="sql"> </sql> <insert id=""> <include refid="sql"></include> </insert>