1.创建如下数据库脚本

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
  1 --创建用户信息表
  2 --编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址
  3 create table users
  4 (
  5   id number(10) primary key,
  6   username varchar2(20) not null,
  7   password varchar2(40) not null,
  8   age number(10) not null,
  9   sex number(10) not null,
 10   nickname varchar2(20) not null,
 11   mobile varchar2(15) not null,
 12   address varchar2(50) not null,
 13   supper number(10) not null,
 14   picpath varchar2(100) not null
 15 );
 16 
 17 --创建微博表
 18 --编号,内容,发布时间,用户编号
 19 create table blog
 20 (
 21   id number(10) primary key,
 22   content varchar2(1000) not null,
 23   publishtime date not null,
 24   userid number(10) references users(id)
 25 );
 26 
 27 --创建序列
 28 create sequence seq_users;
 29 create sequence seq_blog;
 30 
 31 --插入数据
 32 insert into users
 33 (
 34 id,username,password,age,sex,nickname,
 35 mobile,address ,supper,picpath
 36 ) 
 37 values
 38 (seq_users.nextval,'holly','123',18,0,'holly上神',
 39 '13451802404','雨花台铁心桥新河苑',0,'holly.jpg'); 
 40 
 41 insert into users
 42 (
 43 id,username,password,age,sex,nickname,
 44 mobile,address ,supper,picpath
 45 ) 
 46 values
 47 (seq_users.nextval,'倩倩','123',18,0,'倩倩上仙',
 48 '13451805648','大桥北路',1,'qianqian.jpg'); 
 49 
 50 insert into users
 51 (
 52 id,username,password,age,sex,nickname,
 53 mobile,address ,supper,picpath
 54 ) 
 55 values
 56 (seq_users.nextval,'死胖子','123',28,1,'死盘子小仙',
 57 '13451804869','湖北非洲',1,'sipangzi.jpg'); 
 58 
 59 insert into users
 60 (
 61 id,username,password,age,sex,nickname,
 62 mobile,address ,supper,picpath
 63 ) 
 64 values
 65 (seq_users.nextval,'肉肉','123',38,1,'肉肉小妖',
 66 '13451885697','新街口八条巷',1,'rourou.jpg'); 
 67 
 68 insert into users
 69 (
 70 id,username,password,age,sex,nickname,
 71 mobile,address ,supper,picpath
 72 ) 
 73 values
 74 (seq_users.nextval,'戴文','123',38,1,'文文上仙',
 75 '13451888569','东海瀛洲',1,'daiwen.jpg'); 
 76 
 77 commit;
 78 
 79 insert into blog
 80 (id,content,publishtime,userid) 
 81 values
 82 (
 83 seq_blog.nextval,'上神已经下凡渡劫失败...',
 84 to_date('2017-01-01','yyyy-MM-dd'),1
 85 );
 86 
 87 insert into blog
 88 (id,content,publishtime,userid) 
 89 values
 90 (
 91 seq_blog.nextval,'上神已经去东海瀛洲去营救...',
 92 to_date('2017-01-02','yyyy-MM-dd'),1
 93 );
 94 
 95 insert into blog
 96 (id,content,publishtime,userid) 
 97 values
 98 (
 99 seq_blog.nextval,'上神中午下凡去看团子...',
100 to_date('2017-01-03','yyyy-MM-dd'),1
101 );
102 
103 insert into blog
104 (id,content,publishtime,userid) 
105 values
106 (
107 seq_blog.nextval,'上神中午已去十里桃源...',
108 to_date('2017-01-04','yyyy-MM-dd'),1
109 );
110 
111 insert into blog
112 (id,content,publishtime,userid) 
113 values
114 (
115 seq_blog.nextval,'小仙已经去了诛仙台...',
116 to_date('2017-01-05','yyyy-MM-dd'),2
117 );
118 commit;
119 
120 select * from users u,blog b
121 where u.id=b.userid;
blog.sql

2.创建如下项目结构

15.Hibernate一对多双向关联映射+分页_xml_03

15.Hibernate一对多双向关联映射+分页_xml_04

3.在src下的com.pojo包下创建Users.java 类

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
  1 package com.pojo;
  2 
  3 import java.io.Serializable;
  4 import java.util.HashSet;
  5 import java.util.Set;
  6 
  7 /**
  8  * 一方(一对多)
  9  * 一方:引入多方集合
 10  *@author 北大青鸟南京中博 Holly老师
 11  *
 12  */
 13 public class Users implements Serializable{
 14     /**
 15      * 
 16      */
 17     private static final long serialVersionUID = 1L;
 18     private Integer id; //用户编号
 19     private String username; //用户名
 20     private String password; //用户密码
 21     private Integer age; //年龄
 22     private Integer sex; //性别
 23     private String nickname; //昵称
 24     private String mobile; //手机
 25     private String address; //地址
 26     private Integer supper; //是否是管理员
 27     private String picpath;  //头像名称
 28     /*hibernte一对多*/
 29     private Set<Blog> blogset=new HashSet<Blog>(); 
 30     
 31     public Users() {
 32     }
 33     
 34     public Users(String username, String password, Integer age, Integer sex,
 35             String nickname, String mobile, String address, Integer supper,
 36             String picpath) {
 37         this.username = username;
 38         this.password = password;
 39         this.age = age;
 40         this.sex = sex;
 41         this.nickname = nickname;
 42         this.mobile = mobile;
 43         this.address = address;
 44         this.supper = supper;
 45         this.picpath = picpath;
 46     }
 47 
 48     public Users(Integer id, String username, String password, Integer age,
 49             Integer sex, String nickname, String mobile, String address,
 50             Integer supper, String picpath) {
 51         this.id = id;
 52         this.username = username;
 53         this.password = password;
 54         this.age = age;
 55         this.sex = sex;
 56         this.nickname = nickname;
 57         this.mobile = mobile;
 58         this.address = address;
 59         this.supper = supper;
 60         this.picpath = picpath;
 61     }
 62     
 63     
 64 
 65     public Users(Integer id, String username, String password, Integer age,
 66             Integer sex, String nickname, String mobile, String address,
 67             Integer supper, String picpath, Set<Blog> blogset) {
 68         super();
 69         this.id = id;
 70         this.username = username;
 71         this.password = password;
 72         this.age = age;
 73         this.sex = sex;
 74         this.nickname = nickname;
 75         this.mobile = mobile;
 76         this.address = address;
 77         this.supper = supper;
 78         this.picpath = picpath;
 79         this.blogset = blogset;
 80     }
 81 
 82     public Integer getId() {
 83         return id;
 84     }
 85     public void setId(Integer id) {
 86         this.id = id;
 87     }
 88     public String getUsername() {
 89         return username;
 90     }
 91     public void setUsername(String username) {
 92         this.username = username;
 93     }
 94     public String getPassword() {
 95         return password;
 96     }
 97     public void setPassword(String password) {
 98         this.password = password;
 99     }
100     public Integer getAge() {
101         return age;
102     }
103     public void setAge(Integer age) {
104         this.age = age;
105     }
106     public Integer getSex() {
107         return sex;
108     }
109     public void setSex(Integer sex) {
110         this.sex = sex;
111     }
112     public String getNickname() {
113         return nickname;
114     }
115     public void setNickname(String nickname) {
116         this.nickname = nickname;
117     }
118     public String getMobile() {
119         return mobile;
120     }
121     public void setMobile(String mobile) {
122         this.mobile = mobile;
123     }
124     public String getAddress() {
125         return address;
126     }
127     public void setAddress(String address) {
128         this.address = address;
129     }
130     public Integer getSupper() {
131         return supper;
132     }
133     public void setSupper(Integer supper) {
134         this.supper = supper;
135     }
136     public String getPicpath() {
137         return picpath;
138     }
139     public void setPicpath(String picpath) {
140         this.picpath = picpath;
141     }
142 
143     
144 
145     public Set<Blog> getBlogset() {
146         return blogset;
147     }
148 
149     public void setBlogset(Set<Blog> blogset) {
150         this.blogset = blogset;
151     }
152 
153     @Override
154     public String toString() {
155         return "Users [address=" + address + ", age=" + age + ", blogset="
156                 + blogset + ", id=" + id + ", mobile=" + mobile + ", nickname="
157                 + nickname + ", password=" + password + ", picpath=" + picpath
158                 + ", sex=" + sex + ", supper=" + supper + ", username="
159                 + username + "]";
160     }
161 
162 
163 }
Users.java

4.在src下的com.pojo包下创建Blog.java类

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.pojo;
 2 
 3 import java.io.Serializable;
 4 import java.util.Date;
 5 
 6 /**
 7  * 多方:多对一
 8  * 多方:配置对象
 9  * @author 北大青鸟南京中博 Holly老师
10  *
11  */
12 public class Blog implements Serializable{
13     /**
14      * 
15      */
16     private static final long serialVersionUID = 1L;
17     private Integer id;
18     private String content;
19     private Date publishtime;
20     
21     /*多方外建列:引入一方对象*/
22     private Users users;
23     
24     public Blog() {
25     }
26 
27     public Blog(String content, Date publishtime, Users users) {
28         this.content = content;
29         this.publishtime = publishtime;
30         this.users = users;
31     }
32 
33     public Blog(Integer id, String content, Date publishtime, Users users) {
34         this.id = id;
35         this.content = content;
36         this.publishtime = publishtime;
37         this.users = users;
38     }
39 
40     public Integer getId() {
41         return id;
42     }
43 
44     public void setId(Integer id) {
45         this.id = id;
46     }
47 
48     public String getContent() {
49         return content;
50     }
51 
52     public void setContent(String content) {
53         this.content = content;
54     }
55 
56     public Date getPublishtime() {
57         return publishtime;
58     }
59 
60     public void setPublishtime(Date publishtime) {
61         this.publishtime = publishtime;
62     }
63 
64     public Users getUsers() {
65         return users;
66     }
67 
68     public void setUsers(Users users) {
69         this.users = users;
70     }
71 
72     @Override
73     public String toString() {
74         return "Blog [content=" + content + ", id=" + id + ", publishtime="
75                 + publishtime  + "]";
76     }
77     
78 
79 }
Blog.java

5.在src下的com.pojo包下创建Users.hbm.xml文件

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
 3 <hibernate-mapping>
 4    <class name="com.pojo.Users" table="USERS">
 5      <!-- 1.主键列 -->
 6      <id name="id" type="java.lang.Integer" column="ID">
 7         <generator class="sequence">
 8           <param name="sequence">seq_users</param>
 9         </generator>
10      </id>
11      
12      <!-- 2.普通字段配置 -->
13      <property name="username" type="java.lang.String" column="USERNAME"/>
14      <property name="password" type="java.lang.String" column="PASSWORD"/>
15      <property name="sex" type="java.lang.Integer" column="SEX"/>
16      <property name="age" type="java.lang.Integer" column="AGE"/>
17      <property name="nickname" type="java.lang.String" column="NICKNAME"/>
18      <property name="mobile" type="java.lang.String" column="MOBILE"/>
19      <property name="address" type="java.lang.String" column="ADDRESS"/>
20      <property name="supper" type="java.lang.Integer" column="SUPPER"/>
21      <property name="picpath" type="java.lang.String" column="PICPATH"/>
22     
23     <!-- 3.一对多: -->
24     <!-- 
25        name:一方引入对方set集合的对象名称
26        column:是多方的的外键列名
27        class:是一方引入对象集合的对象类型全路径
28      -->
29     <set name="blogset">
30        <key column="userid"/>
31        <one-to-many class="com.pojo.Blog"/>
32     </set>
33    </class>
34 </hibernate-mapping>
Users.hbm.xml

6.在src下的com.pojo包下创建Blog.hbm.xml文件

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
 3 <hibernate-mapping>
 4  <class name="com.pojo.Blog" table="BLOG">
 5     <!-- 1.主键配置 -->
 6     <id name="id" column="ID" type="java.lang.Integer">
 7       <!-- 主键是序列 -->
 8       <generator class="sequence">
 9         <param name="sequence">seq_blog</param>
10       </generator>
11     </id>
12     <!-- 2.普通字段配置 -->
13     <property name="content" column="CONTENT" type="java.lang.String"/>
14     <property name="publishtime" column="PUBLISHTIME" type="java.util.Date"/>
15    
16     <!-- 3.多对一配置 -->
17     <!-- 
18       name:是Blog类引入一方类对象名,
19       column:是blog表中引入一方的外键列列名
20       class:是Blog类引入一方对象的属性名的类全路径
21      -->
22     <many-to-one name="users" class="com.pojo.Users" column="USERID" />
23  </class>
24 </hibernate-mapping>
Blog.hbm.xml

7.在src下创建hibernate.cfg.xml主配置文件

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
 3 <hibernate-configuration>
 4   <session-factory>
 5    <!-- 1.数据库驱动 -->
 6     <property name="connection.driver_class">
 7        oracle.jdbc.driver.OracleDriver
 8     </property>
 9     <!-- 2.数据库url -->
10     <property name="connection.url">
11        jdbc:oracle:thin:@127.0.0.1:1521:orcl
12     </property>
13     <!-- 3.数据库的用户名 -->
14     <property name="connection.username">holly</property>
15     <!-- 4.数据库密码 -->
16     <property name="connection.password">sys</property>
17     <!-- 5.数据库方言 -->
18     <property name="dialect">
19      org.hibernate.dialect.Oracle10gDialect
20     </property>
21     <!-- 6.显示sql -->
22     <property name="show_sql">true</property>
23     <!-- 7.sql格式化 -->
24     <property name="format_sql">true</property>
25     
26     <!-- 8.注册xml文件 -->
27     <mapping resource="com/pojo/Blog.hbm.xml"/>
28     <mapping resource="com/pojo/Users.hbm.xml"/>
29   </session-factory>
30 </hibernate-configuration>
hibernate.cfg.xml

8.在src下的com.util包下创建HibernateUtil.java 获取Session的工具类

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.util;
 2 
 3 import org.hibernate.HibernateException;
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.cfg.Configuration;
 7 
 8 /**
 9  * hiberante获取Session的工具类
10  * @author 北大青鸟南京中博 Holly老师
11  *
12  */
13 public class HibernateUtil {
14     //1.创建线程池(存放session对象)
15     private static  ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
16     //2.定义Configuration对象解析主配置文件
17     private static Configuration configuration=null;
18     //3.定义SessionFactory工厂对象
19     private static SessionFactory sessionFactory=null;
20     //4.static中解析xml获取SessionFactory
21     static{
22         try {
23             configuration=new Configuration().configure("hibernate.cfg.xml");
24             sessionFactory=configuration.buildSessionFactory();
25         } catch (HibernateException e) {
26             System.out.println("sessionFactory 解析hibernate.cfg.xml error");
27             e.printStackTrace();
28         }
29     }
30     //5.获取session对象
31     public static Session getSession(){
32         Session session=threadLocal.get();
33         if(session==null){
34             session=sessionFactory.openSession();
35             threadLocal.set(session);
36         }
37         return session;
38     }
39     //6.关闭session对象
40     public static void closeSession(){
41         Session session=threadLocal.get();
42         threadLocal.set(null);
43         session.close();
44     } 
45     
46     
47 
48 }
HibernateUtil.java

9.在src下的com.dao包下创建UsersDao.java接口

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.pojo.Users;
 6 /**
 7  *  用户数据访问层接口
 8 @author 北大青鸟南京中博 Holly老师
 9  *
10  */
11 public interface UsersDao {
12     /**
13      * 一方只需要提供下拉列表的集合即可
14      * @param pageno
15      * @param pagesize
16      * @param id
17      * @return
18      */
19     List<Users> findAllBlog();
20 
21     
22 
23 }
UsersDao.java

10.在src下的com.dao包下创建BlogDao.java接口

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.pojo.Blog;
 6 /**
 7  *  博客数据访问层接口
 8 @author 北大青鸟南京中博 Holly老师
 9  *
10  */
11 public interface BlogDao {
12     /**
13      * 分页查询
14      * @param pageno
15      * @param pagesize
16      * @param userid
17      * @return
18      */
19     List<Blog> findAllBlogPage(Integer pageno,Integer pagesize,Integer userid);
20     /**
21      * 统计数据条数
22      * @param userid
23      * @return
24      */
25     Integer getTotalCount(Integer userid);
26 }
BlogDao.java

11.在src下的com.dao.impl包下创建UsersDaoImpl.java

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.dao.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Query;
 6 import org.hibernate.Session;
 7 
 8 import com.dao.UsersDao;
 9 import com.pojo.Users;
10 import com.util.HibernateUtil;
11 /**
12  * 用户数据访问层实现类
13 @author 北大青鸟南京中博 Holly老师
14  *
15  */
16 public class UsersDaoImpl implements UsersDao {
17     /**
18      * 一方只需要提供下拉列表的集合即可
19      * @return
20      */
21     public List<Users> findAllBlog() {
22         Session session=HibernateUtil.getSession();
23         Query query=session.createQuery("from Users");
24         //查询所有
25         
26         return query.list();
27     }
28    
29 
30 }
UsersDaoImpl.java

12.在src下的com.dao.impl包下创建BlogDaoImpl.java

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.dao.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Query;
 6 import org.hibernate.Session;
 7 
 8 import com.pojo.Blog;
 9 import com.util.HibernateUtil;
10 /**
11  *  博客数据访问层实现类
12 @author 北大青鸟南京中博 Holly老师
13  *
14  */
15 public class BlogDaoImpl implements com.dao.BlogDao {
16     /**
17      * 分页查询
18      * @param pageno
19      * @param pagesize
20      * @return
21      */
22     public List<Blog> findAllBlogPage(Integer pageno, Integer pagesize,
23             Integer userid) {
24         Session session=HibernateUtil.getSession();
25         Query query=null;
26         
27         if(userid!=0){
28             //条件分页
29             query=session.createQuery("from Blog where userid=:userid");
30             query.setParameter("userid",userid);
31         }else{
32             //普通分页
33             query=session.createQuery("from Blog");
34             
35         }
36         //1.设置页面大小
37         query.setMaxResults(pagesize);
38         
39         //2.设置分页起始行
40         query.setFirstResult((pageno-1)*pagesize);
41         
42         //3.分页查询
43         return query.list();
44         
45     }
46     /**
47      * 统计数据条数
48      * @param userid
49      * @return
50      */ 
51     public Integer getTotalCount(Integer userid) {
52         Session session=HibernateUtil.getSession();
53         Query query=null;
54         if(userid!=0){
55             //统计某个用户下的信息
56             query=session.createQuery("select count(*) from Blog where userid=:userid");
57             query.setParameter("userid",userid);
58         }else{
59             //统计所有用户下的信息
60             query=session.createQuery("select count(*) from Blog");
61         }
62         
63         //查询总条数
64         String strcount=query.uniqueResult().toString();
65         Integer totalcount=Integer.valueOf(strcount);
66         return totalcount;
67     }
68     
69 
70 
71 }
BlogDaoImpl.java

13.在src下的com.servlet包下创建BlogServlet.java控制类

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 import com.dao.BlogDao;
14 import com.dao.UsersDao;
15 import com.dao.impl.BlogDaoImpl;
16 import com.dao.impl.UsersDaoImpl;
17 import com.pojo.Blog;
18 import com.pojo.Users;
19 /**
20  *  博客控制层类
21 @author 北大青鸟南京中博 Holly老师
22  *
23  */
24 public class BlogServlet extends HttpServlet {
25     public void doGet(HttpServletRequest request, HttpServletResponse response)
26             throws ServletException, IOException {
27       this.doPost(request, response);
28     }
29 
30     public void doPost(HttpServletRequest request, HttpServletResponse response)
31             throws ServletException, IOException {
32         request.setCharacterEncoding("UTF-8");
33         response.setCharacterEncoding("UTF-8");
34         response.setContentType("text/html");
35         
36         String flag=request.getParameter("flag");
37         BlogDao blogdao=new BlogDaoImpl();
38         UsersDao usersdao=new UsersDaoImpl();
39         //1.查询
40         if(flag==null){
41             
42             System.out.println("findAll");
43             
44             
45             //分页
46             String no=request.getParameter("pageno");
47             Integer pagesize=3;
48             Integer pageno=1;
49             if(no!=null && no!=""){
50                 pageno=Integer.valueOf(no);
51             }
52             
53             //条件分页
54             String id=request.getParameter("userid");
55             System.out.println("userid:"+id);
56             Integer userid=0;
57             
58             //一方集合
59             
60             if(id!=null){
61                 userid=Integer.valueOf(id);
62             }
63             
64             //某个用户下的微博信息条数
65             Integer totalcount=blogdao.getTotalCount(userid);
66             
67             Integer totalpage=totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1;
68             
69             //某个用户下的所有微博信息和所有用户关联的微博信息
70             //微博的条件查询和查询所有
71             List<Blog> bloglist=blogdao.findAllBlogPage(pageno, pagesize, userid);
72             
73             //查询所有的用户集合:目的用于下拉列表(查询所有)
74             List<Users> userslist=usersdao.findAllBlog();
75             
76             if(userslist!=null){
77                 System.out.println("findall  success...");
78                 request.setAttribute("bloglist", bloglist);
79                 request.setAttribute("pageno", pageno);
80                 request.setAttribute("totalcount", totalcount);
81                 request.setAttribute("totalpage", totalpage);
82                 request.setAttribute("userid", userid);
83                 
84                 HttpSession session=request.getSession();
85                 session.setAttribute("userslist", userslist);
86                 request.getRequestDispatcher("index.jsp").forward(request, response);
87                 
88             }else{
89                 System.out.println("findall bloglist error...");
90                 
91             }
92             
93         }
94         
95     }
96 
97 }
BlogServlet.java

14.在WebRoot下的WEB-INF下的web.xml配置servlet

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <servlet>
 8     <servlet-name>BlogServlet</servlet-name>
 9     <servlet-class>com.servlet.BlogServlet</servlet-class>
10   </servlet>
11 
12   <servlet-mapping>
13     <servlet-name>BlogServlet</servlet-name>
14     <url-pattern>/BlogServlet</url-pattern>
15   </servlet-mapping>
16   <welcome-file-list>
17     <welcome-file>BlogServlet</welcome-file>
18   </welcome-file-list>
19 </web-app>
web.xml

15.在WebRoot下创建index.jsp页面

15.Hibernate一对多双向关联映射+分页_北大青鸟15.Hibernate一对多双向关联映射+分页_Hibernate_02
  1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  3 <%
  4 String path = request.getContextPath();
  5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6 %>
  7 
  8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9 <html>
 10   <head>
 11     <base href="<%=basePath%>">
 12     
 13     <title>My JSP 'index.jsp' starting page</title>
 14     <meta http-equiv="pragma" content="no-cache">
 15     <meta http-equiv="cache-control" content="no-cache">
 16     <meta http-equiv="expires" content="0">    
 17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 18     <meta http-equiv="description" content="This is my page">
 19     <!--
 20     <link rel="stylesheet" type="text/css" href="styles.css">
 21     -->
 22   </head>
 23   
 24   <body>
 25     <CENTER>
 26       <fieldset>
 27         <legend style="font-size:36px;font-weight:bold;">&lt;&lt;三生三世十里桃花&gt;&gt;管理系统</legend>
 28         <h3><a href="add.jsp">加入仙班</a></h3>
 29         <form action="BlogServlet" method="post">
 30                昵称:
 31                <select name="userid">
 32                     <option value="0" selected="selected">--请选择--</option>
 33                  <c:forEach var="i" items="${userslist}">
 34                    <c:choose>
 35                      <c:when test="${userid eq i.id}">
 36                          <option value="${i.id }" selected="selected">${i.nickname }</option>
 37                      </c:when>
 38                      <c:otherwise>
 39                          <option value="${i.id }">${i.nickname }</option>
 40                      </c:otherwise>
 41                    </c:choose>
 42                  </c:forEach>
 43            </select>
 44            <input type="submit" value="搜"/>
 45         </form>
 46         <table border="1">
 47            <tr>
 48              <td>用户头像</td>
 49              <td>微博内容</td>
 50              <td>发布时间</td>
 51              <td>用户昵称</td>
 52              <td>用户性别</td>
 53              <td>联系方式</td>
 54              <td>管理员?</td>
 55              <td>操作</td>
 56            </tr>
 57            <c:forEach var="i" items="${bloglist}">
 58                <tr>
 59                   <td><img src="${basePath}image/${i.users.picpath}" alt="${i.users.picpath}"/></td>
 60                   <td>${i.content}</td>
 61                   <td>${i.publishtime}</td>
 62                   <td>${i.users.nickname}</td>
 63                   <c:choose>
 64                     <c:when test="${i.users.sex eq 0}">
 65                       <td></td>
 66                     </c:when>
 67                     <c:otherwise>
 68                         <td></td>
 69                     </c:otherwise>
 70                   </c:choose>
 71                   <td>${i.users.mobile }</td>
 72                   <c:choose>
 73                     <c:when test="${i.users.supper eq 0}">
 74                       <td></td>
 75                     </c:when>
 76                     <c:otherwise>
 77                         <td></td>
 78                     </c:otherwise>
 79                   </c:choose>
 80                   <td>
 81                     <a href="BlogServlet?flag=findbyid&id=${i.id}">修改</a>
 82                     &nbsp;|&nbsp;
 83                     <a href="BlogServlet?flag=delete&id=${i.id}">删除</a>
 84                   </td>
 85                </tr>
 86            </c:forEach>
 87         </table>
 88         第${pageno}/${totalpage}页
 89         &nbsp;&nbsp;
 90         <a href="BlogServlet?pageno=1&userid=${userid}">首页</a>
 91         &nbsp;&nbsp;
 92         <c:choose>
 93           <c:when test="${pageno>1}">
 94             <a href="BlogServlet?pageno=${pageno-1}&userid=${userid}">上一页</a>
 95           </c:when>
 96           <c:otherwise>
 97             <a href="javascript:alert('已经是第一页!');">上一页</a>
 98           </c:otherwise>
 99         </c:choose>
100         
101         &nbsp;&nbsp;
102         <c:choose>
103           <c:when test="${pageno<totalpage}">
104            <a href="BlogServlet?pageno=${pageno+1}&userid=${userid}">下一页</a>
105           </c:when>
106           <c:otherwise>
107             <a href="javascript:alert('已经是最后一页!');">下一页</a>
108           </c:otherwise>
109         </c:choose>
110         &nbsp;&nbsp;
111         <a href="BlogServlet?pageno=${totalpage}&userid=${userid}">末页</a>
112         &nbsp;&nbsp;
113         共${totalcount }条
114       </fieldset>
115     </CENTER>
116   </body>
117 </html>
index.jsp

16.运行页面

15.Hibernate一对多双向关联映射+分页_北大青鸟_31

 

17. 点击下一页

15.Hibernate一对多双向关联映射+分页_北大青鸟_32

18.在昵称下拉列表选择

15.Hibernate一对多双向关联映射+分页_北大青鸟_33

19.点击下一页

15.Hibernate一对多双向关联映射+分页_北大青鸟_34

 

 -------------------------------------------------------------------------------------------------

以上代码纯属原创 ,为了能够共同进步互相学习,如有问题或更好建议可以联系holly老师:

QQ/微信:964918306

想学习java,安卓,大数据,数据库,web开发,前端开发 可以联系Holly老师!