1、if
<!-- if:判断 --> <!-- 查询员工,要求,携带了哪个字段查询条件就带上这个字段的值 使用where标签后,自己会自动添加and 或者or条件 (第一个的and或者or) --> <!-- public List<Employee> getEmpsByConditionIf(Employee employee); --> <select id="getEmpsByConditionIf" resultType="com.bd.www.Employee"> select * from emp <where> <!-- test:判断表达式 c:if test 从参数中取值进行判断遇见特殊符号应该去写转义字符: &&:&& 或者 and ‘’ 或者使用转义字符 "" --> <if test="id!=null"> and id=#{id} </if> <if test="name!=null && name!="""> and name like #{name} </if> <if test="email!=null and email.trim()!=’’;"> and email=#{email} </if> <!-- ognl会进行字符串与数字的转换判断 "0"==0 --> <if test="gender==0 or gender==1"> and gender=#{gender} </if> </where> </select>
或者
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); --> <select id="getEmpsByConditionIf" resultType="com.bd.www.Employee"> select * from emp where 1=1 <!-- test:判断表达式 c:if test 从参数中取值进行判断遇见特殊符号应该去写转义字符: &&:&& 或者 and ‘’ 或者使用转义字符 "" --> <if test="id!=null"> and id=#{id} </if> …… </select>
2、choose
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); --> <select id="getEmpsByConditionChoose" resultType="com.bd.www.Employee"> select * from emp <where> <!-- 如果带了id就用id查,如果带了lastName就用lastName查;只会进入其中一个 --> <choose> <when test="id!=null"> id=#{id} </when> <when test="name!=null"> name like #{name} </when> <otherwise> gender = 0 </otherwise> </choose> </where> </select>
3、trim
<!--public List<Employee> getEmpsByConditionTrim(Employee employee); --> <select id="getEmpsByConditionTrim" resultType="com.bd.www.Employee"> select * from tbl_employee <!-- 后面多出的and或者or where标签不能解决 prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。 prefix给拼串后的整个字符串加一个前缀 prefixOverrides="":前缀覆盖: 去掉整个字符串前面多余的字符 suffix="":后缀 suffix给拼串后的整个字符串加一个后缀 suffixOverrides=""后缀覆盖:去掉整个字符串后面多余的字符--> <trim prefix="where" suffixOverrides="and"> <if test="id!=null"> id=#{id} and </if> <if test="lastName!=null && lastName!="""> last_name like #{lastName} and </if> <if test="email!=null and email.trim()!="""> email=#{email} and </if> <!-- ognl会进行字符串与数字的转换判断 "0"==0 --> <if test="gender==0 or gender==1"> gender=#{gender} </if> </trim> </select>
4、set
<!--public void updateEmp(Employee employee); --> <update id="updateEmp"> <!-- Set标签的使用 --> update tbl_employee <set> <if test="lastName!=null"> last_name=#{lastName}, </if> <if test="email!=null"> email=#{email}, </if> </set> where id=#{id} <!-- Trim:更新拼串 update tbl_employee <trim prefix="set" suffixOverrides=","> <if test="lastName!=null"> last_name=#{lastName}, </if> <if test="email!=null"> email=#{email}, </if> <if test="gender!=null"> gender=#{gender} </if> </trim> where id=#{id} --> </update>
5、foreach
<!--collection:指定要遍历的集合: list类型的参数会特殊处理封装在map中,map的key就叫list item:将当前遍历出的元素赋值给指定的变量 separator:每个元素之间的分隔符 open:遍历出所有结果拼接一个开始的字符 close:遍历出所有结果拼接一个结束的字符 index:索引。遍历list的时候是index就是索引,item就是当前值 遍历map的时候index表示的就是map的key,item就是map的值 #{变量名}就能取出变量的值也就是当前遍历出的元素 --> <!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids); --> <select id="getEmpsByConditionForeach" resultType="com.bd.www.Employee"> select * from emp <foreach collection="list" item="item_id" separator="," open="where id in(" close=")"> #{item_id} </foreach> </select>
6、批量添加
1> mysql数据库 mysql的批量添加可以采用两种方式,
1)insert into 表名 values(值列表1),(值列表此时对应的映射文件为:
<!--public void addEmps(@Param("emps")List<Employee> emps); --> <insert id="addEmps"> insert into emp(name,email,gender,deptid) values <foreach collection="emps" item="emp" separator=","> (#{emp.name},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>
2)insert into 表名values();insert into 表名values();……这种方式需要设置
allowMultiQueries=true。(在url的后面添加上去jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true)
<insert id="addEmps"> <foreach collection="emps" item="emp" separator=";"> insert into emp(name,email,gender,deptid) values(#{emp.name},#{emp.email},#{emp.gender},#{emp.dept.id}) </foreach> </insert>
2> Oracle数据库
begin insert into employees(employee_id,last_name,email) values(employees_seq.nextval,'test_001','test_001@atguigu.com'); insert into employees(employee_id,last_name,email) values(employees_seq.nextval,'test_002','test_002@atguigu.com'); end; <insert id="addEmps" databaseId="oracle"> <foreach collection="emps" item="emp" open="begin" close="end;"> insert into employees(employee_id,last_name,email) values(employees_seq.nextval,#{emp.lastName},#{emp.email}); </foreach> </insert>
2、利用中间表: insert into employees(employee_id,last_name,email) select employees_seq.nextval,lastName,email from( select 'test_a_01' lastName,'test_a_e01' email from dual union select 'test_a_02' lastName,'test_a_e02' email from dual union select 'test_a_03' lastName,'test_a_e03' email from dual ) <insert id="addEmps" databaseId="oracle"> insert into employees(employee_id,last_name,email) <foreach collection="emps" item="emp" open="select employees_seq.nextval,lastName,email from (" close=")" separator="union" > select #{emp.name} name,#{emp.email} email from dual </foreach> </insert>
3>mybatis两个内置参数
不只是方法传递过来的参数可以被用来判断,取值。。。mybatis默认还有两个内置参数:
_parameter:代表整个参数
单个参数:_parameter就是这个参数
多个参数:参数会被封装为一个map;_parameter就是代表这个map
_databaseId:如果配置了databaseIdProvider标签。
_databaseId就是代表当前数据库的别名oracle
<select id="getEmpsTestInnerPrara" resultType="com.bd.www.Emp"> <if test="_databaseId=='mysql'"> select * from emp <if test="_parameter!=null"> where name = #{name} </if> </if> <if test="_databaseId=='oracle'"> select * from employee <if test="_parameter!=null"> where empname =#{name} </if> </if> </select>
7>bind
比如,传入名字,需要进行模糊查询,可以采用如下做法:
<select id="getEmpsTestInnerPrara" resultType="com.bd.www.Emp"> <if test="_databaseId=='mysql'"> select * from emp <if test="_parameter!=null"> where name like '%${name}%' </if> </if> <if test="_databaseId=='oracle'"> select * from employee <if test="_parameter!=null"> where empname =#{name} </if> </if> </select>
以上方式虽然可以,但是不安全。采用bind标签,可以将其绑定到另一个变量值中
<select id="getEmpsTestInnerParameter" resultType="com.atguigu.mybatis.bean.Employee"> <!-- bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 --> <bind name="_name" value="'%'+name+'%'"/> <if test="_databaseId=='mysql'"> select * from tbl_employee <if test="_parameter!=null"> where last_name like #{_name} </if> …… </select>
注意:参数为空时,会报错。需要添加非空判断
8.SQL
可以使用sql抽象可重用的sql片段,方便后面引用。使用include来引用已经抽取的sql;还可以使用include定义一些property,sql标签内部可以使用自定义的属性,正确的使用方式是${propname}
<sql id="insertColumn"> <if test="_databaseId=='oracle'"> empid,last_name,email,${testColomn } </if> <if test="_databaseId=='mysql'"> last_name,email,gender,deptid </if> </sql> <insert id="addEmps" databaseId="oracle"> insert into employees( <!-- 引用外部定义的sql --> <include refid="insertColumn"> <property name="testColomn" value="abc"/> </include> ) <foreach collection="emps" item="emp" separator="union" open="select employees_seq.nextval,lastName,email from(" close=")"> select #{emp.lastName} lastName,#{emp.email} email from dual </foreach> </insert>