MyBaits中的动态SQL语句

MyBaits中的动态SQL语句

很多时候我们的sql是不确定的,像参数是个条件的时候,这个时候我们很难将sql写死了,这个时候动态的sql语句就起很大作用了。

1.动态Sql中的if和where标签

xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.UserMapper">

    <select id="findByUser" resultMap="findUser"  parameterType="com.wang.domain.User">
        select * from user
        <where>
        <if test="user_username!=null">
            and username like #{user_username}
        </if>
        </where>
    </select>

    <resultMap id="findUser" type="com.wang.domain.User">
        <id column="id" property="user_id"></id>
        <result column="username" property="user_username"></result>
        <result column="age" property="user_age"></result>
    </resultMap>
</mapper>

这里有个小细节,我故意写成实体类中的属性名和数据库中的不一致,这里在查询的时候if标签的test属性里面的属性名应该写实体类中的属性名,还有条件后面的也是。但是sql拼接的就得是数据库的的列名。

测试类:

    @Test
    public void test02() throws Exception {
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = new User();
        user.setUser_username("%王%");
        List<User> user1 = mapper.findByUser(user);
        for (User user2 : user1) {
            System.out.println(user2);
        }
        session.close();
        is.close();
    }

上面的方法中出现了几个标签:

where标签:它是用来取代sql中的where 1=1的,条件标签

if标签:里面写条件,前面需要用and

这里还有一个小细节:如果if标签中的test属性中的条件不止一个时候,这个时候我们需要用and连接,切记不能用&&符号,因为这个是属于sql,我们必须按照sql中的语法来写,不能按照java中的语法。

2.动态sql之foreach标签

很多时候,我们查询一些记录时候可能有多种情况,例如查询id为1,2,3的三条记录,在mybatis中照样可以实现。

先来编写一个实体类用来封装id

@Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class QueryID implements Serializable {
    List<Integer> ids;
}

mapper的编写

public interface UserMapper {
    //多个id查询用户
    List<User> findById(QueryID id);
}

xml的编写

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wang.mapper.UserMapper">

    <select id="findById" resultMap="findUser" parameterType="com.wang.domain.QueryID">
        select * from user
        <where>
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="id in (" close=")" item="uid" separator=",">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>
        <resultMap id="findUser" type="com.wang.domain.User">
        <id column="id" property="user_id"></id>
        <result column="username" property="user_username"></result>
        <result column="age" property="user_age"></result>
    </resultMap>
</mapper>

这里的foreach中很多属性,这里分别简单介绍一下:

collection要遍历的集合元素
open语句开始的部分
close语句结束的部分
item遍历集合中每个元素,生成的别名
separator分隔符

测试类:

    @Test
    public void test03() throws Exception {
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession(true);
        UserMapper mapper = session.getMapper(UserMapper.class);

        QueryID queryID = new QueryID();
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        queryID.setIds(ids);

        List<User> byId = mapper.findById(queryID);
        for (User user : byId) {
            System.out.println(user);
        }
        session.close();
        is.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值