mybatis关于动态SQL语句

mybatis动态SQL语句,主要的元素有

  • if
  • choose(when,otherwise)
  • trim
  • where
  • set
  • foreach

接下来看看它们的使用

在mapper下创建接口类DynamicSql

import org.example.beans.User;

import java.util.List;

public interface DynamicSql {
    /*if*/
    List<User> getAll(User user);
    //choose
    List<User> getChoose(User user);

    //where
    List<User> getWhere(User user);

    //trim
    List<User> getTrim(User user);

    //set
    void set(User user);

    //forEach
    List<User> getByIds(int[] ids);
}

在src/main/resources/mappers下创建DynamicSqlMapper.xml




<mapper namespace="org.example.mapper.DynamicSql">
    <select id="getAll" parameterType="org.example.beans.User" resultType="org.example.beans.User">
        select * from user where password = "7"
        <if test="name!=null">
            and name=#{name}
        if>
    select>

    <select id="getChoose" resultType="org.example.beans.User">
        select * from user where password = "7"
        <choose>
            <when test="name!=null">
                and name=#{name}
            when>
            <otherwise>
                and name="8"
            otherwise>
        choose>
    select>

    <select id="getWhere" resultType="org.example.beans.User">
        select * from user
        <where>
            <if test="name!=null">
                name=#{name}
            if>
        where>
    select>

    <select id="getTrim" resultType="org.example.beans.User">
        select * from user
        <trim prefix="where" prefixOverrides="and|or" suffix="">
            <if test="name!=null">
                and name=#{name}
            if>
            <if test="id!=0">
                and id=#{id}
            if>
        trim>
    select>

    <select id="set" resultType="org.example.beans.User">
        update user
        <set>
            <if test="name!=null">
                name=#{name}
            if>
        set>
        where id=#{id}
    select>

    <select id="getByIds" parameterType="int" resultType="org.example.beans.User">
        select * from user where id in
        <foreach collection="array" item="id" open="(" close=")" separator="," >
            #{id}
        foreach>
    select>
mapper>

编写测试类DynamicSqlTest

public class DynamicSqlTest {
    private SqlSessionFactory sessionFactory;

    @Before
    public void init(){
        InputStream is = null;

        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            sessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void getAll(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setName("7");
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.getAll", user);
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void getChoose(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setName("8");
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.getChoose", user);
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void getWhere(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setName("8");
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.getWhere", user);
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void getTrim(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setName("8");
        user.setId(8);
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.getTrim", user);
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void set(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        user.setName("88");
        user.setId(8);
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.set", user);
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

    @Test
    public void getByIds(){
        SqlSession sqlSession = sessionFactory.openSession();
        User user = new User();
        List<User> users = sqlSession.selectList("org.example.mapper.DynamicSql.getByIds", new int[]{1,8,9});
        users.stream().forEach(System.out::println);
        sqlSession.close();
    }

}

数据库

mybatis关于动态SQL语句_第1张图片

你可能感兴趣的:(mybatis,mybatis,mysql,sql)