<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper">
<!--
NameSpace:名称空间,指定为接口的完整类名
ID:唯一标识符
resultType:返回值类型
#{id}:从传递过来的参数中取出ID值
这个是接口里面的方法,获取数据对象:public Employee getEmpById(Integer id);
-->
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee" databaseId="mysql">
select id, last_name LastName, email from tbl_employee where id = #{id}
</select>
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee" databaseId="oracle">
select EMPLOYEE_ID id, LAST_NAME lastName, GENDER gender, EMAIL email
from EMPLOYEE where EMPLOYEE_ID = #{id}
</select>
<select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee" databaseId="mysql">
select id, last_name LastName, email, gender from tbl_employee where id = #{id}
</select>
<!-- public void addEmp(Employee employee); -->
<!--获取自增主键的值:
MYSQL支持自增主键,自增主键值的获取,myBatis也是利用Statement.getGeneratedKeys()来获取
在insert标签中添加useGeneratedKeys="true";使用自增主键获取主键值策略
keyProperty:用来指定获取到的主键值赋值到这个值中,这个值可以封装到JavaBean的Employee.id中
-->
<!-- useGeneratedKeys="true" keyProperty="id" -->
<insert id="addEmp" databaseId="mysql">
insert into tbl_employee(id, last_name, gender, email) values(#{id}, #{lastName}, #{gender}, #{email})
</insert>
<!-- Oracle不支持自增,但是Oracle可以利用序列来模拟自增
每次插入的数据的主键值是从序列中拿到的值;如何获取到这个值呢?
-->
<!-- keyProperty:查出主键并且封装给JavaBean的属性ID
order = "BEFORE"指定当前SQL在插入操作之前运行
= "AFTER" 指定在当前的SQL之后
resultType:查出来的主键要封装的类型是什么,需要在这里指定
BEFORE运行顺序: 先运行selectKey查询id的SQL:查出id封装给javaBean的id属性
再运行插入的SQL,就可以取出id属性对应的值
AFTER 运行顺序: 先运行插入的SQL(从序列中取出新值作为id
再运行selectKey查出id的SQL-->
<insert id="addEmp" databaseId="oracle" >
<!-- <selectKey keyProperty="id" order = "BEFORE">
select EMPLOYEE_SEQ.nextVal from dual
</selectKey> -->
<!-- 插入时的主键是从序列中拿到的-->
<!-- BEFORE --><!-- ,jdbcType=NULL -->
insert into EMPLOYEE(EMPLOYEE_ID, LAST_NAME,GENDER,EMAIL) values(#{id}, #{lastName}, #{gender}, #{email})
</insert>
<!-- 还可以这么来写 -->
<!-- AFTER -->
<!--
<insert id="addEmp" databaseId="oracle" >
<selectKey keyProperty="id" order = "AFTER">
select EMPLOYEE_SEQ.currVal from dual</selectKey>
-->
<!--insert into EMPLOYEE(EMPLOYEE_ID, LAST_NAME,GENDER,EMAIL)
values(EMPLOYEE_SEQ.nextVal, #{lastName}, #{gender}, #{email}) -->
<!-- 多参数查询 -->
<select id="getEmpByIdAndLastName" resultType="com.atguigu.mybatis.bean.Employee">
select * from EMPLOYEE where EMPLOYEE_ID = #{id} and LAST_NAME = #{lastName}
</select>
<!-- 多参数查询,使用map封装参数 -->
<select id="getEmpByMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from EMPLOYEE where EMPLOYEE_ID = #{id} and LAST_NAME = #{lastName}
</select>
<!-- public void deleteEmp(Integer id); -->
<delete id="deleteEmpById">
delete from EMPLOYEE where EMPLOYEE_ID = #{id}
</delete>
<!-- public void updateEmp(Employee employee); -->
<update id="updateEmp">
update EMPLOYEE
set LAST_NAME = #{lastName}, EMAIL = #{email}, GENDER = #{gender}
where id = #{id}
</update>
</mapper>
原文地址:https://www.cnblogs.com/zjulanjian/p/9194923.html