一.最基本用法
1.增加
<!-- id为映射的方法名,
parameterType参数类型,
useGenerateKeys="true", 使用自增主键获取主键值策略
keyPropert="id" 指定对于主键值,(用于反向给值)
-->
<insert id="方法名" parameterType="类路径"
useGenerateKeys="true"
keyPropert="id"
>
insert into 表名(字段1, 字段2, 字段3)
values(#(参数1), #{字段2}, #{字段3})
</insert>
2.更新
<update id="方法名" >
update 表名
set 列1=#{参数1}, 列2=#{参数2}, 列3=#{参数3}
where id=#{id}
</update>
3.删除
<delete id="方法名" >
delete from 表
where id=#{id}
</delete>
42.查询
<!--resultType查询的对象-->
<select id="方法名" resultType="类路径">
select from 列1, 列2, 列 3
where id=#{id}
</select>
二.参数处理
1.当传入一个参数是没问题,
void test(int id);
selet … where id= #{id}
2.当传入两个是就有问题了*** 会报错 ***
void test(int id,string name);
selet … where id= #{id} and name = #{name}
3.只能写成
selet … where id= #{0} and name = #{1} 或
selet … where id= #{param1} and name = #{param2}
4.@param
void test(@param(“id”) int id,@param("name ") string name);
selet … where id= #{id} and name = #{name}
5.POJP|传入模型,可以直接使用属性值
void test(Pople pople);
selet … where id= #{id} and name = #{name}
6.POJP与@param 一起时
void test(@param(“id”) int id,@param(“pople”) Pople pople)
select …
where id=#{id} and name = #{[pople.name}
7.MAP|传入map, 可直接取值 (不经常使用的参数,建议用map)
void test(map<string, Object>);
selet … where id= #{id} and name = #{name}
8.TO| 不是业务模型,但是经常使用,推荐写一个TO(Tramsfer Object)
Page{
int index;
int size;
}
9.传入Collection(lsit, set) 类似,或者数组,
会特殊处理,也就是吧list或数组封装在map中
void test(List<> ids)
当需要取出第一个是select … where id = #{list[0]}
需要写成为list, 不能是ids
三. 参数的获取
#{} 与 ¥{} 的区别
#{} 以预编译的方式,将参数设置到sql语句中,防sql注入
KaTeX parse error: Expected 'EOF', got '#' at position 39: …全问题 大部分情况下都应该使用#̲{} 原生jdbc不支持占位符…{ } 进行去除
比如分表, 按照年份拆分
select * from 2021_year
select * ${year}_year
四. 返回值
1.返回list
void List<pople> testList(int name);
<select id="方法testList" resultType="类pople">
select from 列1, 列2, 列 3
where name like %#{name}%
</select>
<!--resultType中写List中的对象即可, myBatis会自动打包-->
2.返回map(单条记录)
void Map<String, Object> testList(int id);
<select id="方法testList" resultType="map">
select from 列1, 列2, 列 3
where id=#{id}
</select>
<!--resultType中写List中的对象即可, myBatis会自动打包-->
返回map (多条记录)
<!--多条记录封装一个map, 想使key为这条记录的主键,值是对象·-->
<!--@mapkey 告诉哪个属性做为主键-->
@MapKey("id")
void Map<Integer, Pople> testList(String name);
<select id="方法testList" resultType="Pople">
select from 列1, 列2, 列 3
where name like #{name}
</select>
<!--resultType中写List中的对象即可, myBatis会自动打包-->
五.resultMap
1.自定义返回类型映射关系
<!--指定主键列的封装贵州
id定义主键会底层优化;
cloumn,指定那一列
property:指定对于的javaBean属性
-->
<resultMap Type="pople" id="test">
<id colun='id' peoperty="id" />
<result column="last_name" perperty="lastName"/>
<!--其他不指定的列会自动封装-->
</resultMap>
<select id="方法名" resultMap="test">
2.resultMap联合查询
自定义返回类型映射关系
<!--指定主键列的封装贵州
id定义主键会底层优化;
cloumn,指定那一列
property:指定对于的javaBean属性
-->
<resultMap Type="pople" id="test">
<id colun='id' peoperty="id" />
<result column="last_name" perperty="lastName"/>
<association property="dept(列名)" javaType="类中子类">
<id colun='did' peoperty="id" />
<result column="dapt_name" perperty="departmentName"/>
</associatino>
<!--其他不指定的列会自动封装-->
</resultMap>
<select id="方法名" resultMap="test">
3.resultMap联合查询,分段查询
<!--指定主键列的封装贵州
id定义主键会底层优化;
cloumn,指定那一列
property:指定对于的javaBean属性
-->
<resultMap Type="pople" id="test">
<id colun='id' peoperty="id" />
<result column="last_name" perperty="lastName"/>
<!--select:表示当前属性是调用select指定的方法差出的结果
colimn:指定那一列的值传给这个方法
-->
<association property="dept(列名)" select="空间加方法.方法名"
colimn="d_id"
>
</associatino>
<!--其他不指定的列会自动封装-->
</resultMap>
<select id="方法名" resultMap="test">
4.resultMap联合查询,延迟加载,
在使用到的时候再去加载,
在分段查询的基础上加两个配置
<!--指定主键列的封装贵州
id定义主键会底层优化;
cloumn,指定那一列
property:指定对于的javaBean属性
-->
<!--myBatis配置文件-->
<settings>
<!--是否开启延迟加载, 默认其实是为ture, 防止版本更新,照成问题,手动设置-->
<setting name="lazyLoadingEnabled" vaule="true"/>
<!--当为true时, 用到一个属性,会加载全部属性-->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
5. collection嵌套结果集的方式,定义关联的集合类型元素的封装规则
查询部门的时候,查出所属的全部员工
public calss Department {
Integer id;
private String departmentName;
private List<Employee> emps;
}
<resultMap type="类" id="别名">
<id colun='id' peoperty="id" />
<result column="last_name" perperty="lastName"/>
<!--collectino定义关联集合类型的属性封装规则
ofType:指定记录里面的元素的类型
-->
<collection perperty="列名" ofType="类地址">
<!--定义这个集合中元素的封装规则
column 为sql的列,
property 为实体对象所对应的列
-->
<id colum="eid" property="id" />
<result column="last_name" property="last_name"/>
</conllection>
</resultMap>
6 collection, colimn 传入多个对象
column={deptId = id, deptName = name}
7.collection, fetchType属性
fetchType=‘lazy’ //延迟加载
fetchType=‘eager’ //立即加载
8. resultMap 鉴别器属性
鉴别器,myBatis可以使用discriminator判断 某列的值,然后根据某列的值改变封装行为
<resultMap Type="pople" id="test">
<id colun='id' peoperty="id" />
<result column="last_name" perperty="lastName"/>
<!--
column:指定判定的列名
javaType:列值对应的java类型
-->
<discriminator javaType="string" column="last_name">
<!--指定封装结果类型-->
<case value="0" resultType="类型">
<id ..>
<result ../>
</case>
<case value="1" resultType="类型2">
<id ..>
<result ../>
</case>
</resultMap>