封装SqlSessionUtils
/**
* 获取SqlSession
* @param b 是否自动管理事务
* @return 返回sqlsession
*/
public static SqlSession getSqlSession(boolean b){
SqlSession sqlSession = null;
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(b);
} catch (IOException exception) {
exception.printStackTrace();
}
return sqlSession;
}
获取参数值的两种方式
-
${} 字符串拼接 注意单引号问题
-
#{} 占位符赋值 能用占位符就用,不嫩用采用拼接
mapper接口方法的参数为单个字面量
#{}和${}都可以,以任意名称(建议和形参写一样的)获取参数值,但是需要注意${}的单引号问题
User getUserById(String userId);
<select id="getUserById" resultType="User">
select * from t_user where id = #{userId}
select * from t_user where id = "${userId}"
</select>
mapper接口方法的参数为多个
自动将参数放入map
此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
-
以arg0,arg1...为键,以参数为值
-
以param1,param2...为键,以参数为值
因此只需要通过#{}和${}为键的方式访问值即可,但是需要注意${}的单引号问题
User checkUser(String username, String password);
<!--User checkUser(String username, String password); 参数有多个-->
<select id="checkUser" resultType="User">
<!--select * from t_user where id = '${arg0/para1}' and password = '${arg1/param2}'-->
select * from t_user where id = #{arg0} and password = #{arg1}
</select>
手动将参数放入map集合
只需要通过#{key}和${key}以键的方式访问值即可,但是需要注意${}的单引号问题
@Test
public void testByMap(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("username","asd");
stringObjectHashMap.put("password","123");
User user = mapper.checkUserByMap(stringObjectHashMap);
System.out.println(user);
}
User checkUserByMap(Map<String, Object> map);
<select id="checkUser" resultType="User">
<!--select * from t_user where id = '${username}' and password = '${password}'-->
select * from t_user where id = #{username} and password = #{password}
</select>
**接口方法的参数是实体类类型的参数
只需要通过#{属性名}和${属性名}以键的方式访问值即可,但是需要注意${}的单引号问题
public void testByBean(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.insertUserByBean(new User(null, "ert", "789", 56, 'f', "[email protected]"));
System.out.println(i);
}
int insertUserByBean();
<!--int insertUserByBean(); 插入用户-->
<insert id="insertUserByBean">
insert into t_user values(null, #{username}, #{password}, #{age}, #{sex}, #{email})
</insert>
**使用@Param注解命名参数
建议除了实体类都用这个方法
此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
-
以@Param注解的value为键,以参数为值
-
以param1,param2...为键,以参数为值
User checkUserByAnnotation(@Param("username")String username, @Param("password")String password);
<select id="checkUserByAnnotation" resultType="User">
<!-- select * from t_user where username = #{param1} and password = #{param2} -->
select * from t_user where username = #{username} and password = #{password}
</select>
标签:username,mapper,获取,sqlSession,参数,user,mybatis,password,参数值
From: https://www.cnblogs.com/phonk/p/16607541.html