Hibernate全套增删改查+分页

1.创建一个web工程

2.导入jar包

Hibernate全套增删改查+分页

3.创建Student表

4.创建实体类

package com.entity;

public class Student {
private Integer sid;
private String sname;
private String password;
private String sex;
private Integer cid; public Student() {
}
public Student(Integer sid, String sname, String password, String sex,
Integer cid) {
this.sid = sid;
this.sname = sname;
this.password = password;
this.sex = sex;
this.cid = cid;
}
public Student(String sname, String password, String sex,
Integer cid) {
this.sname = sname;
this.password = password;
this.sex = sex;
this.cid = cid;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Override
public String toString() {
return "Student [cid=" + cid + ", password=" + password + ", sex="
+ sex + ", sid=" + sid + ", sname=" + sname + "]";
} }

5.配置映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--1.实体类的字段和数据库的字段一一映射
name:实体类的全名
table:对应的数据库表名,默认值为类名
-->
<class name="com.entity.Student" table="STUDENT"> <!--1.1主键配置
id:对应表中的主键
name:实体类中的属性名
type:主键属性的数据类型
column:属性名对应表的字段名,默认值为属性名-->
<id name="sid" type="java.lang.Integer" column="SID"> <!--generator:为持久化实例产生一个唯一标识-->
<generator class="sequence"> <!--数据库的序列名-->
<param name="sequence">SEQ_STUDENT</param>
</generator>
</id> <!--1.2普通字段配置-->
<property name="sname" type="java.lang.String" column="SNAME"/>
<property name="password" type="java.lang.String" column="PASSWORD"/>
<property name="sex" type="java.lang.String" column="SEX"/>
<property name="cid" type="java.lang.Integer" column="CID"/>
</class>
</hibernate-mapping>

6.配置主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<hibernate-configuration> <!--通常一个session-factory节点代表一个数据库-->
<session-factory>
<!--1.配置数据库的驱动-->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!--2.配置数据库的url地址-->
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <!--3,配置数据库的username-->
<property name="connection.username">holly</property> <!--4.配置数据库的密码-->
<property name="connection.password">sys</property> <!--5.配置数据库的方言-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!--6.显示sql语句-->
<property name="show_sql">true</property> <!--7.sql语句格式化-->
<property name="format_sql">true</property> <!--8.加载映射文件-->
<mapping resource="com/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

7.创建一个工具类

package com.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; /**
* Hibernate打开session和关闭session的工具包
* @author Holly
*
*/
public class Hibernate_Util {
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
private static Configuration configuration=null;
private static SessionFactory sessionFactory=null;
static{ try {
configuration=new Configuration().configure("/hibernate.cfg.xml");
sessionFactory=configuration.buildSessionFactory();
} catch (HibernateException e) {
System.out.println("解析xml和创建Session工厂error");
e.printStackTrace();
} } public static Session getSession(){
Session session=threadLocal.get();
if(session==null){
session=sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession(){
Session session=threadLocal.get();
if(session!=null){
threadLocal.set(null);
session.close();
}
} }

8.创建分页实体类

package com.page;

public class Page {
private Integer pagesize; //页面大小
private Integer pageno; //当前页
private Integer startrow; //起始行
private Integer totalpage; //总页数
private Integer totalcount; //总条数 public Page() {
}
public Page(Integer pageSize, Integer pageNo, Integer totalCount) {
this.pagesize = pageSize; //页面大小
this.pageno = pageNo; //当前页
this.totalcount = totalCount; //计算总条数
this.setStartrow(pageNo, pageSize); //计算起始行
this.setTotalpage(totalCount, pageSize); //计算总页数
}
public Integer getPagesize() {
return pagesize;
}
public void setPagesize(Integer pageSize) {
this.pagesize = pageSize;
}
public Integer getPageno() {
return pageno;
}
public void setPageno(Integer pageNo) {
this.pageno = pageNo;
}
public Integer getStartrow() {
return startrow;
}
/**
* 计算起始行
* @param pageNo
* @param pageSize
*/
public void setStartrow(Integer pageNo,Integer pageSize) {
this.startrow = (pageNo-1)*pageSize;
}
public Integer getTotalpage() {
return totalpage;
}
/**
* 计算总页数
* @param totalCount
* @param pageSize
*/
public void setTotalpage(Integer totalCount,Integer pageSize) {
this.totalpage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
}
public Integer getTotalcount() {
return totalcount;
}
public void setTotalCount(Integer totalCount) {
this.totalcount = totalCount;
}
@Override
public String toString() {
return "[{\"pageno\":" + pageno + ",\"pagesize\":" + pagesize
+ ",\"startrow\":" + startrow + ",\"totalcount\":" + totalcount
+ ",\"totalpage\":" + totalpage + "}]";
} }

9.创建Dao类

package com.dao;

import java.util.List;

import com.entity.Student;
import com.page.Page; public interface StudentDao {
/**
* 1.根据用户名和密码查询
* @param sname
* @param password
* @return
*/
Student findByNameAndPwd(String sname,String password);
/**
* 2.延时加载:查询所有
* @return
*/
List<Student> findAll();
/**
* 3.保存
* @param stu
* @return
*/
int save(Student stu);
/**
* 4.根据主键id查询单条
* @param sid
* @return
*/
Student findById(Integer sid);
/**
* 5.修改
*/
int update(Student stu);
/**
* 6.删除对象
* @param stu
* @return
*/
int delete(Student stu);
/**
* 7.分页查询
*/
List<Student> findPage(Page page);
/**
* 8.查询总条数
*/
int getTotalCount(); }

10.实现Dao接口

package com.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session; import com.dao.StudentDao;
import com.entity.Student;
import com.page.Page;
import com.util.Hibernate_Util; public class StudentDaoImpl implements StudentDao{
/**
* 1.查询所有:立即加载
*/
public List<Student> findAll() {
//1.获取session对象
Session session=Hibernate_Util.getSession();
//2.定义hql语句
String hql="from Student";
//3.Session创建Query对象
Query query=session.createQuery(hql);
//4.query对象执行操作
List<Student> it=query.list();
//5.释放资源
Hibernate_Util.closeSession();
return it;
}
/**
* 2.根据用户名和密码查询
*/
public Student findByNameAndPwd(String sname, String password) {
//1.获取session对象
Session session=Hibernate_Util.getSession();
//2.定义hql语句(命名参数)
String hql="from Student where sname=:sname and password=:password";
//3.Session创建Query对象
Query query=session.createQuery(hql);
//4.query对象给占位符赋值
/*query.setString("sname", sname);
query.setString("password", password);*/
query.setParameter("sname", sname);
query.setParameter("password", password); // query.setProperties(user);
//5.执行操作(uniqueResult()查询的单行数据)
Student stu=(Student) query.uniqueResult();
//6.释放资源
Hibernate_Util.closeSession();
return stu; } /**
* 根据id主键查询单条
*/
public Student findById(Integer sid) {
//1.获取session对象
Session session=Hibernate_Util.getSession();
//2.利用session根据主键查询
Student stu=(Student) session.get(Student.class, sid);
/*3.
* 因为在修改时要提前查询单条,和session关联已经变成持久化对象,
* 而在修改时session执行update方法时,
* 首先会查询该主键id是否存在,
* 如果存在,则根据主键id修改该对象,此时对象已经处于持久化对象,
* 而实际session中不能同时存在两个主键id相同的对象,
* 否则会出现如下错误:
* org.hibernate.NonUniqueObjectException:
* a different object with the same identifier
* value was already associated with the
* session: [com.entity.Student#1]
*/
Hibernate_Util.closeSession();
// session.clear();
return stu;
}
/**
* 删除对象
*/
public int delete(Student stu) {
int num=1;
//1.获取session对象
Session session=Hibernate_Util.getSession(); try {
//session删除操作
session.delete(stu);
session.beginTransaction().commit();
} catch (HibernateException e) {
num=0;
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//5.释放资源
Hibernate_Util.closeSession();
} return num;
}
/**
* 添加
*/
public int save(Student stu) {
int num=1;
//1.获取session对象
Session session=Hibernate_Util.getSession();
//执行操作
try {
session.save(stu);
session.beginTransaction().commit();
} catch (HibernateException e) {
num=0;
e.printStackTrace();
}finally{
//5.释放资源
Hibernate_Util.closeSession();
}
return num;
}
/**
* 修改
*/
public int update(Student stu) {
int num=1;
//1.获取session对象
Session session=Hibernate_Util.getSession();
try {
//执行操作
//该对象的主键id必须存在
session.update(stu);
session.beginTransaction().commit();
} catch (HibernateException e) {
num=0;
e.printStackTrace();
}finally{
//5.释放资源
Hibernate_Util.closeSession();
}
return num;
}
/**
* 7.分页查询
*/
public List<Student> findPage(Page page) {
//1.获取session
Session session=Hibernate_Util.getSession(); //2.定义查询最大记录数的hql
String hql="from Student"; //3.定义查询最大记录数的Query对象
Query querypage=session.createQuery(hql); //4.查询最大记录数的数据
querypage.setMaxResults(page.getPagesize()); //5.确定查询起点
querypage.setFirstResult(page.getStartrow()); //6.分页查询
List<Student> list=querypage.list(); //7.关闭session
Hibernate_Util.closeSession(); return list;
}
/**
* 8.查询总条数
*/
public int getTotalCount() {
//1.获取session
Session session=Hibernate_Util.getSession(); //2.定义查询总条数hql语句
String hqlcount="select count(*) from Student"; //3.利用Session创建Query对象
Query querycount=session.createQuery(hqlcount); //4.获取总条数(返回单行数据uniqueResult())
Integer totalCount=Integer.parseInt(querycount.uniqueResult().toString());
//5.释放资源
Hibernate_Util.closeSession();
return totalCount;
} }

11.创建业务层的接口

package com.service;

import java.util.List;

import com.entity.Student;
import com.page.Page; /**
* 业务接口
* @author pc
*
*/
public interface StudentService {
/**
* 1.登录的业务
* @param sname
* @param password
* @return
*/
Student login(String sname,String password);
/**
* 2.注册
* @param stu
* @return
*/
int register(Student stu); /**
* 3.查询所有
*/
List<Student> findAll();
/**
* 4.根据主键id查询
* @param sid
* @return
*/
Student findById(Integer sid);
/**
* 5.修改
*/
int update(Student stu);
/**
* 6.删除
*/
int delete(Student stu); /**
* 7.分页查询
*/
List<Student> findPage(Page page);
/**
* 8.查询总条数
*/
int getTotalCount(); }

12.实现业务层接口

package com.service.impl;

import java.util.List;

import com.dao.StudentDao;
import com.dao.impl.StudentDaoImpl;
import com.entity.Student;
import com.page.Page;
import com.service.StudentService; public class StudentServiceImpl implements StudentService {
/**
* 使用多态引用数据访问层对象
*/
private StudentDao dao = new StudentDaoImpl(); /**
* 登录
*/
public Student login(String sname, String password) {
return dao.findByNameAndPwd(sname, password);
} public int delete(Student stu) {
// TODO Auto-generated method stub
return dao.delete(stu);
} public List<Student> findAll() {
// TODO Auto-generated method stub
return dao.findAll();
} public Student findById(Integer sid) {
// TODO Auto-generated method stub
return dao.findById(sid);
} public int register(Student stu) {
// TODO Auto-generated method stub
return dao.save(stu);
} public int update(Student stu) {
// TODO Auto-generated method stub
return dao.update(stu);
} public List<Student> findPage(Page page) {
// TODO Auto-generated method stub
return dao.findPage(page);
} public int getTotalCount() {
// TODO Auto-generated method stub
return dao.getTotalCount();
} }

13.创建Servlet类

Servlet类里的增删改查我是分开写的,所以就不一一粘贴代码,这里只粘贴部分

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 2.接受参数
String sname = request.getParameter("sname");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
Integer cid = Integer.valueOf(request.getParameter("cid"));
Student stu = new Student(sname, password, sex, cid); // 3.业务处理
StudentService service = new StudentServiceImpl();
int num = service.register(stu);
if (num > 0) {
System.out.println("register success!");
response.sendRedirect("FindAllServlet");
} else {
System.out.println("register fail!"); } } }
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.codehaus.jackson.map.ObjectMapper; import com.entity.Student;
import com.page.Page;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; /**
* 分页查询
*
* @author pc
*
*/
public class FindPageServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
/**
* ☆
* 在使用ajax往前台通过输出流对象的print方法发送json时
* 该行获取输出流对象的代码必须放在
* response.setContentType("text/html;charset=UTF-8");
* 之后,否则ajax回调时,页面拿到的中文数据就永远都是乱码,
* 原因是:如果将改行代码写在处理客户端乱码之前,表示编码格式已经确定,
* 所以,编码格式的处理应该放在获取PrintWriter对象之前
*
*/
PrintWriter out=response.getWriter(); // 2.接受参数
String no=request.getParameter("pageNo"); // 3.业务处理
StudentService service = new StudentServiceImpl(); int pageSize=3; //页面大小
int pageNo=1; //默认的pageNo为1
if(no!=null && no!=""){
pageNo=Integer.valueOf(no);
}
//获取总条数
int totalCount=service.getTotalCount();
//封装分页所需字段
Page page=new Page(pageSize, pageNo, totalCount);
//分页查询
List<Student> list = service.findPage(page); ObjectMapper om=new ObjectMapper();
String str=om.writeValueAsString(list);
String pagestr=om.writeValueAsString(page);
str="{\"page\":"+pagestr+",\"list\":"+str+"}";
System.out.println(str); out.print(str);
/*if (list != null) {
System.out.println("find page success");
request.setAttribute("list", list);
request.setAttribute("page", page);
request.getRequestDispatcher("index.jsp").forward(request, response);
} else {
System.out.println("find page fail!"); }*/ } }
package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.page.Page;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; public class FindAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); /**
* ☆
* 在使用ajax往前台通过输出流对象的print方法发送json时
* 该行获取输出流对象的代码必须放在
* response.setContentType("text/html;charset=UTF-8");
* 之后,否则ajax回调时,页面拿到的中文数据就永远都是乱码,
* 原因是:如果将改行代码写在处理客户端乱码之前,表示编码格式已经确定,
* 所以,编码格式的处理应该放在获取PrintWriter对象之前
*
*/ // 2.接受参数
String no=request.getParameter("pageNo"); // 3.业务处理
StudentService service = new StudentServiceImpl(); int pageSize=3; //页面大小
int pageNo=1; //默认的pageNo为1
if(no!=null && no!=""){
pageNo=Integer.valueOf(no);
}
//获取总条数
int totalCount=service.getTotalCount();
Page page=new Page(pageSize, pageNo, totalCount); List<Student> list = service.findPage(page);
if(list!=null){
System.out.println("find All page success");
request.setAttribute("list", list);
request.setAttribute("page", page);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
System.out.println("find All page fail!");
}
} }

代码

package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); //2.接受参数
String sname=request.getParameter("sname");
String password=request.getParameter("password");
//3.业务处理
StudentService service=new StudentServiceImpl();
Student stu=service.login(sname, password);
if(stu!=null){
System.out.println("login success!");
//4.页面跳转
request.getSession().setAttribute("sname", sname);
response.sendRedirect("FindAllServlet");
}else{
System.out.println("login fail!"); }
} }
package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;
import com.util.Hibernate_Util; public class FindByIdSerlvet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 2.接受参数
Integer sid=Integer.valueOf(request.getParameter("sid")); // 3.业务处理
StudentService service = new StudentServiceImpl(); Student stu=service.findById(sid);
System.out.println("stu:"+stu);
if(stu!=null){
System.out.println("根据id查询到对象");
request.setAttribute("stu", stu);
request.getRequestDispatcher("update.jsp").forward(request, response);
}else{
System.out.println("没有根据id查询到对象");
}
}
}
package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 2.接受参数
Integer sid=Integer.valueOf(request.getParameter("sid"));
String sname = request.getParameter("sname");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
Integer cid=Integer.valueOf(request.getParameter("cid"));
Student stu=new Student(sid,sname, password, sex, cid); // 3.业务处理
StudentService service = new StudentServiceImpl();
int num=service.update(stu);
if(num>0){
System.out.println("update success!");
response.sendRedirect("FindAllServlet");
}else{
System.out.println("update fail!");
}
}
}
package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.entity.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl; public class DeleteSerlvet extends HttpServlet {
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 2.接受参数
Integer sid=Integer.valueOf(request.getParameter("sid"));
// 3.业务处理
StudentService service = new StudentServiceImpl();
Student stu=service.findById(sid);
if (stu != null) {
System.out.println("根据id查询成功");
int num=service.delete(stu);
if(num>0){
System.out.println("delete success!!");
// 4.页面跳转
response.sendRedirect("FindAllServlet"); }else{
System.out.println("delete success!!"); }
} else {
System.out.println("根据id查询失败!");
}
}
}

14.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FindAllServlet</servlet-name>
<servlet-class>com.servlet.FindAllServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FindByIdSerlvet</servlet-name>
<servlet-class>com.servlet.FindByIdSerlvet</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateServlet</servlet-name>
<servlet-class>com.servlet.UpdateServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteSerlvet</servlet-name>
<servlet-class>com.servlet.DeleteSerlvet</servlet-class>
</servlet>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FindPageServlet</servlet-name>
<servlet-class>com.servlet.FindPageServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FindAllServlet</servlet-name>
<url-pattern>/FindAllServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FindByIdSerlvet</servlet-name>
<url-pattern>/FindByIdSerlvet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateServlet</servlet-name>
<url-pattern>/UpdateServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteSerlvet</servlet-name>
<url-pattern>/DeleteSerlvet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FindPageServlet</servlet-name>
<url-pattern>/FindPageServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

15.首页

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<CENTER>
<h3>学生信息表</h3>
<table border="2">
<tr>
<td>
序号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td align="center">
操作
</td>
</tr> <c:forEach var="i" items="${list}" varStatus="f">
<tr <c:if test="${f.index%2==0}">style="background-color:orange;"</c:if>>
<td>
${i.sid }
</td>
<td>
${i.sname }
</td>
<td>
${i.sex }
</td>
<td>
<a href="register.jsp">添加</a>
&nbsp;&nbsp;
<a href="FindByIdSerlvet?sid=${i.sid}">修改</a>
&nbsp;&nbsp;
<a href="DeleteSerlvet?sid=${i.sid}">删除</a> </td>
</tr>
</c:forEach> </table>
<div> 第${page.pageno}/${page.totalpage}页 &nbsp;&nbsp;
<a href="FindAllServlet?pageNo=1">首页</a>
<c:choose>
<c:when test="${page.pageno gt 1}">
<a href="FindAllServlet?pageNo=${page.pageno-1 }">上一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是第一页了,没有上一页!');">上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.pageno lt page.totalpage}">
<a href="FindAllServlet?pageNo=${page.pageno+1 }">下一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是最有一页了,没有下一页!');">下一页</a>
</c:otherwise>
</c:choose> <a href="FindAllServlet?pageNo=${page.totalpage}">末页</a>
&nbsp;&nbsp;
共${page.totalcount}条 </div> </CENTER> </body>
</html>

16.登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center>
<fieldset style="width: 300px;">
<legend>
登录
</legend>
<form action="LoginServlet" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="sname" />
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="提交" />
</td>
<td>
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</fieldset>
</center>
</body>
</html>

17.注册页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center>
<fieldset style="width: 500px;">
<legend>
注册
</legend>
<form action="RegisterServlet" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="sname"/>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password"/>
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<input type="radio" name="sex" value="男" checked="checked"/>男
<input type="radio" name="sex" value="女"/>女
</td>
</tr>
<tr>
<td>
班级编号:
</td>
<td>
<select name="cid">
<option value="1">TB07</option>
<option value="2">TB13</option>
<option value="3">TB24</option>
<option value="4">TB31</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="注册" />
</td>
<td>
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</fieldset>
</center>
</body>
</html>

18.修改

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<center>
<fieldset style="width: 500px;">
<legend>
<br>修改
</legend>
<form action="UpdateServlet?sid=${stu.sid}" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="sname" value="${stu.sname}"/>
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="password" value="${stu.password}"/>
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<c:choose>
<c:when test="${stu.sex eq '男'}">
<input type="radio" name="sex" value="男" checked="checked"/>男
<input type="radio" name="sex" value="女"/>女
</c:when>
<c:otherwise>
<input type="radio" name="sex" value="男"/>男
<input type="radio" name="sex" value="女" checked="checked"/>女
</c:otherwise>
</c:choose>
</td>
</tr>
<tr>
<td>
班级编号:
</td>
<td>
<select name="cid">
<option value="1">TB07</option>
<option value="2">TB13</option>
<option value="3">TB24</option>
<option value="4">TB31</option>
</select>
</td>
</tr> <tr>
<td>
<input type="submit" value="修改" />
</td>
<td>
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</fieldset>
</center>
</body>
</html>

部分截图       2016-09-2415:21:19

注:希望各路大神多多指点!!!Hibernate全套增删改查+分页

上一篇:java--简单排序算法


下一篇:使用 Json.Net 对Json文本进行 增删改查