mybatis学习笔记(三) 动态sql

在mybatis的映射文件中有如下一些标签可以使用 if , chose , where ,set , foreach , trim , include , 使用这些标签 可以帮助我们动态的生成sql


用法实例

 <!-- 动态sql -->
    <select id="selectByNameAndPwd" parameterType="map" resultType="User" >
    	select
    	<include refid="queryFields"/>
    	from `user` 
		<where> 
	    	<if test="userName != null">
	    		user_name = #{userName}
	    	</if>
	    	<if test="password != null">
	    		and password = #{password}
	    	</if>
    	</where>
    </select>
    
    <select id="selectByIds" parameterType="list" resultMap="userResultMap" >
    	select
    	<include refid="queryFields"/>
    	from `user` 
		<where> 
		    id in 
			<foreach collection="list" index="index" item="item" open="(" close=")" separator=","   >
				#{item}
			</foreach>
		</where>
    </select>
    
    <update id="updateUserInfo" parameterType="User">
    	update `user`
    	<set>
    		<if test="userAge != null"> user_age = #{userAge} ,</if>
    		<if test="userAddress != null"> user_address = #{userAddress} , </if>
    		<if test="password != null">password = #{password}</if>
    	</set>
    	where id = #{id}
    </update>
    

注意:当select的结果集为list时,需要指定resultMap 


测试类:

package com.zf.mybatis;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.zf.mybatis.pojo.User;

public class TestMyBatis03 {

	private  SqlSessionFactory sqlSessionFactory;
	private  Reader reader; 

	@Before
	public void init(){
		try {
			reader  = Resources.getResourceAsReader("mybatis-config.xml");
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testQueryByNameAndPwd(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			Map<String, Object> param = new HashMap<String, Object>() ;
			param.put("userName", "abc") ;
			param.put("password", "1");
			User user = (User) session.selectOne("com.zf.mybatis.pojo.UserMapper.selectByNameAndPwd" ,param);
			if(user != null){
				System.out.println(user.getUserAddress());
				System.out.println(user.getUserName());
			}else{
				System.out.println("未找到数据");
			}
		} finally {
			session.close();
		}
	}

	@Test
	public void testQueryByIds(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			List<Integer> list = new ArrayList<Integer>() ;
			list.add(1);
			list.add(2) ;
			list.add(5) ;
			List<User> users = session.selectList("com.zf.mybatis.pojo.UserMapper.selectByIds" ,list);
			if(users != null){
				for (User user : users) {
					System.out.println(user.getUserAddress());
					System.out.println(user.getUserName());
				}
			}else{
				System.out.println("未找到数据");
			}
		} finally {
			session.close();
		}
	}

	@Test
	public void testUpdateUser(){
		SqlSession session = sqlSessionFactory.openSession();
		try {
			User user = new User() ;
			user.setId(1);
			user.setUserAge(88) ;
			user.setUserAddress("123");
			user.setPassword("321");
			int updateCount = session.update("com.zf.mybatis.pojo.UserMapper.updateUserInfo" ,user );
			session.commit() ;
			System.out.printf("修改了%s" , updateCount ==1 ? "成功" : "失败");
		} finally {
			session.close();
		}
	}





}


项目文件下载:mybatis-study-03.zip


你可能感兴趣的:(mybatis学习笔记(三) 动态sql)