MyBatis配置动态sql语句
CREATE DATABASE `mybatis_study`;
USE `mybatis_study`;
CREATE TABLE `user`(
`user_id` INT(20) NOT NULL PRIMARY KEY,
`user_name` VARCHAR(30) DEFAULT NULL,
`password` VARCHAR(30) DEFAULT NULL
)ENGINE = INNODB DEFAULT CHARSET = utf8;
INSERT INTO `user` (`user_id`,`user_name`,`password`) VALUES
(1, '张三', '123456'),
(2, 'admin', 'admin'),
(3, 'root', 'root');
在 MyBatis 的 sql映射文件中,有时候需要根据一些查询条件,来选择不同的sql语句,如果每一个场景都重写sql,很显然效率没有很高,而 MyBatis 的动态sql很好的解决了这种问题,根据条件动态的处理 sql, 特别简单的说就是,写一次sql,但是根据分支等的跳转,在多个场景下也可以使用,例如:
- 当查询条件由于参数不同而无法确定具体是什么,可以使用
<where>
标签包含 - 在
<where>
可以使用<if test="....">
分条件进行处理,实现动态 <foreach>
遍历标签,在用户中查询寻多个id,例如(12,16,17)
在此之外,动态sql同时结局了,在原生 JDBC 中需要拼接sql语句时由于书写问题,而导致报错
(一) where 和 if 标签
UserMapper 接口
List<User> getUserList(User user);
UserMapper.xml
<select id="getUserList" resultType="com.test.pojo.User" parameterType="com.test.pojo.User">
select * from mybatis_study.user
<where>
<if test="user_name != null">
and user_name = #{user_name}
</if>
<if test="password != null">
and password = #{password}
</if>
</where>
</select>
注意:在sql中,“and” 用来拼接已有一个或多个查询条件的语句,当此语句为第一个查询条件的时候,会因为 的存在屏蔽第一个 “and”
UserTest
import com.test.MyBatisUtils;
import com.test.mapper.UserMapper;
import com.test.pojo.User;
import org.apache.ibatis.session.sqlSession;
import org.junit.Test;
import java.util.List;
public class UserTest {
@Test
public void test(){
sqlSession sqlSession = MyBatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUser_name("admin");
List<User> userList = mapper.getUserList(user);
for (User u:userList) {
System.out.println(u);
}
sqlSession.close();
}
}
执行效果
(二) foreach标签
提出这样一种需求,在用户中查询寻多个id,例如(12,16,17)我们可以这样写sql
select * from mybatis_study.user where user_id=1 or user_id=2 or user_id=3
或者这样
select * from mybatis_study.user where id in (1,2,3)
而这种情况下,我们需要向sql中传递一个数据或者List类型的参数,然后使用 标签去遍历然后解析
UserMapper 接口
List<User> getUserList(List<Integer> ids);
UserMapper.xml
<select id="getUserList" resultType="com.test.pojo.User" parameterType="list">
select * from mybatis_study.user
<where>
<foreach collection="list" open="and user_id in (" close=")" item="item" separator=",">
#{item}
</foreach>
</where>
</select>
foreach元素的属性主要有item,index,collection,open,separator,close。
- item:集合中元素迭代时的别名,该参数为必选。
- index:在list和数组中,index是元素的序号,在map中,index是元素的 key,该参数可选
- open:foreach代码的开始符号,一般是和close=")"合用。常用在in(),values()时。该参数可选
- separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
- close: foreach代码的关闭符号,一般是和open="("合用。常用在in(),values()时。该参数可选。
- collection: 要做foreach的对象,作为入参时,List对象默认用"list"代替作为键,数组对象有"array"代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param(“keyName”)来设置键,设置keyName后,list,array将会失效。
UserTest
import com.test.MyBatisUtils;
import com.test.mapper.UserMapper;
import com.test.pojo.User;
import org.apache.ibatis.session.sqlSession;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class UserTest {
@Test
public void test(){
sqlSession sqlSession = MyBatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<Integer> ids = Arrays.asList(1, 2, 3);
List<User> userList = mapper.getUserList(ids);
for (User u:userList) {
System.out.println(u);
}
sqlSession.close();
}
}
执行效果
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。