1.创建如下数据库脚本
--创建用户信息表
--编号,用户名,密码,年龄,性别,昵称,手机,地址,管理员,图像地址
create table users
(
id number(10) primary key,
username varchar2(20) not null,
password varchar2(40) not null,
age number(10) not null,
sex number(10) not null,
nickname varchar2(20) not null,
mobile varchar2(15) not null,
address varchar2(50) not null,
supper number(10) not null,
picpath varchar2(100) not null
); --创建微博表
--编号,内容,发布时间,用户编号
create table blog
(
id number(10) primary key,
content varchar2(1000) not null,
publishtime date not null,
userid number(10) references users(id)
); --创建序列
create sequence seq_users;
create sequence seq_blog; --插入数据
insert into users
(
id,username,password,age,sex,nickname,
mobile,address ,supper,picpath
)
values
(seq_users.nextval,'holly','',18,0,'holly上神',
'','雨花台铁心桥新河苑',0,'holly.jpg'); insert into users
(
id,username,password,age,sex,nickname,
mobile,address ,supper,picpath
)
values
(seq_users.nextval,'倩倩','',18,0,'倩倩上仙',
'','大桥北路',1,'qianqian.jpg'); insert into users
(
id,username,password,age,sex,nickname,
mobile,address ,supper,picpath
)
values
(seq_users.nextval,'死胖子','',28,1,'死盘子小仙',
'','湖北非洲',1,'sipangzi.jpg'); insert into users
(
id,username,password,age,sex,nickname,
mobile,address ,supper,picpath
)
values
(seq_users.nextval,'肉肉','',38,1,'肉肉小妖',
'','新街口八条巷',1,'rourou.jpg'); insert into users
(
id,username,password,age,sex,nickname,
mobile,address ,supper,picpath
)
values
(seq_users.nextval,'戴文','',38,1,'文文上仙',
'','东海瀛洲',1,'daiwen.jpg'); commit; insert into blog
(id,content,publishtime,userid)
values
(
seq_blog.nextval,'上神已经下凡渡劫失败...',
to_date('2017-01-01','yyyy-MM-dd'),1
); insert into blog
(id,content,publishtime,userid)
values
(
seq_blog.nextval,'上神已经去东海瀛洲去营救...',
to_date('2017-01-02','yyyy-MM-dd'),1
); insert into blog
(id,content,publishtime,userid)
values
(
seq_blog.nextval,'上神中午下凡去看团子...',
to_date('2017-01-03','yyyy-MM-dd'),1
); insert into blog
(id,content,publishtime,userid)
values
(
seq_blog.nextval,'上神中午已去十里桃源...',
to_date('2017-01-04','yyyy-MM-dd'),1
); insert into blog
(id,content,publishtime,userid)
values
(
seq_blog.nextval,'小仙已经去了诛仙台...',
to_date('2017-01-05','yyyy-MM-dd'),2
);
commit; select * from users u,blog b
where u.id=b.userid;
blog.sql
2.创建如下项目结构
3.在src下的com.pojo包下创建Users.java 类
package com.pojo; import java.io.Serializable;
import java.util.HashSet;
import java.util.Set; /**
* 一方(一对多)
* 一方:引入多方集合
*@author 北大青鸟南京中博 Holly老师
*
*/
public class Users implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id; //用户编号
private String username; //用户名
private String password; //用户密码
private Integer age; //年龄
private Integer sex; //性别
private String nickname; //昵称
private String mobile; //手机
private String address; //地址
private Integer supper; //是否是管理员
private String picpath; //头像名称
/*hibernte一对多*/
private Set<Blog> blogset=new HashSet<Blog>(); public Users() {
} public Users(String username, String password, Integer age, Integer sex,
String nickname, String mobile, String address, Integer supper,
String picpath) {
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
this.nickname = nickname;
this.mobile = mobile;
this.address = address;
this.supper = supper;
this.picpath = picpath;
} public Users(Integer id, String username, String password, Integer age,
Integer sex, String nickname, String mobile, String address,
Integer supper, String picpath) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
this.nickname = nickname;
this.mobile = mobile;
this.address = address;
this.supper = supper;
this.picpath = picpath;
} public Users(Integer id, String username, String password, Integer age,
Integer sex, String nickname, String mobile, String address,
Integer supper, String picpath, Set<Blog> blogset) {
super();
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.sex = sex;
this.nickname = nickname;
this.mobile = mobile;
this.address = address;
this.supper = supper;
this.picpath = picpath;
this.blogset = blogset;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getSupper() {
return supper;
}
public void setSupper(Integer supper) {
this.supper = supper;
}
public String getPicpath() {
return picpath;
}
public void setPicpath(String picpath) {
this.picpath = picpath;
} public Set<Blog> getBlogset() {
return blogset;
} public void setBlogset(Set<Blog> blogset) {
this.blogset = blogset;
} @Override
public String toString() {
return "Users [address=" + address + ", age=" + age + ", blogset="
+ blogset + ", id=" + id + ", mobile=" + mobile + ", nickname="
+ nickname + ", password=" + password + ", picpath=" + picpath
+ ", sex=" + sex + ", supper=" + supper + ", username="
+ username + "]";
} }
Users.java
4.在src下的com.pojo包下创建Blog.java类
package com.pojo; import java.io.Serializable;
import java.util.Date; /**
* 多方:多对一
* 多方:配置对象
* @author 北大青鸟南京中博 Holly老师
*
*/
public class Blog implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String content;
private Date publishtime; /*多方外建列:引入一方对象*/
private Users users; public Blog() {
} public Blog(String content, Date publishtime, Users users) {
this.content = content;
this.publishtime = publishtime;
this.users = users;
} public Blog(Integer id, String content, Date publishtime, Users users) {
this.id = id;
this.content = content;
this.publishtime = publishtime;
this.users = users;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public Date getPublishtime() {
return publishtime;
} public void setPublishtime(Date publishtime) {
this.publishtime = publishtime;
} public Users getUsers() {
return users;
} public void setUsers(Users users) {
this.users = users;
} @Override
public String toString() {
return "Blog [content=" + content + ", id=" + id + ", publishtime="
+ publishtime + "]";
} }
Blog.java
5.在src下的com.pojo包下创建Users.hbm.xml文件
<?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>
<class name="com.pojo.Users" table="USERS">
<!-- 1.主键列 -->
<id name="id" type="java.lang.Integer" column="ID">
<generator class="sequence">
<param name="sequence">seq_users</param>
</generator>
</id> <!-- 2.普通字段配置 -->
<property name="username" type="java.lang.String" column="USERNAME"/>
<property name="password" type="java.lang.String" column="PASSWORD"/>
<property name="sex" type="java.lang.Integer" column="SEX"/>
<property name="age" type="java.lang.Integer" column="AGE"/>
<property name="nickname" type="java.lang.String" column="NICKNAME"/>
<property name="mobile" type="java.lang.String" column="MOBILE"/>
<property name="address" type="java.lang.String" column="ADDRESS"/>
<property name="supper" type="java.lang.Integer" column="SUPPER"/>
<property name="picpath" type="java.lang.String" column="PICPATH"/> <!-- 3.一对多: -->
<!--
name:一方引入对方set集合的对象名称
column:是多方的的外键列名
class:是一方引入对象集合的对象类型全路径
-->
<set name="blogset">
<key column="userid"/>
<one-to-many class="com.pojo.Blog"/>
</set>
</class>
</hibernate-mapping>
Users.hbm.xml
6.在src下的com.pojo包下创建Blog.hbm.xml文件
<?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>
<class name="com.pojo.Blog" table="BLOG">
<!-- 1.主键配置 -->
<id name="id" column="ID" type="java.lang.Integer">
<!-- 主键是序列 -->
<generator class="sequence">
<param name="sequence">seq_blog</param>
</generator>
</id>
<!-- 2.普通字段配置 -->
<property name="content" column="CONTENT" type="java.lang.String"/>
<property name="publishtime" column="PUBLISHTIME" type="java.util.Date"/> <!-- 3.多对一配置 -->
<!--
name:是Blog类引入一方类对象名,
column:是blog表中引入一方的外键列列名
class:是Blog类引入一方对象的属性名的类全路径
-->
<many-to-one name="users" class="com.pojo.Users" column="USERID" />
</class>
</hibernate-mapping>
Blog.hbm.xml
7.在src下创建hibernate.cfg.xml主配置文件
<?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>
<!-- 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.数据库的用户名 -->
<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.注册xml文件 -->
<mapping resource="com/pojo/Blog.hbm.xml"/>
<mapping resource="com/pojo/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml
8.在src下的com.util包下创建HibernateUtil.java 获取Session的工具类
package com.util; import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; /**
* hiberante获取Session的工具类
* @author 北大青鸟南京中博 Holly老师
*
*/
public class HibernateUtil {
//1.创建线程池(存放session对象)
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
//2.定义Configuration对象解析主配置文件
private static Configuration configuration=null;
//3.定义SessionFactory工厂对象
private static SessionFactory sessionFactory=null;
//4.static中解析xml获取SessionFactory
static{
try {
configuration=new Configuration().configure("hibernate.cfg.xml");
sessionFactory=configuration.buildSessionFactory();
} catch (HibernateException e) {
System.out.println("sessionFactory 解析hibernate.cfg.xml error");
e.printStackTrace();
}
}
//5.获取session对象
public static Session getSession(){
Session session=threadLocal.get();
if(session==null){
session=sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
//6.关闭session对象
public static void closeSession(){
Session session=threadLocal.get();
threadLocal.set(null);
session.close();
} }
HibernateUtil.java
9.在src下的com.dao包下创建UsersDao.java接口
package com.dao; import java.util.List; import com.pojo.Users;
/**
* 用户数据访问层接口
@author 北大青鸟南京中博 Holly老师
*
*/
public interface UsersDao {
/**
* 一方只需要提供下拉列表的集合即可
* @param pageno
* @param pagesize
* @param id
* @return
*/
List<Users> findAllBlog(); }
UsersDao.java
10.在src下的com.dao包下创建BlogDao.java接口
package com.dao; import java.util.List; import com.pojo.Blog;
/**
* 博客数据访问层接口
@author 北大青鸟南京中博 Holly老师
*
*/
public interface BlogDao {
/**
* 分页查询
* @param pageno
* @param pagesize
* @param userid
* @return
*/
List<Blog> findAllBlogPage(Integer pageno,Integer pagesize,Integer userid);
/**
* 统计数据条数
* @param userid
* @return
*/
Integer getTotalCount(Integer userid);
}
BlogDao.java
11.在src下的com.dao.impl包下创建UsersDaoImpl.java
package com.dao.impl; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session; import com.dao.UsersDao;
import com.pojo.Users;
import com.util.HibernateUtil;
/**
* 用户数据访问层实现类
@author 北大青鸟南京中博 Holly老师
*
*/
public class UsersDaoImpl implements UsersDao {
/**
* 一方只需要提供下拉列表的集合即可
* @return
*/
public List<Users> findAllBlog() {
Session session=HibernateUtil.getSession();
Query query=session.createQuery("from Users");
//查询所有 return query.list();
} }
UsersDaoImpl.java
12.在src下的com.dao.impl包下创建BlogDaoImpl.java
package com.dao.impl; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session; import com.pojo.Blog;
import com.util.HibernateUtil;
/**
* 博客数据访问层实现类
@author 北大青鸟南京中博 Holly老师
*
*/
public class BlogDaoImpl implements com.dao.BlogDao {
/**
* 分页查询
* @param pageno
* @param pagesize
* @return
*/
public List<Blog> findAllBlogPage(Integer pageno, Integer pagesize,
Integer userid) {
Session session=HibernateUtil.getSession();
Query query=null; if(userid!=0){
//条件分页
query=session.createQuery("from Blog where userid=:userid");
query.setParameter("userid",userid);
}else{
//普通分页
query=session.createQuery("from Blog"); }
//1.设置页面大小
query.setMaxResults(pagesize); //2.设置分页起始行
query.setFirstResult((pageno-1)*pagesize); //3.分页查询
return query.list(); }
/**
* 统计数据条数
* @param userid
* @return
*/
public Integer getTotalCount(Integer userid) {
Session session=HibernateUtil.getSession();
Query query=null;
if(userid!=0){
//统计某个用户下的信息
query=session.createQuery("select count(*) from Blog where userid=:userid");
query.setParameter("userid",userid);
}else{
//统计所有用户下的信息
query=session.createQuery("select count(*) from Blog");
} //查询总条数
String strcount=query.uniqueResult().toString();
Integer totalcount=Integer.valueOf(strcount);
return totalcount;
} }
BlogDaoImpl.java
13.在src下的com.servlet包下创建BlogServlet.java控制类
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 javax.servlet.http.HttpSession; import com.dao.BlogDao;
import com.dao.UsersDao;
import com.dao.impl.BlogDaoImpl;
import com.dao.impl.UsersDaoImpl;
import com.pojo.Blog;
import com.pojo.Users;
/**
* 博客控制层类
@author 北大青鸟南京中博 Holly老师
*
*/
public class BlogServlet extends HttpServlet {
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"); String flag=request.getParameter("flag");
BlogDao blogdao=new BlogDaoImpl();
UsersDao usersdao=new UsersDaoImpl();
//1.查询
if(flag==null){ System.out.println("findAll"); //分页
String no=request.getParameter("pageno");
Integer pagesize=3;
Integer pageno=1;
if(no!=null && no!=""){
pageno=Integer.valueOf(no);
} //条件分页
String id=request.getParameter("userid");
System.out.println("userid:"+id);
Integer userid=0; //一方集合 if(id!=null){
userid=Integer.valueOf(id);
} //某个用户下的微博信息条数
Integer totalcount=blogdao.getTotalCount(userid); Integer totalpage=totalcount%pagesize==0?totalcount/pagesize:totalcount/pagesize+1; //某个用户下的所有微博信息和所有用户关联的微博信息
//微博的条件查询和查询所有
List<Blog> bloglist=blogdao.findAllBlogPage(pageno, pagesize, userid); //查询所有的用户集合:目的用于下拉列表(查询所有)
List<Users> userslist=usersdao.findAllBlog(); if(userslist!=null){
System.out.println("findall success...");
request.setAttribute("bloglist", bloglist);
request.setAttribute("pageno", pageno);
request.setAttribute("totalcount", totalcount);
request.setAttribute("totalpage", totalpage);
request.setAttribute("userid", userid); HttpSession session=request.getSession();
session.setAttribute("userslist", userslist);
request.getRequestDispatcher("index.jsp").forward(request, response); }else{
System.out.println("findall bloglist error..."); } } } }
BlogServlet.java
14.在WebRoot下的WEB-INF下的web.xml配置servlet
<?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>BlogServlet</servlet-name>
<servlet-class>com.servlet.BlogServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>BlogServlet</servlet-name>
<url-pattern>/BlogServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>BlogServlet</welcome-file>
</welcome-file-list>
</web-app>
web.xml
15.在WebRoot下创建index.jsp页面
<%@ 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>
<legend style="font-size:36px;font-weight:bold;"><<三生三世十里桃花>>管理系统</legend>
<h3><a href="add.jsp">加入仙班</a></h3>
<form action="BlogServlet" method="post">
昵称:
<select name="userid">
<option value="0" selected="selected">--请选择--</option>
<c:forEach var="i" items="${userslist}">
<c:choose>
<c:when test="${userid eq i.id}">
<option value="${i.id }" selected="selected">${i.nickname }</option>
</c:when>
<c:otherwise>
<option value="${i.id }">${i.nickname }</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
<input type="submit" value="搜"/>
</form>
<table border="1">
<tr>
<td>用户头像</td>
<td>微博内容</td>
<td>发布时间</td>
<td>用户昵称</td>
<td>用户性别</td>
<td>联系方式</td>
<td>管理员?</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${bloglist}">
<tr>
<td><img src="${basePath}image/${i.users.picpath}" alt="${i.users.picpath}"/></td>
<td>${i.content}</td>
<td>${i.publishtime}</td>
<td>${i.users.nickname}</td>
<c:choose>
<c:when test="${i.users.sex eq 0}">
<td>女</td>
</c:when>
<c:otherwise>
<td>男</td>
</c:otherwise>
</c:choose>
<td>${i.users.mobile }</td>
<c:choose>
<c:when test="${i.users.supper eq 0}">
<td>是</td>
</c:when>
<c:otherwise>
<td>否</td>
</c:otherwise>
</c:choose>
<td>
<a href="BlogServlet?flag=findbyid&id=${i.id}">修改</a>
|
<a href="BlogServlet?flag=delete&id=${i.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
第${pageno}/${totalpage}页
<a href="BlogServlet?pageno=1&userid=${userid}">首页</a>
<c:choose>
<c:when test="${pageno>1}">
<a href="BlogServlet?pageno=${pageno-1}&userid=${userid}">上一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是第一页!');">上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${pageno<totalpage}">
<a href="BlogServlet?pageno=${pageno+1}&userid=${userid}">下一页</a>
</c:when>
<c:otherwise>
<a href="javascript:alert('已经是最后一页!');">下一页</a>
</c:otherwise>
</c:choose>
<a href="BlogServlet?pageno=${totalpage}&userid=${userid}">末页</a>
共${totalcount }条
</fieldset>
</CENTER>
</body>
</html>
index.jsp
16.运行页面
17. 点击下一页
18.在昵称下拉列表选择
19.点击下一页
-------------------------------------------------------------------------------------------------
以上代码纯属原创 ,为了能够共同进步互相学习,如有问题或更好建议可以联系holly老师:
QQ/微信:964918306
想学习java,安卓,大数据,数据库,web开发,前端开发 可以联系Holly老师!