万能Map
假设实体类或者数据库的字段或者参数过多,应当使用map,因为new User时必须写所有的参数,过于繁琐
//万能map
int addUser2(Map<String,Object> map);
<!--传递map中的key-->
<insert id="addUser2" parameterType="map">
insert into mybatis.user(id,name,pwd) values(#{userid},#{username},#{password})
</insert>
//增删改需要提交事务
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid",6);
map.put("username","holle");
map.put("password","163736");//可以不用全写
mapper.addUser2(map);
Map传递参数,直接在sql中取出key即可!【parameterType=“map”】
对象传递参数,直接在sql中取对象的属性即可!【parameterType=“Object”】
只有一个基本参数的情况下,可以直接在sql中取到!
多个参数用map,或者注解
模糊查询
List<User> getUserLike(String value);
<select id="getUserLike" resultType="com.faq.pojo.User">
select * from mybatis.user where name like #{value}
</select>
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getsqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserLike("%李%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
模糊查询注意点:
1.Java代码执行时,传递通配符% %
2.在sql拼接中使用通配符
select * from mybatis.user where name like "%"#{value}"%"