学⽣信息管理系统(附运⾏效果图和源码下载)分页技术(后台封装json数据传递到前端显⽰,动。。。
【项⽬需求】
1.查询数据库并将查询结果封装成标准json数据格式,传递到前端显⽰。
2.前端解析后台传递来的json数据并分页显⽰。
3.实现动态分页。
【涉及技术】
Mybatis    Ajax  jQuery    HTML5    BootStrap
【开发环境】
IDE:Eclipse J2ee2019-06          WebServer:Apache Tomcat 9.0.13        DB:MySQL8          OS:Windows 10
【开发过程】
1.数据库准备
(1)创建名为test的数据库,建⽴⼀张名为student的学⽣信息表,表设计如下:
2.Mybatis搭建后端持久层
(1)导⼊依赖包:
(2)创建持久化类:Student
package com.pojo;
public class Student {
private int stuNum;
private String stuName,stuClass,stuSex,academy,profession;
public Student() {}
public Student(int stuNum, String stuName, String stuClass, String stuSex, String academy, String profession) {  this.stuNum = stuNum;
this.stuName = stuName;
this.stuClass = stuClass;
this.stuSex = stuSex;
this.academy = academy;
this.profession = profession;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuClass() {
return stuClass;
}
public void setStuClass(String stuClass) {
this.stuClass = stuClass;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public String getAcademy() {
return academy;
}
public void setAcademy(String academy) {
this.academy = academy;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@Override
public String toString() {
public String toString() {
return "Student [stuNum=" + stuNum + ", stuName=" + stuName + ", stuClass=" + stuClass + ", stuSex=" + stuSex    + ", academy=" + academy + ", profession=" + profession + "]";
}
}
(3)创建接⼝:StudentMapper.java
batis;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.pojo.Student;
public interface StudentMapper {
//根据起始索引和查询⾏数查询
public List<Student> selectInPage(@Param("startindx")int start,@Param("mount")int mount) throws Exception;
}
(4)创建映射⽂件:l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="batis.StudentMapper">
<resultMap type="com.pojo.Student" id="stumap">
<id property="stuNum" column="stu_Num"/>
<result property="stuName" column="stu_Name"/>
<result property="stuClass" column="stu_Class"/>
<result property="stuSex" column="stu_Sex"/>
<result property="academy" column="academy"/>
<result property="profession" column="profession"/>
</resultMap>
<select id="selectInPage" resultMap="stumap">
select * from student limit #{startindx},#{mount}
</select>
</mapper>
(5)创建Mybatis核⼼配置⽂件:l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使⽤maybatis的⽇志控制 -->
<setting name="logImpl" value="LOG4J" />
<setting name="defaultExecutorType" value="BATCH" />
</settings>
<!-- 定义所有的数据库连接,并指定使⽤哪⼀个数据源 -->
<environments default="">
<environment id=""><!-- 定义数据源名称 -->
<!-- 使⽤JDBC的事务管理 -->
<transactionManager type="JDBC" />
<!-- 定义⼀个数据源,连接⽅式为数据库连接池⽅式 -->
<dataSource type="POOLED"><!-- 定义⼀个数据源,连接⽅式为数据库连接池⽅式 -->
<property name="driver" value="sql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=GMT%2B8" />    <property name="username" value="root" />
<property name="password" value="xlt123456" />
</dataSource>
</environment>
</environments>
<!-- 加载接⼝ -->
<mappers>
<mapper resource="com/l"/>
</mappers>
</configuration>
(6)创建⽇志配置⽂件:log4j.propertiesjquery源码在线
sole=org.apache.log4j.ConsoleAppender
sole.Target=System.out
sole.layout=org.apache.log4j.PatternLayout
sole.layout.ConversionPattern=[%-5p] [%d{yyyy-MM-dd HH:mm:ss}] [%t] %c %L  %m%Logger=DEBUG,console
(7)连通性测试。
创建⼀个测试类test.java,测试到⽬前为⽌,持久层是否能正常⼯作:
st;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
batis.StudentMapper;
import com.pojo.Student;
public class test {
private SqlSession session = null;
private SqlSessionFactory build;
private List<Student> stuList=new ArrayList<Student>();
@Before
public void init() {
InputStream is;
try {
is = ResourceAsStream("l");
build = new SqlSessionFactoryBuilder().build(is);
session = build.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
@After
public void destory() {
if (session != null) {
session.close();
}
}
@Test
public void teststupage() throws Exception {
StudentMapper Mapper(StudentMapper.class);
stuList=studao.selectInPage(0, 15);
for(Student stu : stuList) {
System.out.println(stu);
}
}
}
使⽤jUnit Test运⾏teststupage()⽅法,控制台打印出下列信息,说明持久层⼯作正常:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。