定义sql映射xml文件

  userMapper.xml文件的内容如下:

<!--头文件-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace:
命名空间:区分不同空间下的同名SQLID
A: findlAll
B: findAll
-->
<mapper namespace="cn.happy.dao.IStudentInfoDAO">
<!--SQL标签
id:唯一锁定到SQL标识
paramenterType:SQL语句的入参 可以省略
resultType:
增删除操作:不能 写
查询:单个实体的类型
-->
<sql id="columns">
stuid,stuname,stuage,studate
</sql>
<resultMap id="studentMap" type="StudentInfo">
<!-- <result column="stuname2" property="stuName"></result>-->
</resultMap> <select id="findAll" resultMap="studentMap">
/*SQL文:SQL语句*/
select <include refid="columns"></include> from studentinfo
</select>
<!--按主键查询-->
<select id="getStudentById" resultType="StudentInfo">
select * from studentinfo WHERE stuid=#{stuId}
</select>

  

 <!--添加学生-->
<insert id="addStudent">
insert into studentinfo( stuName,stuAge,stuDate) VALUES (#{stuName},#{stuAge},#{stuDate})
</insert> <!--修改学生-->
<update id="updateStudent">
update studentinfo set stuName= #{stuName} WHERE stuId=#{stuId}
</update> <!--删除学生-->
<delete id="deleteStudent">
delete from studentinfo WHERE stuId=#{stuId}
</delete> <!--模糊查询-->
<select id="findStudentListLike" resultType="StudentInfo">
<!--select * from studentinfo where stuname like concat('%',#{stuName},'%') and stuAge>#{stuAge}-->
select * from studentinfo where stuname like '%${stuName}%' and stuAge>#{stuAge}
</select> <!--多条件查询-->
<select id="findStudentsByCondition" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{stuName} '%' and stuAge>#{stuAge}
</select> <!--多条件查询使用索引-->
<select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
select * from studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
</select> <!--智能标签foreach List-->
<select id="findByForeachListStudent" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stu">
#{stu.stuId}
</foreach>
</if>
</where>
</select>
<!--智能标签foreach List-->
<select id="findByForeachList" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="list.size>0">
stuid in
<foreach collection="list" open="(" close=")" separator="," item="stuno">
#{stuno}
</foreach>
</if>
</where>
</select>

  

 <!--智能标签foreach Array-->
<select id="findByForeachArray" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="array.length>0">
stuid in
<foreach collection="array" open="(" close=")" separator="," item="stuno">
#{stuno}
</foreach>
</if>
</where>
</select> <!--智能标签choose-->
<select id="findByChoose" resultType="StudentInfo">
select * from studentinfo
<where>
<choose>
<when test="stuName!=null">
and stuName like '%' #{stuName} '%'
</when>
<when test="stuAge!=null">
and stuAge>#{stuAge}
</when>
<otherwise>
and 1=2
</otherwise>
</choose>
</where>
</select> <!--智能标签if-->
<select id="findByIf" resultType="StudentInfo">
select * from studentinfo
<where>
<if test="stuName!=null"><!--用户录入的姓名字段-->
and stuName like '%' #{stuName} '%'
</if>
<if test="stuAge!=null">
and stuAge>#{stuAge}
</if>
</where>
</select>
</mapper>

  

最新文章

  1. Mac如何删除MySQL,Mac下MySQL卸载方法
  2. Jsoup 使用教程:输入
  3. Percona XtraDB Cluster(转)
  4. window8.1中用户的管理员权限的提升方法
  5. 基础知识 - Rabin-Karp 算法
  6. 坑爹JDK8,可怜的XP
  7. tree(简单并差集)
  8. iOS在地图上WGS84、GCJ-02、BD-09互转解决方案
  9. 【Harmony】概述
  10. [WinForm]dataGridView动态加载以本地图片显示列
  11. genymotion中app不能安装问题
  12. Zabbix系列之七——添加磁盘IO监测
  13. 集腋成裘-01 sublime常用的快捷键
  14. JDK 1.8 新特性
  15. NLP第9章 NLP 中用到的机器学习算法——基于统计学(文本分类和文本聚类)
  16. AndroidのTextView之CompoundDrawable那些坑
  17. Linux常用命令 查找文件
  18. ASP.NET Core2读写InfluxDB时序数据库
  19. SQL查询表结构的语句
  20. Mac下PHP+Apache+MySQL环境搭建

热门文章

  1. POJ2154 Color【 polya定理+欧拉函数优化】(三个例题)
  2. regular
  3. [ZJU 2112] Dynamic Rankings
  4. 关于yolo 模型中1X1卷积层的作用
  5. Day03:集合、文件处理和函数基础
  6. MFC绘制直角坐标系
  7. Tomcat自定义classLoader加密解密
  8. 17.Identity Server 4回顾
  9. js的call()通俗解释
  10. java 中判断字符串相等