public class User {
private Integer id;
private String password;
private String username;
private Date birthday;
//省略getter setter方法
}
public class CompPojo {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
public interface UserService {
User getUserById();
List<User> getUserByUsernamePassword(User user);
User getUserByPojoProp(CompPojo comp);
List<User> selectUserByArray(Integer[] userIds);
List<User> selectUserByList(List<Integer> userIds);
List<User> selectUserByMap(Map<String,List<Integer>> userIds);
}
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userDAO;
@Override
public User getUserById(Integer id) {
return userDAO.getUserById(id);
}
@Override
public List<User> getUserByUsernamePassword(User user) {
return userDAO.getUserByUsernamePassword(user);
}
@Override
public User getUserByPojoProp(CompPojo comp) {
return userDAO.getUserByPojoProp(comp);
}
@Override
public List<User> selectUserByArray(Integer[] userIds) {
return userDAO.selectUserByArray(userIds);
}
@Override
public List<User> selectUserByList(List<Integer> userIds) {
return userDAO.selectUserByList(userIds);
}
@Override
public List<User> selectUserByMap(Map<String, List<Integer>> userIds) {
return userDAO.selectUserByMap(userIds);
}
}
public interface UserMapper {
User getUserById(Integer id);
List<User> getUserByUsernamePassword(User user);
User getUserByPojoProp(CompPojo comp);
List<User> selectUserByArray(Integer[] userIds);
List<User> selectUserByList(List<Integer> userIds);
List<User> selectUserByMap(Map<String,List<Integer>> userIds);
}
<!--根据用户ID查询用户信息
#{id}表示占位符,会将传递过来的参数填充到查询条件中,其中#{id}写法中的id可以是任意的字符串
提示,parameterType可以省略
-->
<select id="getUserById" resultType="com.itheima.info.domain.User">
select * from user where id = #{id}
</select>
@Autowired
private UserService userService;
@Test
public void testId() {
User user = userService.getUserById(1);
System.out.println(user);
}
User [id=1, password=null, username=用户1, birthday=Wed Oct 10 00:00:00 CST 2018]
<!--根据用户名 密码 查询用户信息
其中#{username}中的username是入参的对象user的属性名,#{password}中的password是入参的对象user的属性名
-->
<select id="getUserByUsernamePassword" resultType="com.itheima.info.domain.User">
select * from user where username = #{username} and password = #{password}
</select>
@Test
public void testUsernamePassword() {
//封装一个User类 查询入参
User user = new User();
user.setUsername("用户1");
user.setPassword("1");
List<User> results = userService.getUserByUsernamePassword(user);
for (User resUser : results) {
System.out.println(resUser);
}
}
<!--根据封装的POJO中的User用户ID 查询用户信息
其中#{user.id}user为入参POJO的属性,id为user的属性
-->
<select id="getUserByPojoProp" resultType="com.itheima.info.domain.User">
select * from user where id = #{user.id}
</select>
@Test
public void testCompPojo() {
//封装一个User类
User user = new User();
user.setId(1);
//将user封装到CompPojo
CompPojo comp = new CompPojo();
comp.setUser(user);
User result = userService.getUserByPojoProp(comp);
System.out.println(result);
}
<!-- mybatis对数组的处理 根据用户id 1 2 3查询用户列表
入参如果是数组类型,则可以直接使用array来接收数据。在<foreach>中的collection属性值array即获取入参数据
-->
<select id="selectUserByArray" resultType="com.itheima.info.domain.User" >
select *
from user
<where>
<foreach collection="array" open="and id in(" close=")" item="userId" separator=",">#{userId}</foreach>
</where>
</select>
@Test
public void testArray() {
//将1,2,3封装到数组 入参
Integer[] ids = {1,2,3};
List<User> users = userService.selectUserByArray(ids);
for (User user : users) {
System.out.println(user);
}
}
User [id=1, password=1, username=用户1, birthday=Wed Oct 10 00:00:00 CST 2018]
User [id=2, password=2, username=用户2, birthday=Wed Oct 10 00:00:00 CST 2018]
User [id=3, password=3, username=用户3, birthday=Wed Oct 10 00:00:00 CST 2018]
<!-- mybatis对集合的处理 根据用户id 1 2 3查询用户列表
入参类型是List,可以直接使用"list"来获取入参数据,参见<foreach>中collection="list"可以直接遍历入参的集合数据
-->
<select id="selectUserByList" resultType="com.itheima.info.domain.User" >
select *
from user
<where>
<foreach collection="list" item="userId" open="and id in(" close=")" separator=",">#{userId}</foreach>
</where>
</select>
@Test
public void testList() {
//准备List集合 入参
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
List<User> users = userService.selectUserByList(ids);
for (User user : users) {
System.out.println(user);
}
}
User [id=1, password=1, username=用户1, birthday=Wed Oct 10 00:00:00 CST 2018]
User [id=2, password=2, username=用户2, birthday=Wed Oct 10 00:00:00 CST 2018]
User [id=3, password=3, username=用户3, birthday=Wed Oct 10 00:00:00 CST 2018]
<!-- mybatis对map的处理
入参类型为map集合,则可以根据key的名称来取值,比如入参的map中,存在key为userList,可以直接在<foreach>的collection="userList"使用key获取数据
-->
<select id="selectUserByMap" resultType="com.itheima.info.domain.User" >
select *
from user
<where>
<foreach collection="userList" item="userId" open="and id in(" close=")" separator=",">
#{userId}
</foreach>
</where>
</select>
@Test
public void testMap() {
//向map集合中存放key为userList,value为List集合的数据
Map<String, List<Integer>> idMap = new HashMap<String, List<Integer>>();
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
idMap.put("userList", ids);
List<User> users = userService.selectUserByMap(idMap);
for (User user : users) {
System.out.println(user);
}
}
User [id=1, password=1, username=用户1, birthday=Wed Oct 10 00:00:00 CST 2018]
User [id=2, password=2, username=用户2, birthday=Wed Oct 10 00:00:00 CST 2018]
1.33 MB, 下载次数: 68
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |