其他分享
首页 > 其他分享> > mybatis-获取参数值的方式

mybatis-获取参数值的方式

作者:互联网

MyBatis获取参数值的两种方式(重点)

单个字面量类型的参数

<!--User getUserByUsername(String username);-->
<select id="getUserByUsername" resultType="User">
	select * from t_user where username = #{username}
</select>
<!--User getUserByUsername(String username);-->
<select id="getUserByUsername" resultType="User">  
	select * from t_user where username = '${username}'  
</select>

多个字面量类型的参数

<!--User checkLogin(String username,String password);-->
<select id="checkLogin" resultType="User">  
	select * from t_user where username = #{arg0} and password = #{arg1}  
</select>
<!--User checkLogin(String username,String password);-->
<select id="checkLogin" resultType="User">
	select * from t_user where username = '${param1}' and password = '${param2}'
</select>

map集合类型的参数

<!--User checkLoginByMap(Map<String,Object> map);-->
<select id="checkLoginByMap" resultType="User">
	select * from t_user where username = #{username} and password = #{password}
</select>
@Test
public void checkLoginByMap() {
	SqlSession sqlSession = SqlSessionUtils.getSqlSession();
	ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
	Map<String,Object> map = new HashMap<>();
	map.put("usermane","admin");
	map.put("password","123456");
	User user = mapper.checkLoginByMap(map);
	System.out.println(user);
}

实体类类型的参数

<!--int insertUser(User user);-->
<insert id="insertUser">
	insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
</insert>
@Test
public void insertUser() {
	SqlSession sqlSession = SqlSessionUtils.getSqlSession();
	ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
	User user = new User(null,"Tom","123456",12,"男","123@321.com");
	mapper.insertUser(user);
}

整体测试类代码

package com.atguigu.mybatis.test;

import com.atguigu.mybatis.mapper.ParameterMapper;
import com.atguigu.mybatis.pojo.user;
import com.atguigu.mybatis.utils.sqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.List;

public class ParameterMapperTest {
    @Test
    public void TestGetALLUser(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        List allUser = mapper.getAllUser();
        allUser.forEach(user-> System.out.println(user));
    }

    /**
     * mybatis 获取参数值的两中方式:${}  和 #{}
     * 1:${}  本质就是字符串拼接
     * 2:#{} 本质就是占位符赋值
     *
     *   select * from t_user where username = #{username} // '${userame}'
     *
     *  可以通过${} 和 #{}以任意字符串来获取参数值,但是需要注意${}的单引号问题
     */
    @Test
    public void TestGetUserByUsername(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        user admin = mapper.GetUserByUsername("张三");
        System.out.println(admin.toString());

    }

    /**
     * mapper接口方法的参数为多个时,、此时mybatis会将这些参数放到一个map集合中,以两种方式进行存储
     * a> 以arg0、arg1、、、、、、为健,以参数为值
     * b> 以param1,param2、、、、为健,以参数为值
     * 因此只需要通过#{} 和 ${} 以键的方式访问即可、但是需要注意的${}的单引号问题
     *
     */
    @Test
    public void testCheckLogin(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        user user1= mapper.checkLogin("张三", "123456");
        System.out.println(user1.toString());
    }
    @Test
    public void testCheckLoginByMap(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("username","张三");
        objectObjectHashMap.put("password","123456");
        user user1 = mapper.checkLoginMap(objectObjectHashMap);
        System.out.println(user1);
    }
    /**
     * 4.mapper接口方法中的参数是一个实体类的类型的参数时,如何访问?
     * 只需要通过#{}和${}以属性的方式访问属性值即可,但是需要注意的${}的单引号问题
     */
    @Test
    public void testInsertUser(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        user user = new user(null, "lisai", "123456", 23, "男", "wiseleer@163.com");
        int i = mapper.insertUser(user);
        System.out.println(i);
    }
    /**
     * 使用@param测试
     */
    @Test
    public void testcheckLoginByParam(){
        SqlSession sqlSession = sqlSessionUtils.GetSqlSession();
        ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
        user user1= mapper.checkLoginByParam("张三", "123456");
        System.out.println(user1.toString());
    }
}

标签:username,mapper,ParameterMapper,map,获取,sqlSession,user,mybatis,参数值
来源: https://www.cnblogs.com/wiseleer/p/16457638.html