1、MyBatisz中模糊查询
1.1 #{}和${}的区别
${}与#{}的区别
#{}是占位符
使用模糊查询需要在参数传入之前给参数做拼接处理
${}可以把字符做**原样输出**,与sql语句做拼接
1.只能使用用了 @param() 做注解的参数
2.${}常用于sql语句前部分,分表分库使用
由此可见无论是#{}还是${}都容易造成sql注入问题 所以此时我们使用concat帮我们做处理
1.2concat函数实现
cancat函数,既保留的占位符,又能拼接需要的字符串
例如模糊查询
<!--
concat()函数
concat(''1','1','1','1','1')
=>11111
-->
<select id="custLike" resultType="com.atguigu.entity.Customer">
select id, `name`,sex,email from customer where `name` like concat("%",#{name},"%")
</select>
2.自定义结果集resultMap标签
2.1一对一案例
2.1.1立即加载
## 一对一数据表
## 创建锁表
create table t_lock(
`id` int primary key auto_increment,
`name` varchar(50)
);
## 创建钥匙表
create table t_key(
`id` int primary key auto_increment,
`name` varchar(50),
`lock_id` int ,
foreign key(`lock_id`) references t_lock(`id`)
);
## 插入初始化数据
insert into t_lock(`name`) values('阿里巴巴');
insert into t_lock(`name`) values('华为');
insert into t_lock(`name`) values('联想');
insert into t_key(`name`,`lock_id`) values('马云',1);
insert into t_key(`name`,`lock_id`) values('任正非',2);
insert into t_key(`name`,`lock_id`) values('柳传志',3);
创建keyMapper接口
key keyById(int id);
keyMapper.xml
<!-- 1.加载所有属性并立即加载-->
<resultMap id="keyById_resultMap" type="com.atguigu.entity.key">
<id column="id" property="id" />
<result column="name" property="name" />
<!-- 级联-->
<result column="lock_id" property="lock.id"/>
<result column="lock_name" property="lock.name"/>
</resultMap>
<!--一次查询-->
<select id="keyById" resultMap="keyById_resultMap" parameterType="int">
select
t_key.id,
t_key.`name`,
t_lock.id lock_id,
t_lock.`name` lock_name
from
t_key,t_lock
where
t_key.id = t_lock.id
and
t_key.id=#{id}
</select>
2.1.2懒加载(分步查询)
懒加载需要在config配置文件中配置
<settings>
<!--
lazyLoadingEnabled 开启延迟加载
aggressiveLazyLoading 将积极加载改为消极加载
-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
keyMapper.xml
<!-- association 分步查询-->
<resultMap id="keyById_resultMap1" type="com.atguigu.entity.key">
<id column="id" property="id" />
<result column="name" property="name" />
<!--select属性,在获取key中的lock属性时在去调用-->
<association property="lock" column="lock_id"
select="com.atguigu.mapper.LockMapper.selectById">
</association>
</resultMap>
<!--分步查询-->
<select id="keyById1" resultMap="keyById_resultMap1" parameterType="int">
select
id,
`name`,
lock_id
from
t_key
where
t_key.id=#{id}
</select>
studentMapper接口
key keyById1(int id);
studentMapper.xml
<select id="keyById1" resultMap="keyById_resultMap1" parameterType="int">
select
id,
`name`,
lock_id
from
t_key
where
t_key.id=#{id}
</select>
2.2 一对多案例
2.2.1 立即加载
<!-- 一对多关系查询 立即加载-->
<resultMap id="queryOne_resultMap" type="com.atguigu.entity.Clazz">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--
collection 返回集合 适用于一对多关系
ofType: 返回的封装记录的具体类型
-->
<collection property="student" ofType="com.atguigu.entity.Student">
<id property="id" column="stuId"/>
<result column="stuName" property="name"/>
</collection>
</resultMap>
<select id="queryOne" resultMap="queryOne_resultMap">
select
t_clazz.id,
t_clazz.`name`,
t_student.id stuId,
t_student.`name` stuName
from
t_clazz
left join t_student on t_clazz.id = t_student.clazz_id
where
t_clazz.id = #{id}
</select>
2.2.2 懒加载及多参数传递
学生接口
List<Student> queryLikeStu(@Param("stuId") int id,@Param("stuName") String name);
班级xml文件中的sql语句
<!-- 分步查询 懒加载 多参数-->
<resultMap id="queryLikeClazz_resultMap" type="com.atguigu.entity.Clazz">
<id property="id" column="id"/>
<result column="name" property="name"/>
<!-- 多参 {参数名=参数,参数名=参数}-->
<collection property="student" column="{stuId=id,stuName=name}" ofType="com.atguigu.entity.Student"
select="com.atguigu.mapper.StudentMapper.queryLikeStu"/>
</resultMap>
<select id="queryLikeClazz" resultMap="queryLikeClazz_resultMap">
select
t_clazz.id,
t_clazz.`name`
from
t_clazz
where
t_clazz.id = #{id}
</select>