写入方法 List getUserById(Map<String,Integer> map);
怎么说呢使用map的好处就是可以用键值对,然后#{ xxx} xxx里面为键,这样我们可以自由的书写,不用和pojo类的一一对应
package com.zsp.dao;
import com.zsp.pojo.User;
import java.util.List;
import java.util.Map;
public interface UserMapper {
List<User> getUserList();
List<User> getUserLike(String like);
List<User> getUserById(Map<String,Integer> map);
}
配置方法id
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=第定·个对应mDao/Mapper接-->
<mapper namespace="com.zsp.dao.UserMapper">
<!-- type里面写泛型的东西 -->
<select id="getUserList" resultType="com.zsp.pojo.User">
select *from mybaties.user;
</select>
<select id="getUserLike" parameterType="String" resultType="com.zsp.pojo.User">
select *from mybaties.user where name like #{like} ;
</select>
<select id="getUserById" parameterType="map" resultType="com.zsp.pojo.User" >
select * from mybaties.user where id=#{myid};
</select>
</mapper>
测试map
package com.zsp.dao;
import com.zsp.pojo.User;
import com.zsp.utils.MybatiesUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserMapperTest {
@Test
public void testgetUserList(){
SqlSession session = MybatiesUtils.getSession();
// 1方式: getMapper
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user: userList){
System.out.println(user);
}
// 关闭jSqlSession
session.close();
}
@Test
public void testgetUserLike(){
SqlSession session = MybatiesUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.getUserLike("%朱%");
for (User user : users) {
System.out.println(user);
}
}
@Test
public void getUserById(){
SqlSession session = MybatiesUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
Map<String,Integer> map =new HashMap<>();
map.put("myid",1);
List<User> users = mapper.getUserById(map);
for (User user : users) {
System.out.println(user);
}
}
}