一、注解配置

目前MyBatis支持注解配置,用注解方式来替代映射文件,但是注解配置还是有点不完善,在开发中使用比较少,大部分的企业还是在用映射文件来进行配置。不完善的地方体现在于当数据表中的字段名与实体对象的属性名不一致时,需要配置ResultMap,但是注解的配置只能是在方法前,也就是当前方法能使用这个配置,其他的方法是不能应用此注解配置的ResultMap。

注解配置的方式是直接在数据访问接口的方法前加上相应的注解

@select(“select * from  student ”)

public List<Student> find();

在MyBatis的主配置文件的mappers节点中注册所有的数据持久化接口类

案例:

数据持久化接口

public interface SubjectDao {

@Select("select * from subject")

@Results({

@Result(id=true,column="subjectNo",property="id"),

@Result(column="subjectName",property="subjectName"),

@Result(column="classHour",property="hour")

})

public List<Subject> find();

}

MyBatis主配置文件的mappers节点配置

<!-- 如果有映射时,对映射文件进行配置 -->

<mappers>

<!-- 加载单个映射文件,允许映射文件与持久接口类不在同一目录下。 -->

<mapper resource="com/icss/mapping/AdminDao.xml"/>

<mapper resource="com/icss/mapping/GradeDao.xml"/>

<mapper resource="com/icss/mapping/StudentDao.xml"/>

<!-- 注解配置时,需要在主配置文件中注册数据持久化接口类 -->

<mapper class="com.icss.dao.SubjectDao"/>

<!-- 加载这个包中的所有映射文件,要求持久化接口与映射文件必须在同一个目录中 -->

<!-- <package name="com.icss.dao"/>  -->

</mappers>

当表中的数据行有一对多或多对一的映射关系时的处理方式

一对多:

//一对多  : many

@Select("select * from grade")

@Results(

{

@Result(id=true, column="gradeId",property="gradeId"),

@Result(column="gradeName", property="gradeName"),

@Result(column="gradeId",property="subjects",

many=@Many(select = "com.etc.dao.SubjectDao.findSubjectByGradeId"))

}

)

public List<Grade> find();

多对一:

@Select("select * from subject")

@Results({

@Result(id=true,column="subjectNo",property="id"),

@Result(column="subjectName",property="subjectName"),

@Result(column="classHour",property="hour"),

//多对一的情况操作,根据年级编号调用年级的数据持久化接口的方法,返回一个年级对象存储在grade这个属性中。

@Result(column="gradeId",property="grade",one=@One(select="com.icss.dao.GradeDao.getGradeById"))

})

public List<Subject> find();

数据持久化层代码:

package com.icss.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.One;

import org.apache.ibatis.annotations.Result;

import org.apache.ibatis.annotations.Results;

import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.Update;

import com.icss.entity.Subject;

public interface SubjectDao {

@Select("select * from subject")

@Results({

@Result(id=true,column="subjectNo",property="id"),

@Result(column="subjectName",property="subjectName"),

@Result(column="classHour",property="hour"),

//多对一的情况操作,根据年级编号调用年级的数据持久化接口的方法,返回一个年级对象存储在grade这个属性中。 @Result(column="gradeId",property="grade",one=@One(select="com.icss.dao.GradeDao.getGradeById"))

})

public List<Subject> find();

//根据年级查询课程

@Select("select * from subject where gradeId=#{id}")

@Results({

@Result(id=true,column="subjectNo",property="id"),

@Result(column="subjectName",property="subjectName"),

@Result(column="classHour",property="hour"),

//多对一的情况操作,根据年级编号调用年级的数据持久化接口的方法,返回一个年级对象存储在grade这个属性中。 @Result(column="gradeId",property="grade",one=@One(select="com.icss.dao.GradeDao.getGradeById"))

})

public List<Subject> findByGID(int id);

//新增

@Insert("insert into subject(null,#{subjectName},#{hour},#{grade.gradeId})")

public int add(Subject subject);

//删除

@Delete("delete from subject where subjectNo=#{id}")

public int delete(int id);

//修改

@Update("update subejct set subjectName=#{subjectName},classHour=#{hour},"

+ " gradeId=#{grade.gradeId} where subjectNo=#{id}")

public int update(Subject subject);

}

在团队开发中,建议统一用其中的一种处理方式,用注解就全部用注解,用映射文件就统一用映射文件。

二、模糊查询

错误的处理方法:

<!-- 字段与属性名不一致,不能用resultType属性 -->

<select id="getStudents" resultMap="studentMap" parameterType="map">

select * from student

<if test="name != null">

where name like '%'+#{name}+'%'

</if>

</select>

正确的方式是在参数传入之前就加了这些通配符。

sqlSession = SqlSessionFactoryUtil.getsqlSession();

StudentDao dao= sqlSession.getMapper(StudentDao.class);

//参数传入到映射文件前就加了模糊查询的通配符

Map<String, String> map = new HashMap<>();

map.put("name", "%张%");

//调用方法,执行查询

list=dao.getStudents(map);

映入文件如下:

<!-- 字段与属性名不一致,不能用resultType属性 -->

<select id="getStudents" resultMap="studentMap" parameterType="map">

select * from student

<if test="name != null">

where name like #{name}

</if>

</select>

三、动态SQL

MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力,也是MyBatis最有特色的地方,就是它的动态SQL,解决了我们多条件时SQL指令的拼接问题。查询学生信息:条件可能(姓名,年龄,年级,性别,地址)

通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语 言来改进这种情形,这种语言可以被用在任意映射的 SQL 语句中。 动态SQL非常简单,与JSTL的使用非常类似。

if:当条件成立时,则把内部的SQL拼接到外部的SQL指令中。

<!-- 字段与属性名不一致,不能用resultType属性 -->

<select id="getStudents" resultMap="studentMap" parameterType="map">

select * from student

<if test="name != null">

where name like #{name}

</if>

</select>

choose, when, otherwise:

这是一个多重if-else结构,如果只有一个when和otherwise,则可以认为是if-else结构

<!-- 字段与属性名不一致,不能用resultType属性 -->

<select id="getStudents" resultMap="studentMap" parameterType="map">

select * from student

<choose>

<when test="name != null">

where name like #{name}

</when>

<otherwise>

where age > #{age}

</otherwise>

</choose>

</select>

where:

当条件比较多的时候,要进行条件拼接,where的作用是进行条件拼接,自动加上where 关键字,而且会把多余的and 或 or 这些连接关键字去掉。

<!-- 字段与属性名不一致,不能用resultType属性 -->

<select id="getStudents" resultMap="studentMap" parameterType="map">

select * from student

<where>

<if test="name != null">

and name like #{name}

</if>

<if test="age != null">

and age > #{age}

</if>

</where>

</select>

拼接后的SQL: select * from student where and name like '%张%' and age > 22

标红的 and 会自动去掉.

set: 用于修改的SQL指令

在修改的SQL指令中自动加上set关键字,同时把多余的逗号(,)去掉。非常适合部分值

<update id="update" parameterType="map">

update student

<set>

<if test="name != null">

name=#{name},

</if>

<if test="age != null">

age=#{age},

</if>

<if test="sex != null">

sex=#{sex},

</if>

<if test="phone != null">

phone=#{phone}

</if>

</set>

where studentNo=1002

</update>

拼接后的SQL:

update student set name='肖月月',age=20,sex='女' where studentNo=1002

foreach

这个动态 SQL 通用的必要操作是迭代一个集合, 通常是构建在 IN 条件中.

<select id="getStudents1" resultMap="studentMap" parameterType="list">

select * from student where address in

<foreach item="s" collection="list" open="(" close=")" separator=",">

#{s}

</foreach>

</select>

业务层处理:

sqlSession = SqlSessionFactoryUtil.getsqlSession();

StudentDao dao= sqlSession.getMapper(StudentDao.class);

List<String> arr = new ArrayList<String>();

arr.add("广州市");

arr.add("深圳市");

//调用方法,执行查询

list=dao.getStudents1(arr);

MyBatis 注解配置及动态SQL的更多相关文章

  1. mybatis注解开发,动态sql

    在利用mybatis注解开始时,如果没有用到动态sql时,可以直接写 @Select("select * from order") List<XlSubOrder> g ...

  2. 08—mybatis注解配置二

    动态sql mybatis的注解也支持动态sql.mybatis提供了各种注解,如@InsertProvider.@UpdateProvider.@DeleteProvider和@SelectProv ...

  3. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  4. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  5. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  6. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  7. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  8. MyBatis注解配置动态SQL

    MySQL创建表 DROP TABLE IF EXISTS `tb_employee`; CREATE TABLE `tb_employee` ( `id` int(11) NOT NULL AUTO ...

  9. 学习MyBatis必知必会(7)~注解开发、动态SQL

    一.MyBatis的注解开发 开发中推荐是使用xml文件配置 1.配置映射关系[使用注解的方式]: <!-- 全局的配置文件 --> <configuration> <! ...

随机推荐

  1. apache Rewrite配置(转)

    1.Rewrite规则简介: Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要 ...

  2. etcd部署说明

    etcd是一个K/V分布式存储,每个节点都保存完成的一份数据.有点类似redis.但是etcd不是数据库. 1.先说废话.之所以会用etcd,并不是实际项目需要,而是前面自己写的上传的DBCacheS ...

  3. Python 学习笔记(十一)Python语句(二)

    For 循环语句 基础知识 for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for 循环规则: do sth >>> for i in "python ...

  4. Maven项目改为spring boot项目的方法

    目录树 新建Maven项目及步骤 修改方法 启动测试 新建Maven项目及步骤 我这里是从创建开始讲,使用的工具是Idea2017版本.如果是已经创建了Maven,想改为spring boot项目的请 ...

  5. go加密算法:CBC对称加密(一)--DES

    package main import ( "bytes" //"crypto/aes" "crypto/cipher" "cry ...

  6. pm2命令管理启动的nodejs项目进程

    安装 npm install -g pm2 用法 $ npm install pm2 -g # 命令行安装 pm2 $ pm2 start app.js -i 4 #后台运行pm2,启动4个app.j ...

  7. 【淘宝客】批量提取QQ号

    1:打开QQ群官方,网址:http://qun.qq.com/ 2.点击导航栏:群管理 3.点击成员管理,选择需要提取QQ号的群 4.全选复制群成员 5.打开网址:http://tool.oschin ...

  8. 关于NPOI导出excel文件(xls和xlsx两种格式)提示格式不符的问题

    这两天在做导出excel文件的时候遇到这个问题 本来我导出的格式是xlsx格式的,但是下载得到的文件格式变成了xls, 一开始以为是返回的contenttype设置错了 return File(ms, ...

  9. python之变量的命名规则

    变量的命名规则: 1.变量名由数字.字母和下划线组成名 2.变量名不能以数字开头 3.禁止使用python中的关键字 4.不能使用中文和拼音 5.变量名要区分大小写 6.变量名要有意义 7.推荐写法: ...

  10. Go语言的接口与反射

    美女图片没啥用,就是为了好看 本文还在完善中... go总体而言是一门比较好入门的语言,许多特性都很精简易懂,但是接口与反射除外.他们真的让人头疼,不知道是自身资质问题还是怎么着,总是觉得很多书上写的 ...