#{}: 是个占位符,会自动加上' '
select * from t_user where username = #{username}
select * from t_user where username = ?
${}:字符串拼接,需要手动加上' '
select * from t_user where username = '${username}'
select * from t_user where username = 'admin'
<mapper namespace="com.bijing.mybatis.mapper.UserMapper">
<select id="getUserByUserName" resultType="User">
select * from t_user where username = '${username}';
select>
mapper>
<select id="checkLogin" resultType="User">
select * from t_user where username = #{arg0} and password = #{arg1};
select>
<select id="checkLoginByMap" resultType="User">
select * from t_user where username = #{username} and password = #{password};
select>
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email});
insert>
<select id="checkLoginByParam" resultType="User">
select * from t_user where username = #{username} and password = #{password};
select>
UserMapper.java
package com.bijing.mybatis.mapper;
import com.bijing.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
* @author 毕晶
* MyBatis获取参数的两种方式:#{}和${}
* #{}的本质是占位符赋值,${}的本质是字符串拼接
* 1.若mapper接口方法的参数为单个字面量类型
* 此时可以通过#{}和${}以任意内容获取参数值,一定要注意${}单引号问题
* 2.若mapper接口方法的参数为多个的字面量类型
* 此时=======MyBatis会将参数放在map集合======中,然后以两种方式存储数据
* 第一种:arg0,arg1...为键,以参数为值
* 第二种:param1,Param2...为键,以参数为值
* 因此只需要通过#{}和${}访问map集合的键,就可以获取相应的值
* 3.若mapper接口方法的参数为map集合类型的参数,
* 只需要通过#{}和${}访问map集合的键,就可以获取相应的值
* 4.若mapper接口方法的参数类型为实体类型的参数
* 只需要通过#{}和${}访问实体类中的属性名,就可以获取相对应的属性值,一定要注意${}单引号问题
* 5.可以在mapper接口方法的参数上设置@Param注解
* 此时MyBatis会将这些参数放在map中,以两种方式进行存储
* 第一种:以@Param注解的value属性值为键,以参数为值
* 第二种:以param1,param2...为键,以参数为值
* 只需要通过#{}和${}访问实体类中的属性名,就可以获取相对应的属性值,一定要注意${}单引号问题
* @date 2022/9/27 15:38
*/
public interface UserMapper {
/**
* 根据用户名查询用户信息
*
* @param username
* @return
*/
User getUserByUserName(String username);
/**
* 验证登录
*
* @param username
* @param password
* @return
*/
User checkLogin(String username, String password);
/**
* 验证登录,以map集合为参数
*
* @param map
* @return
*/
User checkLoginByMap(Map<String, Object> map);
/**
* 添加用户
*
* @param user
* @return
*/
int insertUser(User user);
/**
* 验证登录,使用@param注解
*
* @param username
* @param password
* @return
*/
User checkLoginByParam(@Param("username") String username, @Param("password") String password);
}
UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bijing.mybatis.mapper.UserMapper">
<select id="getUserByUserName" resultType="User">
select * from t_user where username = '${username}';
select>
<select id="checkLogin" resultType="User">
select * from t_user where username = #{arg0} and password = #{arg1};
select>
<select id="checkLoginByMap" resultType="User">
select * from t_user where username = #{username} and password = #{password};
select>
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email});
insert>
<select id="checkLoginByParam" resultType="User">
select * from t_user where username = #{username} and password = #{password};
select>
mapper>
ParameterTest.java
package com.bijing.mybatis.test;
import com.bijing.mybatis.mapper.UserMapper;
import com.bijing.mybatis.pojo.User;
import com.bijing.mybatis.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @author 毕晶
* @date 2022/9/27 19:04
*/
public class ParameterTest {
@Test
public void testGetUserByUsername() throws Exception {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User admin = mapper.getUserByUserName("admin");
System.out.println(admin);
}
@Test
public void checkLogin() throws Exception {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.checkLogin("admin", "123456");
System.out.println(user);
}
@Test
public void checkLoginByMap() throws Exception {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("username", "admin");
map.put("password", "123456");
User user = mapper.checkLoginByMap(map);
System.out.println(user);
}
@Test
public void testInsertUser() throws Exception {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(null, "小黑子", "abcd", 18, "男", "鸡脚@qq.com");
int result = mapper.insertUser(user);
System.out.println(user);
}
@Test
public void checkLoginByParam() throws Exception {
SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.checkLoginByParam("小黑子", "abcd");
System.out.println(user);
}
}
/**
*若sql语句的查询结果为多条时,一定不能以实体类类型作为方法的返回值
* 否则会抛出异常TooManyResultsException
* 若sql语句的查询结果为一条时,此时可以使用实体类类型或者List集合类型来作为方法的返回值
*/
/**
* 根据id查询用户信息
*
* @param id
* @return
*/
User getUserById(@Param("id") Integer id);
/**
* 查询所有用户信息
*
* @return
*/
List<User> getAllUser();
/**
* 查询用户的总数量
*
* @return
*/
Integer getCount();
/**
* 根据id查询用户信息为一个map集合
*
* @param id
* @return
*/
Map<String, Object> getUserByIdToMap(@Param("id") Integer id);
/**
* 查询所有的用户信息为map集合
* 若查询的数据有多条的时候,并且要将每条数据转换为map集合
* 此时有两种解决方案:
* 1.将mapper接口方法的返回值设置为泛型是map的list集合
* 2.可以将每条数据的所转换的Map集合放到一个大的Map集合中,
* 但是必须通过@MapKey注解设置大的Map集合的键,键对应的值就是所转换的Map集合
*
* @return
*/
// List
//把当前查询到的数据所转换的Map集合放到一个大的Map集合中,通过MapKey注解设置大的Map集合的键,键对应的值就是所转换的Map集合
@MapKey("id")
Map<String, Object> getAllUserToMap();
注意:有时查询到的数据没有实体类时,就要将它转换成一个map
/**
* 通过用户名模糊查询用户信息
*
* @param name
* @return
*/
List<User> getUserByLike(@Param("name") String name);
尽量使用第三种方式
<select id="getUserByLike" resultType="User">
select * from t_user where username like "%"#{name}"%";
select>
/**
* 批量删除
*
* @param ids
*/
void deleteMoreUser(@Param("ids") String ids);
<delete id="deleteMoreUser">
delete from t_user where id in(${ids});
delete>
/**
* 动态设置表名,查询用户信息
*
* @param tableName
* @return
*/
List<User> getUserList(@Param("tableName") String tableName);
<select id="getUserList" resultType="User">
select * from ${tableName};
select>
/**
* 添加用户信息,并获取自增主键
*
* @param user
*/
void insertUser(User user);
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email})
insert>