Mybatis映射文件动态SQL语句-01
生活随笔
收集整理的這篇文章主要介紹了
Mybatis映射文件动态SQL语句-01
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為在很多業務邏輯復雜的項目中,往往不是簡單的sql語句就能查詢出來自己想要的數據,所有mybatis引入了動態sql語句,
UserMapper.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.hao.mapper.UserMapper"><select id="findByCondition" parameterType="user" resultType="user">select * from user<where><if test="id!=0">and id=#{id}</if><if test="username!=null">and username=#{username}</if><if test="password!=null">and password=#{password}</if></where></select> </mapper>Dao層接口
然后測試
@Testpublic void test1() throws IOException {InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");SqlSession sqlSession = new SqlSessionFactoryBuilder().build(stream).openSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class); // 模擬條件User user = new User();user.setId(1);user.setUsername("張三");user.setPassword("123");List<User> userList = mapper.findByCondition(user);System.out.println(userList);}結果:
總結
以上是生活随笔為你收集整理的Mybatis映射文件动态SQL语句-01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mybatis-Dao层实现(通过代理方
- 下一篇: linux cmake编译源码,linu