MyBatis分页查询

目标:通过对username模糊查询得到相应记录,此处使用分页

所用到的表
user
在这里插入图片描述

映射文件

<?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">
<mapper namespace="com.openlab.mapper.testMapper">

    <select id="Limitquery" resultType="User">
      SELECT
        u.id,u.username,u.password,
        r.roleName
        from
        user u right join role r
        on u.roleId= r.roleId
        where u.username like concat('%',#{0},'%')
        limit #{1},#{2}
    </select>
 
</mapper>

实体类

package com.openlab.pojo;

public class User {
	
	private String id;
	private String username;
	private String password;
	private String roleId;
	private String iconURL;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRoleId() {
		return roleId;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	public String getIconURL() {
		return iconURL;
	}
	public void setIconURL(String iconURL) {
		this.iconURL = iconURL;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", roleId=" + roleId
				+ ", iconURL=" + iconURL + "]";
	}
	
	

}

映射接口

import java.util.List;

import com.openlab.pojo.User;

public interface testMapper {
	
	public  List<User>  Limitquery(String username,int start,int end);

}

测试

package com.openlab.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.openlab.mapper.testMapper;
import com.openlab.pojo.User;

public class Test {
	
	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
		
		
		InputStream is = Resources.getResourceAsStream(resource);
		
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		
		SqlSession session =factory.openSession();
	
		testMapper mapper = session.getMapper(testMapper.class);
		
		List<User> users = mapper.query("n", 0,3);
		
		for(User user : users){
			System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getId());
			System.out.println();
		}
	}
	

}


结果
在这里插入图片描述

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页