网站简介
一个博客系统,能够注册用户,发表博文,浏览博文,评论博文,查看好友博文,上传照片等
网站技术
后台:servlet+jsp+spring+jpa
前台:jquery
数据库:mysql
网站具体设计
网站开发环境搭建
网站所需jar包:
MVC各个包的对应情况
搭建JPA开发环境
在MATA—INF文件夹下放置persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="syj123"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
搭建Spring与JPA的开发环境
主要在是beans.xml中实现JPA常用基础类的加载
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="cn.itcast"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="itcast"/>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
数据库
采用jpa设计数据库:
各个数据表的设计
master(用户)
package cn.itcast.bean.db;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Master {
private int QICQ;
private String name;
private String password;
private String sex;
private String email;
private Date regiestdate;
private Set<Friend> friendset=new HashSet<Friend>();
private Set<Photo> photoset=new HashSet<Photo>();
private Set<Artical> articalset=new HashSet<Artical>();
private Set<Word> wordset=new HashSet<Word>();
@OneToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy="master")
public Set<Word> getWordset() {
return wordset;
}
public void setWordset(Set<Word> wordset) {
this.wordset = wordset;
}
@OneToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy="master")
public Set<Artical> getArticalset() {
return articalset;
}
public void setArticalset(Set<Artical> articalset) {
this.articalset = articalset;
}
@Column(length=50)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToMany(cascade=CascadeType.REFRESH,mappedBy="master",fetch=FetchType.LAZY)
public Set<Photo> getPhotoset() {
return photoset;
}
public void setPhotoset(Set<Photo> photoset) {
this.photoset = photoset;
}
@Id @GeneratedValue
public int getQICQ() {
return QICQ;
}
public void setQICQ(int qICQ) {
QICQ = qICQ;
}
public void setName(String name) {
this.name = name;
}
@Column(length=20,nullable=false)
public String getName() {
return name;
}
@Column(length=20,nullable=false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(length=5,nullable=false)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Temporal(TemporalType.DATE)
public Date getRegiestdate() {
return regiestdate;
}
public void setRegiestdate(Date regiestdate) {
this.regiestdate = regiestdate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (QICQ ^ (QICQ >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Master other = (Master) obj;
if (QICQ != other.QICQ)
return false;
return true;
}
@OneToMany(cascade=(CascadeType.REFRESH),mappedBy="master",fetch=FetchType.LAZY)
public Set<Friend> getFriendset() {
return friendset;
}
public void setFriendset(Set<Friend> friendset) {
this.friendset = friendset;
}
public void addFriend(Friend friend)
{
friend.setMaster(this);
this.friendset.add(friend);
}
public void removefriend(Friend friend)
{
if(this.friendset.contains(friend))
{
this.friendset.remove(friend);
}
}
}
Feriend(用户好友表)
package cn.itcast.bean.db;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Friend {
private int id;
private String name;
private String sex;
private String QICQ;
private Master master;
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=5)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getQICQ() {
return QICQ;
}
public void setQICQ(String qICQ) {
QICQ = qICQ;
}
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="master_QICQ")
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
}
Artical(文章表)
package cn.itcast.bean.db;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Artical {
private int Id;
private String title;//标题
private ArticalType articalType;//文章类型
private String content;//正文内容
private Date sdTime;//写文章时间
private String info;//注释
private String comeform;
private int count;//阅读此时
private Set<Word> wordset=new HashSet<Word>();
private Master master;
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="master_id")
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
@OneToMany(cascade=CascadeType.REFRESH,mappedBy="artical",fetch=FetchType.LAZY)
public Set<Word> getWordset() {
return wordset;
}
public void setWordset(Set<Word> wordset) {
this.wordset = wordset;
}
@Lob
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Temporal(TemporalType.DATE)
public Date getSdTime() {
return sdTime;
}
public void setSdTime(Date sdTime) {
this.sdTime = sdTime;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@ManyToOne
@JoinColumn(name="articalType_id")
public ArticalType getArticalType() {
return articalType;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Artical other = (Artical) obj;
if (Id != other.Id)
return false;
return true;
}
public void setArticalType(ArticalType articalType) {
this.articalType = articalType;
}
@Id @GeneratedValue
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@Column(length=50)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(length=20)
public String getComeform() {
return comeform;
}
public void setComeform(String comeform) {
this.comeform = comeform;
}
}
ArticalType(文章类型表)
package cn.itcast.bean.db;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class ArticalType {
private int Id;
private String name;
private String info;
private Set<Artical> articalset=new HashSet<Artical>();
@OneToMany(cascade=(CascadeType.REFRESH),mappedBy="articalType",fetch=FetchType.LAZY)
public Set<Artical> getArtical() {
return articalset;
}
public void setArtical(Set<Artical> articalset) {
this.articalset = articalset;
}
public void addArtical(Artical artical)
{
artical.setArticalType(this);
this.articalset.add(artical);
}
public void removeArtical(Artical artical)
{
if(this.articalset.contains(artical))
{
this.articalset.remove(artical);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArticalType other = (ArticalType) obj;
if (Id != other.Id)
return false;
return true;
}
@Id @GeneratedValue
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@Column(length=30,nullable=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
Photo(图片表)
package cn.itcast.bean.db;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Photo {
private int id;
private String name;
private Date uptime;
private String photoinfo;
private String pathname;
private byte[] image;
private Master master;
@Column(length=30)
public String getPhotoinfo() {
return photoinfo;
}
public void setPhotoinfo(String photoinfo) {
this.photoinfo = photoinfo;
}
@Column(length=30)
public String getPathname() {
return pathname;
}
public void setPathname(String pathname) {
this.pathname = pathname;
}
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="master_id")
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=50)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Temporal(TemporalType.DATE)
public Date getUptime() {
return uptime;
}
public void setUptime(Date uptime) {
this.uptime = uptime;
}
@Lob
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
Word(评论表)
package cn.itcast.bean.db;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Word {
private int id;
private String content;
private Date setdate;
private Artical artical;
private Set<Review> reviewset=new HashSet<Review>();
private Master master;
@ManyToOne
@JoinColumn(name="master_id")
public Master getMaster() {
return master;
}
public void setMaster(Master master) {
this.master = master;
}
@OneToMany(cascade=CascadeType.REFRESH,mappedBy="word",fetch=FetchType.LAZY)
public Set<Review> getReviewset() {
return reviewset;
}
public void setReviewset(Set<Review> reviewset) {
this.reviewset = reviewset;
}
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="artical_id")
public Artical getArtical() {
return artical;
}
public void setArtical(Artical artical) {
this.artical = artical;
}
@Id @GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Temporal(TemporalType.DATE)
public Date getSetdate() {
return setdate;
}
public void setSetdate(Date setdate) {
this.setdate = setdate;
}
}
Review(回复评论表)
package cn.itcast.bean.db;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Review {
private int Id;
private String content;
private Date setdate;
private Word word;
@ManyToOne(cascade=CascadeType.REFRESH)
@JoinColumn(name="word_id")
public Word getWord() {
return word;
}
public void setWord(Word word) {
this.word = word;
}
@Id @GeneratedValue
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Temporal(TemporalType.DATE)
public Date getSetdate() {
return setdate;
}
public void setSetdate(Date setdate) {
this.setdate = setdate;
}
}