做项目时随手写的笔记,有点乱,第一次写,希望有所帮助呀~
大于20且小于40的的数据
Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.gt("age",new Integer(20)));
criteria.add(Restrictions.it("age",new Integer(40)));
List users = criteria.list();
tinyint 对应java中是TINYINT
int占4个字节,tinyint占1个字节,如果存储一个较小的数,用tinyint性能更高一点。
trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码:
select * from user
<trim prefix="WHERE" prefixoverride="AND |OR">
<if test="name != null and name.length()>0"> AND name=#{name}</if>
<if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
</trim>
假如说name和gender的值都不为null的话打印的SQL为:select * from user where [ ] name = ‘xx’ and gender = ‘xx’
在框标记的地方是不存在第一个and的,上面两个属性的意思如下:
prefix:前缀
prefixoverride:去掉第一个and或者是or
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="gender != null and gender.length()>0"> gender=#{gender} , </if>
</trim>
假如说name和gender的值都不为null的话打印的SQL为:update user set name='xx' , gender='xx' [ ] where id='x'
在框标记的地方不存在逗号,而且自动加了一个set前缀和where后缀,上面三个属性的意义如下,其中prefix意义如上:
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
suffix:后缀
判断 : <if test="name!=null">name</if>
<include refid="Base_Colum_List"/>
association 一对一, 一对多 collection,多对多 discrimination
<--assocication可以指定联合的JavaBean对象
property="createUser"指定哪个属性是联合的对象
javaType:指定这个属性对象的类型-->
<association property="createUser" javaType="uyun.xxx.common.entity.User"> //在里面嵌套一个resultMap
<result column="create_user_id" property="userId" jdbcType="VARCHAR" />
</association>
association方式一
<resultMap type="Address" id="AddressResult">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</resultMap>
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" resultMap="AddressResult"/> // 单独在创建一个resultMap
</resultMap>
association方式二
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" javaType="Address">
<result property="id" column="id"/>
<result property="sheng" column="sheng"/>
<result property="shi" column="shi"/>
<result property="qu" column="qu"/>
</association>
</resultMap>
association方式三
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressId"
select="com.java1234.mappers.AddressMapper.findById">
</association>
</resultMap>
foreach
//针对修改相同的数据
<update id="update" parameterType="java.util.Map">
UPDATE tableNamw SET name = #{name}
WHERE id IN
<foreach collection="list" item="entity" index="index" open="(" separator="," close=")">
#{entity.xxx}
</foreach>
</update>
//针对修改不同的数据
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" index="index" item="item"
separator=";">
update tableName set
name=#{item.name,jdbcType=VARCHAR},
age=#{item.age,jdbcType=INTEGER}
where
id=#{item.id,jdbcType=INTEGER}
</foreach>
</update>
item
循环体中的具体对象,支持属性的点路径访问,item.age,item.info.details。
具体说明:若collection属性为list或array,则item代表list或array里面的一个元素。若collection属性对应一个
map,
则item代表的是map中的value集合中的单个value,该参数为必选。
collection
foreach
遍历的对象,作为参数传入时,List对象默认用list(collection=”list”)代替作为键,数组对象有
open
foreach代码的开始符号,一般是"("和close=”)”合用。常用在in(),values()时。该参数可选。
close
foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。该参数可选。
index
在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
array
(collection=”array”)代替作为键,Map对象没有默认的键。
当然在作为入参时可以使用@Param(“params”)来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = “ids”
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = “ids.id”
如果传入参数类型为map,这个入参有注解@Param(“params”),则map的所有的key集合可以写成params.keys,所有值集合可以写成params.values。这样foreach就可以对key集合或值集合进行迭代了。
该参数为必选。
separator
元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
代码示例,演示查询的三种情况
传入list参数
//对应的Dao中的Mapper文件
public List<User> selectByIds(List<Integer> ids);
<select id="selectByIds" resultType="com.wuuushao.pojo.User">
select * from user where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
传入的参数为Array
//对应的Dao中的Mapper文件
public List<User> selectByIds(int[] ids);
<select id="selectByIds" resultType="com.wuuushao.pojo.User">
select * from user where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
传入的参数为Map
//对应的Dao中的Mapper文件
public List<User> selectByIds(Map<String, Object> params);
<select id="selectByIds" resultType="com.wuuushao.pojo.User">
select * from user where id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
//map的时候需要注意的是:collection的值“ids”是存储在map中的key
//(比如:map.put(“ids”,ids));这个不是随便乱写的,尤其需要注意;
再举一些插入更新的例子
//批量插入数据,传入参数list
<insert id="insertInfo">
insert into tableName(
id....
)
values
<foreach collection="list" item="entity" index="index"
separator=",">
(
#{entity.id,jdbcType=VARCHAR}....
)
</foreach>
</insert>
解释一下jdbcType=VARCHAR
在执行SQL时MyBatis会自动通过对象中的属性给SQL中参数赋值,它会自动将Java类型转换成数据库的类型。
而一旦传入的是null它就无法准确判断这个类型应该是什么,就有可能将类型转换错误,从而报错。
要解决这个问题,需要针对这些可能为空的字段,手动指定其转换时用到的类型。
一般情况下,我们没有必要按个字段去识别/判断它是否可以为空,而是将所有的字段都当做可以为空,全部手动设置转换类型。
(以前从来没有人说过要写这些)
<insert id="save"
parameterType="com.tarena.entity.Cost">
insert into cost values(
cost_seq.nextval,
#{name,jdbcType=VARCHAR},
#{base_duration,jdbcType=INTEGER},
#{base_cost,jdbcType=DOUBLE},
#{unit_cost,jdbcType=DOUBLE},
#{status,jdbcType=CHAR},
#{descr,jdbcType=VARCHAR},
#{creatime,jdbcType=TIMESTAMP},
#{startime,jdbcType=TIMESTAMP},
#{cost_type,jdbcType=CHAR}
)
</insert>
批量修改数据
//针对修改相同的数据
<update id="update" parameterType="java.util.Map">
UPDATE tableNamw SET name = #{name}
WHERE id IN
<foreach collection="list" item="entity" index="index" open="(" separator="," close=")">
#{entity.xxx}
</foreach>
</update>
//针对修改不同的数据
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" index="index" item="item"
separator=";">
update tableName set
name=#{item.name,jdbcType=VARCHAR},
age=#{item.age,jdbcType=INTEGER}
where
id=#{item.id,jdbcType=INTEGER}
</foreach>
</update>