您的位置:首页 > 编程语言 > Java开发

Mybatis实现简单的分页,以及基本的查询修改功能

2018-01-19 18:57 621 查看
1. 项目结构



2. 数据库



3. 数据库配置文件jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/zhq?characterEncoding\=utf-8
username=root
password=root4. 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>
<properties resource="config/jdbc.properties" />
<typeAliases>
<package name="com.zhq.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.zhq.dao"/>
</mappers>
</configuration>
5. com.zhq.entity
package com.zhq.entity;

public class MembersInfo {
private int mid;
private String mname;
private String mgender;
private int mage;
private String madderess;
private String memail;
public int getMid() {
return mid;
}
public void setMid(int mid) {
this.mid = mid;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getMgender() {
return mgender;
}
public void setMgender(String mgender) {
this.mgender = mgender;
}
public int getMage() {
return mage;
}
public void setMage(int mage) {
this.mage = mage;
}
public String getMadderess() {
return madderess;
}
public void setMadderess(String madderess) {
this.madderess = madderess;
}
public String getMemail() {
return memail;
}
public void setMemail(String memail) {
this.memail = memail;
}

}
6. com.zhq.dao
package com.zhq.dao;

import java.util.List;

import com.zhq.entity.MembersInfo;

public interface MembersInfoDao {
public List<MembersInfo> find(int num,int size);
public MembersInfo findById(int id);
public int update(MembersInfo membersInfo);
public int findCount();

}
<?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.zhq.dao.MembersInfoDao">
<select id="find"  resultType="MembersInfo" parameterType="int">
select * from membersInfo limit #{0},#{1}
</select>
<select id="findById" resultType="MembersInfo" parameterType="int">
select * from membersInfo where  mid=#{Id}
</select>
<update id="update" parameterType="MembersInfo">
update membersInfo set mname=#{mname},mgender=#{mgender},mage=#{mage},madderess=#{madderess},memail=#{memail}  where mid=#{mid}
</update>
<select id="findCount" parameterType="int" resultType="int">
select count(*) from membersInfo
</select>
</mapper>
7. com.zhq.biz

package com.zhq.biz;

import com.zhq.entity.MembersInfo;
import com.zhq.tools.PageUtil;

public interface MembersInfoService {
public PageUtil<MembersInfo> find(int pageIndex,int size);
public MembersInfo findById(int id);
public int update(MembersInfo membersInfo);
}
package com.zhq.biz;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zhq.dao.MembersInfoDao;
import com.zhq.entity.MembersInfo;
import com.zhq.tools.PageUtil;
import com.zhq.tools.SQLSessionFactory;

public class MembersInfoServiceImpl implements MembersInfoService {

@Override
public PageUtil<MembersInfo> find(int pageIndex, int size) {
PageUtil<MembersInfo> pageUtil=new PageUtil<MembersInfo>();
List<MembersInfo> list=null;
SqlSession sqlSession=null;
//总记录数
int count=0;
try {
sqlSession= SQLSessionFactory.getSession();
MembersInfoDao dao=sqlSession.getMapper(MembersInfoDao.class);
count=dao.findCount();
int num=(pageIndex-1)*size;
list =dao.find(num,size);
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
//封装分页所需要的数据
pageUtil.setPageIndex(pageIndex);
pageUtil.setList(list);
pageUtil.setCount(count);
pageUtil.setPageSize(size);
pageUtil.setPageNumber((int)Math.ceil((double)count/size));
return pageUtil;
}

@Override
public MembersInfo findById(int id) {
MembersInfo membersInfo=new MembersInfo();
SqlSession sqlSession=null;
try {
sqlSession= SQLSessionFactory.getSession();
MembersInfoDao dao=sqlSession.getMapper(MembersInfoDao.class);
membersInfo =dao.findById(id);
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
return membersInfo;
}

@Override
public int update(MembersInfo membersInfo) {
SqlSession sqlSession=null;
try {
sqlSession= SQLSessionFactory.getSession();
MembersInfoDao dao=sqlSession.getMapper(MembersInfoDao.class);
return dao.update(membersInfo);
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
if(sqlSession!=null) {
sqlSession.commit();
sqlSession.close();
}
}
return 0;
}

}
8. com.zhq.web
package com.zhq.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhq.biz.MembersInfoService;
import com.zhq.biz.MembersInfoServiceImpl;
import com.zhq.entity.MembersInfo;
import com.zhq.tools.PageUtil;

/**
* Servlet implementation class FindServlet
*/
@WebServlet("/FindServlet")
public class FindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FindServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pageIndex=request.getParameter("pageIndex");
int index=1;
if( pageIndex!=null) {
index=Integer.valueOf(pageIndex);
}
MembersInfoService ser=new MembersInfoServiceImpl();
PageUtil<MembersInfo> pageUtil=ser.find(index, 2);
request.getSession().setAttribute("pageUtil", pageUtil);
request.getRequestDispatcher("/findList.jsp").forward(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
package com.zhq.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhq.biz.MembersInfoService;
import com.zhq.biz.MembersInfoServiceImpl;
import com.zhq.entity.MembersInfo;

/**
* Servlet implementation class FindByIdServlet
*/
@WebServlet("/FindByIdServlet")
public class FindByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public FindByIdServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mid=request.getParameter("mid");
if(mid!=null) {
MembersInfoService ser=new MembersInfoServiceImpl();
MembersInfo membersInfo=ser.findById(Integer.valueOf(mid));
request.getSession().setAttribute("membersInfo", membersInfo);
}
request.getRequestDispatcher("/findListInfo.jsp").forward(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
package com.zhq.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhq.biz.MembersInfoService;
import com.zhq.biz.MembersInfoServiceImpl;
import com.zhq.entity.MembersInfo;

/**
* Servlet implementation class UpdateServlet
*/
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public UpdateServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String mid=request.getParameter("mid");
String mname=request.getParameter("mname");
System.out.println("mid"+mid);
System.out.println("mname"+mname);
String mgender=request.getParameter("mgender");
System.out.println("mgender"+mgender);
String mage=request.getParameter("mage");
String madderess=request.getParameter("madderess");
String memail=request.getParameter("memail");
MembersInfo membersInfo=new MembersInfo();
if(mid!=null&&mname!=null&&mgender!=null&&mage!=null&&madderess!=null&&madderess!=null&&memail!=null) {
membersInfo.setMid(Integer.valueOf(mid));
membersInfo.setMname(mname);
membersInfo.setMage(Integer.valueOf(mage));
membersInfo.setMadderess(madderess);
membersInfo.setMemail(memail);
membersInfo.setMgender(mgender);
MembersInfoService ser=new MembersInfoServiceImpl();
ser.update(membersInfo);
}
request.getRequestDispatcher("/FindServlet").forward(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
9. com.zhq.tools
package com.zhq.tools;

import java.util.List;

/**
* 这是一个分页工具类
* T表示一个泛型
* */
public class PageUtil<T> {
//当前页索引
private int pageIndex;
//每页数量多少
private int pageSize;
//总记录数
private int count;
//总页数
private int pageNumber;
//要分页的内容
public List<T> list;
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}

}
package com.zhq.tools;

import java.io.IOException;
import java.io.Reader;

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

public class SQLSessionFactory {
static Reader reader = null;
static SqlSessionFactory sqlSessionFactory=null;
private  SQLSessionFactory() {}
//这是一个静态语句块,用于读取主配置文件,因为主配置文件只需要读取一次。
static {
try {
//形成输入流
reader = Resources.getResourceAsReader("config/mybatis-config.xml");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
//SqlSession工厂,只需要一个,所以做成单例模式
public static SqlSessionFactory  getSqlSessionFactory(){
if(sqlSessionFactory == null){
synchronized (SQLSessionFactory.class) {
if(sqlSessionFactory == null){
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
}
}
return sqlSessionFactory;
}
public static SqlSession getSession(){
//返回一个数据操作连接对象
return getSqlSessionFactory().openSession();
}
}

10.页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/FindServlet">俱乐部会员信息列表</a><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>会员信息详情</title>
</head>
<body>
<h2>俱乐部会员信息</h2>
<table border="1">
<tr>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>家庭住址</td>
<td>Email</td>
</tr>
<c:forEach items="${pageUtil.list}" var="membersInfo">
<tr>
<td><a href="${pageContext.request.contextPath}/FindByIdServlet?mid=${membersInfo.mid}">${membersInfo.mid}</a></td>
<td>${membersInfo.mname}</td>
<td>${membersInfo.mgender}</td>
<td>${membersInfo.mage}</td>
<td>${membersInfo.madderess}</td>
<td>${membersInfo.memail}</td>
</tr>
</c:forEach>
</table>
<a href="${pageContext.request.contextPath}/FindServlet?pageIndex=${pageUtil.pageIndex>1?pageUtil.pageIndex-1:1}">上一页</a>
<a href="${pageContext.request.contextPath}/FindServlet?pageIndex=${pageUtil.pageIndex<pageUtil.pageNumber?pageUtil.pageIndex+1:pageUtil.pageNumber}">下一页</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>会员信息</title>
</head>
<body>
<h3>会员信息</h3>
<form action="${pageContext.request.contextPath}/UpdateServlet" method="post">
<table border="1">
<tr>
<td>姓名</td>
<td><input value="${membersInfo.mname}" name="mname"></td>
</tr>
<tr>
<td>性别</td>
<td>
<select name="mgender">
<c:choose>
<c:when test="${membersInfo.mgender eq '男'}">
<option value="${membersInfo.mgender}" selected>${membersInfo.mgender}</option>
<option value="女">女</option>
</c:when>
<c:otherwise>
<option value="${membersInfo.mgender}" selected>${membersInfo.mgender}</option>
<option value="男">男</option>
</c:otherwise>
</c:choose>
</select>
</td>
</tr>
<tr>
<td>年龄</td>
<td><input value="${membersInfo.mage}" name="mage"></td>
</tr>
<tr>
<td>家庭住址</td>
<td><input value="${membersInfo.madderess}" name="madderess"></td>
</tr>
<tr>
<td>Email</td>
<td><input value="${membersInfo.memail}" name="memail"></td>
</tr>
</table>
<input type="hidden" value="${membersInfo.mid}" name="mid">
<input type="submit" value="修改">
<input type="reset" value="重置">
</form>
</body>
</html>
11. 最终效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mybatis javaEE
相关文章推荐