Mybatis 中 sql映射的xml文件(mapper)的写法:
里面包括 select insert update delete
在select中,id 是唯一标识符,parameterType 是需要的参数类型,resultType是返回的类型,
在resultType返回的类型可以使用别名,别名的配置在
mybatis-config.xml的
typeAlias中
参数:
resultMap 返回的是在<resultMap>标签中设置的map
flushCache 如果设为true,语句执行时会清空缓存(默认false)
useCache 如果设为true,将会缓存本条语句的结果,(默认是true)
fetchSize 每次批量返回的结果行数(默认不设置,由驱动自行处理)
timeout 等待数据库返回结果的时间(默认不设置,由驱动自行处理)
statementType : STATEMENT(Statement),PREPARED(PreparedStatement)或是CALLABLE(CallableStatement)的一种。
***************************************************************************************************
在insert,update,delete中的部分参数中,
有一个针对于insert的,useGeneratedKeys,这个属性会使Mybatis使用JDBC的getGeneratedKeys方法,来取出数据内部的主键。(默认值是false)
还有一个只针对于insert的keyProperty(是Java对象的属性名),这个属性设为true的话,Mybatis会通过getGeneratedKeys的返回值或者通过insert语句的selectKey子元素设置它的值,(默认为false)
还有一个order属性,可以被设置为BEFORE或是AFTER,决定了语句的执行顺序
如果数据库支持自动生成主键的字段(比如MySQL和SQL Server)
那么可以这只useGeneratedKeys="true" ,然后再把keyProperty设置到目标属性上就OK了。
对于不支持自动生成类型的数据库或不支持自动生成主键JDBC驱动来说,Mybatis有另外一种方法生成主键。需要使用selectKey,
<insert id="insertAutjor">
<selectKey keyProperty="id" resultType="int" order="BEFORE" >
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author
(id ,username,password,email)
values(#{id},#{username},#{password},#{email})
</insert>
在上面的代码中 selectKey 先会首先运行,Author的id会被设置,然后再执行插入语句,类似于自动生成主键的行为
***************************************************************************************************
sql 可以用来定义可重用的sql代码段,
比如:
<sql id="From_Table">from base_address</sql>
然后在使用的时候,
<select id="selectAddress" parameterType="java.lang.String" result="BaseResultMap">
select
<include refid="Base_Column_List"/> //使用include就可以填充上
<include refid ="From_Table"/>
where code =#{code,jdbcType=VARCHAR} //传入的参数自动设置为code,并且在这里指定了code的类型。
</select>
除了上面的在xml中配置别名,还有一种方式就是在mapper中使用resultMap ,它的id就是别名,type就是真正的bean类的所在(如果这个bean已经在typeAlias中设置了别名,这里直接写它的别名就可以了),同时
resultMap还解决了列名不匹配的问题
<resultMap id="userResultMap" type="User">
<id property="username" cloumn="user_name " jdbcType="VARCHAR"/> //设置了列名与属性名之间的对应关系,还有列的类型。
<result property="passwd" column="hashed_passwd" jdbcType="VARCHAR"/>
</resultMap>
在上面的resultMap中,<id property ="xxx" 表示的是id 为主键映射,<result property =“”
表示的是数据库字段与实体类属性的映射
jdbcType的类型有
BIT,FLOAT,CHAR,TIMESTAMP,OTHER,UNDEFINED,TINYINT,REAL,VARCHAR,BINARY,BLOB,NVARCHAR,SMALLINT,DOUBLE,LONGVARCHAR,VARBINARY,CLOB,NCHAR,INTEGER,NUMERIC,DATE,LONGVARBINARY,BOOLEAN,NCLOB,BIGINT,DECIMAL,TIME,NULL,CURSOR
动态sql语句
selectKey
解决insert数据时,不支持主键自动生成的问题
使用<selectKey keyproperty=“studentId” resultType="String" order="BEFORE">
select nextval('student');
</select>
这个就是表示在执行以前,先执行selectKey的语句,然后返回的数值设置给studentId,
然后再外层的insert中就可以使用产生的studentId进行使用了。
keyProperty selectKey语句生成结果所需要设置的属性。
resultType 生成结果类型
order 支持BEFORE 和AFTER ,决定了执行的先后顺序。
if标签
使用 if来判断参数,可以进行一些判空之类的操作
<if test="orderNumer!=null">
//里面使用的全都是test
AND order_number=#{orderNumber,jdbcType=VARCHAR}
</if>
从上面可以看到紧跟在and 后面的是order_number 是数据库中的列名
在括号中的orderNumber是bean中的东西
if + where 标签
在上面只有if的情况下,代码是这样的
<select id="getinfo" resultMap="xxx" parameterType="xxx">
select * from student
where
<if test="orderNumer!=null">
//里面使用的全都是test
order_number=#{orderNumber,jdbcType=VARCHAR}
</if>
<if test="studentId!=null">
AND studentId=#{studentId,jdbcType=VARCHAR}
// 这里使用的是AND 或是OR
</if>
</select>
如果使用了<where>标签之后,就可以如下
<select id="getinfo" resultMap="xxx" parameterType="xxx">
select *
from student
<where>
<if test="orderNumer!=null">
order_number=#{orderNumber,jdbcType=VARCHAR}
</if>
<if test="studentId!=null">
AND
studentId=#{studentId,jdbcType=VARCHAR}
</if>
</where>
</select>
这样的话就用<where >代替了where
if+ set 标签
在update的时候也需要对参数进行判空之类的判断,所以也是用到了if标签
代码如下:
<update id="update_student" parameterType="xxx" >
update student
<set>
<if test="studentId!=null and studentName!='' ">
studentId=#{studentName}
</if>
<if>
...
</if>
</set>
where student_age=#{student_age};
</update>
使用if+trim 代替where/set等标签
trim的使用非常的简单:
trim中可设置的包括
prefix 前缀 suffix 后缀 suffixOverrides 间隔符
如下代码代表
(, , , ,... ) 前后是括号() 中间是 ,
<trim
prefix
="("
suffix
=")"
suffixOverrides
=","
>
...
</trim>
<trim prefix ="WHERE" prefixOverrides="AND|OR"> //
用来代替<where >
<trim prefix ="SET" prefixOverrides=","> //
用来代替<set>
choose (when otherwise)
类似于switch语句,choose是switch,when是case,otherwise是default
choose标签是按照顺序判断内部when标签中的test条件是否成立,如果有一个成立则choose结束
...
<where>
<choose>
<when test="studentName!='' ">
student_name=#{student_name}
</when>
...
<when>
...
</when>
<otherwise>
...
</otherwise>
</choose>
</where>
foreach
1.参数是array的时候
public List<StudentEntity> getStudentListByClassIds_foreach_array(
String[] classIds);
<foreach collection="
array
" item="classIds" open="(" separator="," close=")">
#{classIds}
</foreach>
2.参数是List的时候
public List<StudentEntity> getStudentListByClassIds_foreach_list(
List<String> classIdList);
<foreach collection="
list
" item="classIdList" open="(" separator="," close=")">
#{classIdList}
</foreach>