mybatis学习系列二

1 参数处理(封装map过程)(23)

1.1)F5进入断点:Employee employee1=mapper.selectEmployeeByMap(map);

1.2)进入MapperProxy的invoke方法,跟踪到mapperMethod.execute(sqlSession, args);

1.3)进入MapperMethod的execute方法,可以看到增删改查均有对应case:

Object result;

switch (command.getType()) {

case INSERT: {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.insert(command.getName(), param));

break;

}

1.4)进入method.convertArgsToSqlCommandParam(args);方法--》 paramNameResolver.getNamedParams(args);

1.5)ParamNameResolver类的关键方法:

/**

* <p>

* A single non-special parameter is returned without a name.<br />

* Multiple parameters are named using the naming rule.<br />

* In addition to the default names, this method also adds the generic names (param1, param2,

* ...).

* </p>

*/

public Object getNamedParams(Object[] args) {

final int paramCount = names.size();

//为null或0个参数直接返回null

if (args == null || paramCount == 0) {

return null;

//单个参数且没有param注解:args[0];直接返回

} else if (!hasParamAnnotation && paramCount == 1) {

return args[names.firstKey()];

//多个参数或有param注解

} else {

final Map<String, Object> param = new ParamMap<Object>();

int i = 0;

//遍历names集合:{0=id,1=lastName,2=2}

for (Map.Entry<Integer, String> entry : names.entrySet()) {

//封装效果:{id:args[0],lastName:args[1],2=args[2]}

param.put(entry.getValue(), args[entry.getKey()]);

// add generic param names (param1, param2, ...)

final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);

// ensure not to overwrite parameter named with @Param

//除了按注解方式保存外,额外将每个参数也保存到map中,使用新的key:param1,...,paramN

if (!names.containsValue(genericParamName)) {

param.put(genericParamName, args[entry.getKey()]);

}

i++;

}

return param;

}

}

(@Param(“id”) Integer id,@Param(“lastName”) String lastName)

ParamNameResolver解析参数封装map的:

1、names:{0=id,1=lastName}构造器时即确定好

确定流程:

1)获取每个param注解参数的@Param值:id,lastName;赋值给name

2)每次解析一个参数给map中保存信息:(key:参数索引,value:name的值)

Name的值:

标注了param注解则为注解的值

没有标注:

1)全局配置:isUseActualParamName :name=参数名

2)Name=map.size()相当于当前元素的索引{0=id,1=lastName,2=2}

2参数处理#与$区别(24,25)

获取参数方法

作用

备注

#

#{};

1)一般用法:以预编译形式,将参数设置到sql中,类似于preparedstatement;防止sql注入

2)更丰富用法:

规定参数规则:javaType,jdbcType,mode(存储过程),numericScale,resultmap,typehandler,jdbctypename,expression(未来准备支持的)

其中jdbcType在某种特定条件下设置(数据为null时,有些数据库可能不能识别mybatis对null的默认处理,如oracle(会报错)。因为mybatis对所有的null都映射为原生jdbc的other类型,由于全局配置中,jdbcTypeForNull=other而oracle不支持

占位符?

大部分情况下使用#

可在全局配置文件中配置

jdbcTypeForNull设置为null

<setting name=jdbcTypeForNull value=NULL/>

$

${}:取出的值直接拼接sql中,会有安全问题

原生jdbc不支持占位符的地方:

1)查询表名(分表操作,如按年份分表拆分)

Select * from ${year}_salary where ***;

2)排序(order)

Select * from salary order by ${name}  ${order};

示例:

select * from myemployeee where id = ${id}

and last_name=#{lastName}

对应执行情况:

select * from myemployeee where id = 2

and last_name=?

Parameters:test(string)

3select 返回list(26)

<select id="selectEmployeeByName" resultType="com.mybatis.bean.Employee">

select * from myemployeee where last_name=#{lastName}

</select>

//返回list

List<Employee> selectEmployeeByName(String name);

3select 返回封装为map(27)

单条记录

<!-- 返回封装为map -->

<select id="selectEmployeeMap" resultType="map">

select * from myemployeee where last_name=#{lastName}

</select>

//返回map(单个)

Map<String,Object> selectEmployeeMap(String name);

Map<String,Object> returnMap= mapper.selectEmployeeMap("test");

System.out.println(returnMap);

多条记录:

<!-- 返回封装为map,多条 -->

<select id="selectEmployeeMaps" resultType="com.mybatis.bean.Employee">

select * from myemployeee where last_name like #{lastName}

</select>

//返回map(多条) Map<Integer,Employee> 键:主键;值:Employee

@MapKey("id")  //通过@MapKey注解封装map时使用指定属性作为key

Map<Integer,Employee> selectEmployeeMaps(String name);

Map<Integer,Employee> returnMap2= mapper.selectEmployeeMaps("%test%");

System.out.println(returnMap2);

也可以将id改为lastName使用名字作为key。

返回结果:

{test3=com.mybatis.bean.Employee@c81cdd1, test=com.mybatis.bean.Employee@1fc2b765}

4select resultMap自定义结果映射规则(28)

<!-- 自定义结果映射规则

type: 自定义规则的java类型

id:该映射的唯一标识,方便映射

-->

<resultMap type="com.mybatis.bean.Employee" id="resultMap">

<!-- column:数据库列;property:javabean属性 -->

<id column="id" property="id" jdbcType="INTEGER"/>

<result column="last_name" property="lastName" jdbcType="VARCHAR"/>

<!-- 其他不指定的列会自动封装 -->

</resultMap>

//返回list

List<Employee> selectEmployeeByName(String name);

List<Employee> selectEmployeeByName = mapper.selectEmployeeByName("test");

System.out.println(selectEmployeeByName);

5select resultMap关联查询(级联属性)(30)

联合查询:级联属性封装结果集

1) bean对象

public class Employee {

private Integer id;

private String lastName;

private String email;

private String gender;

private MyDept myDept;

public class MyDept {

private Integer deptno;

private String dname;

private String dbSource;

2) mybatis xml文件配置

<resultMap type="com.mybatis.bean.Employee" id="DeptResultMap">

<!-- column:数据库列;property:javabean属性 -->

<id column="id" property="id" jdbcType="INTEGER"/>

<result column="last_name" property="lastName" jdbcType="VARCHAR"/>

<!-- 其他不指定的列会自动封装 -->

<result column="deptno" property="myDept.deptno"/>

<result column="dname" property="myDept.dname"/>

<result column="db_source" property="myDept.dbSource"/>

</resultMap>

<select id="selectEmployeeAndDept" resultMap="DeptResultMap">

select e.id,e.last_name,e.email,e.gender

,d.deptno,d.dname,d.db_source

from myemployeee e,mydept d where e.dept_id=d.deptno and e.id = #{id}

</select>

3)测试

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

map.put("id", 1);

map.put("lastName","test");

//   List<Employee> selectEmployeeByName = mapper.selectEmployeeByName("test");

Employee selectEmployeeAndDept = mapper.selectEmployeeAndDept(1);

System.out.println(selectEmployeeAndDept.getMyDept().getDbSource()+"=="+selectEmployeeAndDept.getMyDept().getDname());

6select resultMap关联查询(association)(31)

1) Xml

<resultMap type="com.mybatis.bean.Employee" id="DeptResultMap2">

<!-- column:数据库列;property:javabean属性 -->

<id column="id" property="id" jdbcType="INTEGER"/>

<result column="last_name" property="lastName" jdbcType="VARCHAR"/>

<!-- 其他不指定的列会自动封装 -->

<!-- assocation可以指定联合的javabean对象

property=dept,指定哪个属性是联合的对象

javatype:指定整个属性对象的类型,不能省略 -->

<association property="myDept" javaType="com.mybatis.bean.MyDept">

<id column="deptno" property="deptno"/>

<result column="dname" property="dname"/>

<result column="db_source" property="dbSource"/>

</association>

</resultMap>

2)测试

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

map.put("id", 1);

map.put("lastName","test");

//   List<Employee> selectEmployeeByName = mapper.selectEmployeeByName("test");

Employee selectEmployeeAndDept = mapper.selectEmployeeAndDept(1);

System.out.println(selectEmployeeAndDept.getMyDept().getDbSource()+"=="+selectEmployeeAndDept.getMyDept().getDname());

7select resultMap关联查询(association分步查询)(32)

1)创建mapper文件

public interface MyDeptMapper {

//查询

MyDept getMyDeptById(Integer id);

}

2)创建xml文件

<mapper namespace="com.mybatis.dao.MyDeptMapper">

<select id="getMyDeptById" resultType="com.mybatis.bean.MyDept">

select * from mydept d where d.deptno=#{deptno}

</select>

</mapper>

3)employee.xml文件中

<!-- 分步查询

1、根据员工id查询员工信息

2、根据员工信息中id查询部门信息

3、部门设置到员工中-->

<resultMap type="com.mybatis.bean.Employee" id="DeptResultByStep">

<!-- column:数据库列;property:javabean属性 -->

<id column="id" property="id" jdbcType="INTEGER"/>

<result column="last_name" property="lastName" jdbcType="VARCHAR"/>

<!-- 其他不指定的列会自动封装 -->

<!-- assocation定义关联对象的封装规则

property:指定关联的对象

select:表明当前属性是调用select指定的方法查出的结果

column:指定哪一列的值传给这个方法

流程:使用select指定的方法(传入column指定的这列参数的值)

查出对象,并封装给property指定的属性

-->

<association property="myDept" select="com.mybatis.dao.MyDeptMapper.getMyDeptById"

column="dept_id" >

</association>

</resultMap>

<select id="selectEmployeeAndDept" resultMap="DeptResultByStep">

select * from myemployeee where id = #{id}

</select>

3)测试

Employee selectEmployeeAndDept = mapper.selectEmployeeAndDept(1);

System.out.println(selectEmployeeAndDept.getMyDept().getDbSource()+"=="+selectEmployeeAndDept.getMyDept().getDname());

8select resultMap关联查询(association分步查询,延迟加载)(33)

默认为全加载,可在全局配置文件中配置

<settings>

<!-- 设置懒加载 -->

<setting name="lazyLoadingEnabled" value="true"/>

<!-- name:设置项名,value:设置项值 -->

<setting name="mapUnderscoreToCamelCase" value="true"/><!--映射数据库下划线-java bean驼峰命名规则 -->

</settings>

9select resultMap关联查询(collection关联集合封装规则)(34)

上面association是针对一对一情况(一个员工对应一个部门),查询一对多(一个部门下所有员工信息)情况使用collection。

1)javabean配置

public class MyDept {

private Integer deptno;

private String dname;

private String dbSource;

private List<Employee> employees;

2)mapper接口

public interface MyDeptMapper {

//查询

MyDept getMyDeptById(Integer id);

//查询(带员工)

MyDept getMyDeptAndEmployeeById(Integer id);

}

3)xml配置

<resultMap type="com.mybatis.bean.MyDept" id="DeptAndEmployeeResultMap">

<id column="deptno" property="deptno"/>

<result column="db_source" property="dbSource"/>

<result column="dname" property="dname"/>

<!--

collection:定义关联集合类型的属性的封装规则

ofType:指定集合里面元素的类型

collection中的属性对应employee属性

-->

<collection property="employees" ofType="com.mybatis.bean.Employee">

<!-- 定义这个集合中元素的封装规则 -->

<id column="id" property="id"/>

<result column="last_name" property="lastName"/>

<result column="dept_id" property="deptId"/>

<result column="email" property="email"/>

<result column="gender" property="gender"/>

<result column="dept_id" property="deptId"/>

</collection>

</resultMap>

<select id="getMyDeptAndEmployeeById" resultMap="DeptAndEmployeeResultMap">

select d.*,e.* from mydept d,myemployeee e where d.deptno=e.dept_id and d.deptno=#{deptno}

</select>

注意!需要的属性必须列出,否则为null。

如:

<collection property="employees" ofType="com.mybatis.bean.Employee">

<!-- 定义这个集合中元素的封装规则 -->

<id column="id" property="id"/>

<result column="last_name" property="lastName"/>

<result column="dept_id" property="deptId"/>

<!-- <result column="email" property="email"/>

<result column="gender" property="gender"/>

<result column="dept_id" property="deptId"/> -->

</collection>

对应输出结果:

test null null 1

test3 null null 4

属性列出后输出结果:

test test@com.cm 男 1

test3 test3@com.cm 男 4

10select resultMap关联查询(collection分步查询)(35)

1) Employmapper.xml

<select id="selectEmployeeByDeptno" resultType="com.mybatis.bean.Employee">

select * from myemployeee where dept_id = #{deptno}

</select>

2) Mydeptmapper.xml

<!-- 分步查询 -->

<resultMap type="com.mybatis.bean.MyDept" id="DeptAndEmployeeByStepResultMap">

<id column="deptno" property="deptno"/>

<result column="db_source" property="dbSource"/>

<result column="dname" property="dname"/>

<!--

collection:定义关联集合类型的属性的封装规则

ofType:指定集合里面元素的类型

collection中的属性对应employee属性

-->

<collection property="employees" select="com.mybatis.dao.EmployeeMapper.selectEmployeeByDeptno" column="deptno">

</collection>

</resultMap>

<select id="getMyDeptAndEmployeeStepById" resultMap="DeptAndEmployeeByStepResultMap">

select * from mydept d where d.deptno=#{deptno}

</select>

3) 接口MyDeptMapper

public interface MyDeptMapper {

//查询

MyDept getMyDeptById(Integer id);

//查询(带员工)

MyDept getMyDeptAndEmployeeById(Integer id);

//查询(带员工),分步查询

MyDept getMyDeptAndEmployeeStepById(Integer id);

}

4) 测试

//collection测试

MyDeptMapper mapper=session.getMapper(MyDeptMapper.class);

MyDept myDeptAndEmployeeById = mapper.getMyDeptAndEmployeeStepById(1);

if (null!=myDeptAndEmployeeById && !myDeptAndEmployeeById.getEmployees().isEmpty()) {

for(Employee e:myDeptAndEmployeeById.getEmployees()) {

System.out.println(e.getLastName()+" "+e.getEmail()+" "+e.getGender()+" "+e.getId());

}

}

11select resultMap关联查询(collection分步查询传递多列值)(36)

多列传值,将多列的值封装map传递column=”{key1=column1,key2=column2}”

fetchType=”lazy”表示使用延迟加载

-lazy:延迟;-eager:立即

<collection property="employees"  select="com.mybatis.dao.EmployeeMapper.selectEmployeeByDeptno"

column="deptno" fetchType="lazy">

</collection>

11select resultMap关联查询discriminator鉴别器(37)

鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列值改变封装行为。

示例:

Employee:查出女生就把部门信息查询出来,否则不查询。

1)修改employeemapper

<resultMap type="com.mybatis.bean.Employee" id="DeptResultByStep">

<!-- column:数据库列;property:javabean属性 -->

<id column="id" property="id"/>

<result column="last_name" property="lastName"/>

<result column="gender" property="gender"/>

<!-- 其他不指定的列会自动封装 -->

<!-- assocation定义关联对象的封装规则

property:指定关联的对象

select:表明当前属性是调用select指定的方法查出的结果

column:指定哪一列的值传给这个方法

流程:使用select指定的方法(传入column指定的这列参数的值)

查出对象,并封装给property指定的属性

-->

<!-- column:指定判定的列名

javatype:列值对应的java类型 -->

<discriminator javaType="string" column="gender">

<!-- 女生resultType:指定封装的结果类型 -->

<case value="女" resultType="com.mybatis.bean.Employee">

<association property="myDept" select="com.mybatis.dao.MyDeptMapper.getMyDeptById"

column="dept_id" >

</association>

</case>

<case value="男" resultType="com.mybatis.bean.Employee">

<id column="id" property="id"/>

<result column="gender" property="gender"/>

<result column="last_name" property="lastName" jdbcType="VARCHAR"/>

</case>

</discriminator>

</resultMap>

2)测试类

//discriminator鉴别器

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

Employee employee1=mapper.selectEmployeeAndDept(4);

System.out.println(employee1.getLastName()+" "+employee1.getEmail());

12动态sql(38-39)

OGNL(object graph navigation language)对象图导航语言(apache)。类似el,sqel等表达式

1)Mapper

//返回map

List<Map<String,Object>> selectEmployeeByMap(@Param("employee") Map<String,Object> map);

2) Xml

<!-- 返回封装为map

动态sql:

特殊子父需要使用转义字符

-->

<select id="selectEmployeeByMap" resultType="map">

select * from myemployeee where

1=1

<if test=" null  != employee.lastName and employee.lastName != '' ">

and last_name like CONCAT('%',#{employee.lastName,jdbcType=VARCHAR},'%')

</if>

<if test="null!=employee.id and employee.id != '' ">

and id=#{employee.id,jdbcType=INTEGER}

</if>

</select>

3) 测试

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

//   map.put("id", 1);

map.put("lastName","test");

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

List<Map<String,Object>> selectEmployeeByMap = mapper.selectEmployeeByMap(map);

System.out.println(selectEmployeeByMap.toString());

报错:

Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

原因:sql语句like查询为

and last_name like'%#{employee.lastName,jdbcType=VARCHAR}%'

导致报错,修改为concat即可

13动态sql-where查询条件(40)

1)xml修改

<where>

<if test=" null  != employee.lastName and employee.lastName != '' ">

and last_name like CONCAT('%',#{employee.lastName,jdbcType=VARCHAR},'%')

</if>

<if test="null!=employee.id and employee.id != '' ">

and id=#{employee.id,jdbcType=INTEGER}

</if>

</where>

2)测试

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

//   map.put("id", 1);

map.put("lastName","test");

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

List<Map<String,Object>> selectEmployeeByMap = mapper.selectEmployeeByMap(map);

System.out.println(selectEmployeeByMap.toString());

14动态sql-trim自定义字符串截取(41)

后面多出的and或or,where标签不能解决

1)xml文件

<!-- trim自定义字符串截取

prefix:前缀,给拼串后的整个字符串加一个前缀

prefixOverrides:前缀覆盖,去掉整个字符串前面多余字符

suffix:后缀,给拼串后的整个字符串加一个后缀

suffixOverrides:后缀覆盖,去掉整个字符串后面多余的字符

-->

<trim prefix="where" prefixOverrides="and" suffixOverrides="and">

<if test=" null  != employee.lastName and employee.lastName != '' ">

and last_name like CONCAT('%',#{employee.lastName,jdbcType=VARCHAR},'%')

</if>

<if test="null!=employee.id and employee.id != '' ">

and id=#{employee.id,jdbcType=INTEGER}

</if>

</trim>

</select>

2)测试

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

//   map.put("id", 1);

map.put("lastName","test");

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

List<Map<String,Object>> selectEmployeeByMap = mapper.selectEmployeeByMap(map);

System.out.println(selectEmployeeByMap.toString());

15动态sql-choose分支选择(42)

If:判断

Choose:(when,otherwise):分支选择,switch-case

如果带了id就用id查,如果带了lastname就用lastname查,只选择一个

Trim:字符串截取

Where

Set:封装修改

Forearch:

1)xml修改

<!-- choose 分支选择

只选择一个

-->

<where>

<choose>

<when test=" null  != employee.lastName and employee.lastName != '' ">

last_name like CONCAT('%',#{employee.lastName,jdbcType=VARCHAR},'%')

</when>

<when test=" null!=employee.id and employee.id != '' ">

id=#{employee.id,jdbcType=INTEGER}

</when>

<otherwise>

gender='男'

</otherwise>

</choose>

</where>

</select>

16动态sql-set与if结合的动态更新(43)

1)set方式

<update id="updateEmp" parameterType="com.mybatis.bean.Employee">

update myemployeee mm

<set>

<if test="lastName !=null and lastName!='' ">

mm.last_name=#{lastName,jdbcType=VARCHAR},

</if>

<if test="email !=null and email !='' ">

mm.email=#{email,jdbcType=VARCHAR},

</if>

<if test="gender !=null and gender !='' ">

mm.gender=#{gender,jdbcType=VARCHAR}

</if>

</set>

where mm.id=#{id,jdbcType=INTEGER}

</update>

2)trim方式

<trim prefix="set" suffixOverrides=",">

<if test="lastName !=null and lastName!='' ">

mm.last_name=#{lastName,jdbcType=VARCHAR},

</if>

<if test="email !=null and email !='' ">

mm.email=#{email,jdbcType=VARCHAR},

</if>

<if test="gender !=null and gender !='' ">

mm.gender=#{gender,jdbcType=VARCHAR},

</if>

</trim>

where mm.id=#{id,jdbcType=INTEGER}

</update>

17动态sql-forearch遍历集合(44)

Collection:指定要遍历的集合

List类型的参数会特殊处理在map中,map的key就叫list

Item:将当前遍历出来的元素赋值给指定变量。

#{变量名}即可取出

1)Xml

<!-- 根据list查询 -->

<select id="selectEmployeeByIds" resultMap="resultMap" >

select * from myemployeee

<!-- collection:指定要遍历的集合

list类型的参数会特殊处理封装在map中,map的key即叫list

item:将当前遍历出的元素赋值给指定的变量

#{变量名}即可取出变量的值(即当前遍历出的元素)

index:索引,遍历list时为索引;遍历map时表示map的key(item是值)

-->

where id in

<foreach collection="list" item="item" open="("

separator="," close=")">

#{item,jdbcType=INTEGER}

</foreach>

</select>

2)Javabean

public Employee(Integer id, String lastName, String email, String gender,Integer deptId) {

super();

this.id = id;

this.lastName = lastName;

this.email = email;

this.gender = gender;

this.deptId=deptId;

}

3)测试

//list查询

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

List<Integer> ids=new ArrayList<Integer>();

ids=Arrays.asList(1,2,3);

List<Employee> selectEmployeeByIds = mapper.selectEmployeeByIds(ids);

for(Employee e:selectEmployeeByIds) {

System.out.println(e.toString());

}

或者

<foreach collection="list" item="item" open="where id in ("

separator="," close=")">

#{item,jdbcType=INTEGER}

</foreach>

18动态sql-forearch批量插入的2种方式(45)

1)Xml

<!-- 方式一:直接list对象

addEmps(List<Employee> emps)

Insert into (),(),()

-->

<!-- insert into myemployeee(last_name,email,gender,dept_id)

values

<foreach collection="list" item="emp" separator=",">

(#{emp.lastName,jdbcType=VARCHAR},#{emp.email,jdbcType=VARCHAR},

#{emp.gender,jdbcType=VARCHAR},#{emp.deptId,jdbcType=INTEGER})

</foreach> -->

2)Mapper

//批量插入

Integer addEmps(List<Employee> emps);

3)测试

//批量插入

EmployeeMapper mapper=session.getMapper(EmployeeMapper.class);

List<Employee> emps=new ArrayList<Employee>();

emps.add(new Employee(null,"addemps1","addemps1@qq.com","男",1));

emps.add(new Employee(null,"addemps2","addemps2@qq.com","女",2));

System.out.println(mapper.addEmps(emps));

方式二:

<!-- 方式二:封装为map

addEmps(@Param("emps") List<Employee> emps);

-->

insert into myemployeee(last_name,email,gender,dept_id)

values

<foreach collection="emps" item="emp" separator=",">

(#{emp.lastName,jdbcType=VARCHAR},#{emp.email,jdbcType=VARCHAR},

#{emp.gender,jdbcType=VARCHAR},#{emp.deptId,jdbcType=INTEGER})

</foreach>

方式三:一次执行多个语句

<!-- 方式三:封装为map,且使用;分割为多个插入语句,

一次执行多个语句;

需要配置连接属性:&allowMultiQueries=true

jdbc.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true

addEmps(@Param("emps") List<Employee> emps);

-->

<foreach collection="emps" item="emp" separator=";">

insert into myemployeee(last_name,email,gender,dept_id)

values

(#{emp.lastName,jdbcType=VARCHAR},#{emp.email,jdbcType=VARCHAR},

#{emp.gender,jdbcType=VARCHAR},#{emp.deptId,jdbcType=INTEGER})

</foreach>

以上为mysql情况下;下面为oracle批量插入

oracle批量插入

注意:oracle不支持values(),(),()

Oracle使用

Begin

Insert into myemployeee(last_name,email,gender,dept_id)

Values (#{emp.lastName,jdbcType=VARCHAR},#{emp.email,jdbcType=VARCHAR},

#{emp.gender,jdbcType=VARCHAR},#{emp.deptId,jdbcType=INTEGER});

Insert into myemployeee(last_name,email,gender,dept_id)

Values (#{emp.lastName,jdbcType=VARCHAR},#{emp.email,jdbcType=VARCHAR},

#{emp.gender,jdbcType=VARCHAR},#{emp.deptId,jdbcType=INTEGER});

End;

包括在begin和end中间

上一篇:Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)


下一篇:[已解决]报错:Failed to restart network.service: Unit network.service not found