main.class
System.out.println("分页展示员工信息如下:");
Map<String, Integer> map = new HashMap<String,Integer>();
int a=5;
int b=10;
map.put("beginIndex",a);
map.put("pageSize",b);
fu_tn.FindBypage(map);
break;
Service
List<EmployeeUser> FindBypage(Map<String,Integer> map);
Service_impl
@Override
public List<EmployeeUser> FindBypage(Map<String,Integer> map) {
return fun.FindBypage(map);
}
Dao
List<EmployeeUser> FindBypage(Map<String, Integer> map);
Dao_impl
@Override
public List<EmployeeUser> FindBypage(Map<String, Integer> map) {
SqlSession sqlSession = Mybatis.openSession();
MapperEmployeeDao userMapper = sqlSession.getMapper(MapperEmployeeDao.class);
List<EmployeeUser> users = userMapper.FindBypage(map);
for (EmployeeUser user : users) {
System.out.println(user);
}
return users;
}
实体Bean
public class EmployeeUser {
int id ;
String name ;
String sex;
int telephone;
String post;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setTelephone(int telephone) {
this.telephone = telephone;
}
public int getTelephone() {
return telephone;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
}
Mybatis工具类
package Utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class Mybatis {
static SqlSessionFactory factory;
static {
InputStream inputStream = null;
String resources="mybatis-config.xml";
try{ inputStream = Resources.getResourceAsStream(resources);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace(); } }
public static SqlSession openSession(){
return factory.openSession();
}
}
Mapper.xml
<select id="FindBypage" parameterType="map" resultType="pojo.EmployeeUser">
-- 分页查询
select * from employees limit #{beginIndex},#{pageSize};
</select>
db.propertis
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&\
characterEncoding=utf8&useUnicode=true&useSSL=false
mysql.username=账号
mysql.password=密码
mybatais-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 环境配置-->
<!-- 加载类路径下的属性文件-->
<properties resource="db.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/employee.xml"/> <!--绑定映射⽂件-->
</mappers>
</configuration>
实现效果
