该文章仅为示例:
引入Jar包:
pagehelper-5.1.2.jar:https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/
jsqlparser-1.0.jar:http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/0.9.5/
注意:请看清楚版本号!!!若无法下载,留下邮箱,有时间会发给你!
数据库(Mysql):
create database userdb default character set utf8;
use userdb;
create table DeptInfo
(
deptId integer primary key auto_increment,
deptName varchar(20),
remark varchar(500)
);
create table EmpInfo
(
empId integer primary key auto_increment,
deptId integer,
name varchar(20),
sex varchar(10),
age integer,
beginTime date,
address varchar(500),
foreign key(deptId) references DeptInfo(deptId)
);
insert into deptinfo values(1,'研发部','IT牛人');
insert into deptinfo values(2,'设计部','妹子');
insert into empinfo values(1,2,'小白','女',20,SYSDATE(),'重庆');
insert into empinfo values(2,1,'小黑','男',18,SYSDATE(),'北京');
insert into empinfo values(3,1,'小明','女',21,SYSDATE(),'重庆');
insert into empinfo values(4,2,'小强','男',22,SYSDATE(),'北京');
insert into empinfo values(5,1,'张三','男',19,SYSDATE(),'北京');
**创建项目并构建实体类:
DeptInfo.JAVA
package com.huanzhen.entity;
import java.io.Serializable;
import java.util.Set;
public class DeptInfo implements Serializable{
private int deptId;
private String deptName;
private String remark;
private Set<EmpInfo> eInfo;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public DeptInfo(int deptId, String deptName, String remark) {
super();
this.deptId = deptId;
this.deptName = deptName;
this.remark = remark;
}
public DeptInfo() {
super();
}
public DeptInfo(String deptName, String remark) {
super();
this.deptName = deptName;
this.remark = remark;
}
public Set<EmpInfo> geteInfo() {
return eInfo;
}
public void seteInfo(Set<EmpInfo> eInfo) {
this.eInfo = eInfo;
}
}
EmpInfo.JAVA
package com.huanzhen.entity;
import java.io.Serializable;
import java.util.Date;
public class EmpInfo implements Serializable{
private int empId;
private DeptInfo dept;
private String name;
private String sex;
private int age;
private Date beginTime;
private String address;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public DeptInfo getDept() {
return dept;
}
public void setDept(DeptInfo dept) {
this.dept = dept;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public EmpInfo(int empId, DeptInfo dept, String name, String sex, int age,
Date beginTime, String address) {
super();
this.empId = empId;
this.dept = dept;
this.name = name;
this.sex = sex;
this.age = age;
this.beginTime = beginTime;
this.address = address;
}
public EmpInfo(DeptInfo dept, String name, String sex, int age,
Date beginTime, String address) {
super();
this.dept = dept;
this.name = name;
this.sex = sex;
this.age = age;
this.beginTime = beginTime;
this.address = address;
}
public EmpInfo() {
super();
}
}
实体类构建完成,下面进行MyBatis-config.xml的配置
MyBatis-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>
<!-- 为com.huanzhen.entity.Userinfo取别名为Userinfo -->
<typeAliases>
<typeAlias alias="Userinfo" type="com.huanzhen.entity.Userinfo"/>
<typeAlias alias="EmpInfo" type="com.huanzhen.entity.EmpInfo"/>
<typeAlias alias="DeptInfo" type="com.huanzhen.entity.DeptInfo"/>
</typeAliases>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
</plugin>
</plugins>
<!-- 配置环境,default指定默认使用哪个环境 -->
<environments default="development">
<!-- 配置具体的环境,id作为该环境的唯一标识 -->
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/userdb?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 关联映射文件 -->
<mappers>
<!-- 关联用于配置sql的资源文件 -->
<mapper resource="com/huanzhen/mapper/EmpInfoMapper.xml"/>
<mapper resource="com/huanzhen/mapper/DeptInfoMapper.xml"/>
</mappers>
</configuration>
DeptInfoMapper.xml:
<?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为dao接口类 -->
<mapper namespace="com.huanzhen.dao.IDeptInfoDao">
<resultMap type="DeptInfo" id="deptMap">
<id property="deptId" column="deptId"/>
<result property="deptName" column="deptName"/>
<result property="remark" column="remark"/>
<collection property="eInfo" ofType="EmpInfo" >
<id property="empId" column="empId"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="beginTime" column="beginTime"/>
<result property="address" column="address"/>
</collection>
</resultMap>
<select id="queryDeptById" resultMap="deptMap" parameterType="int">
select * FROM DeptInfo di inner join EmpInfo ei on di.deptId=ei.deptId where di.deptId=#{id}
</select>
</mapper>
EmpInfoMapper.xml:
<?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为dao接口类 -->
<mapper namespace="com.huanzhen.dao.IEmpInfoDao">
<!-- 配置返回值类型 -->
<resultMap type="EmpInfo" id="resEmp">
<id property="empId" column="empId"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="beginTime" column="beginTime"/>
<result property="address" column="address"/>
<!-- 配置外键表 -->
<association property="dept" javaType="DeptInfo">
<id property="deptId" column="deptId"/>
<result property="deptName" column="deptName"/>
<result property="remark" column="remark"/>
</association>
</resultMap>
<select id="queryEmpPage" resultMap="resEmp">
select * FROM EmpInfo order by empId desc
</select>
</mapper>
创建Dao接口:
IEmpInfoDao.JAVA
package com.huanzhen.dao;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.huanzhen.entity.EmpInfo;
public interface IEmpInfoDao {
//分页方法
public List<EmpInfo> queryEmpPage();
}
创建一个测试类:test.JAVA
test.JAVA
package com.huanzhen.main;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
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.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huanzhen.dao.IEmpInfoDao;
import com.huanzhen.entity.EmpInfo;
public class test {
public static void main(String[] args) throws IOException {
//通过MyBatis-config.xml获取到Reader对象
Reader reader = Resources.getResourceAsReader("MyBatis-config.xml");
//调用SqlSessionFactoryBuilder的build方法,传入之前获得的Reader对象,得到SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
//通过SqlSessionFactory对象调用openSession方法获得SqlSession对象
SqlSession session = factory.openSession();
//通过SqlSession对象的getMapper方法,传入接口类的反射对象,得到该接口类的对象
IEmpInfoDao iDao = session.getMapper(IEmpInfoDao.class);
//startPage(pageNum, pageSize, count) pageNum:页数,pageSize:每页数据条数,count:是否开启计算总数据条数
Page page = PageHelper.startPage(2, 3, true);
List<EmpInfo> dept = iDao.queryEmpPage();
for(EmpInfo emp:dept)
{
System.out.println(emp.getName());
}
session.close();
}
}
运行得到:
张三
小强
小明