好记性不如烂笔头

惠普周训(第二天)

Posted on By ZealerDrm

如需转载本网站内容,请标明转载来源,且保持作品完整性

 

package Explorer

bbs_hibernate
   src 
     com.bbs.bean
        Message.java
        Reply.java
        User.java
     com.bbs.test
        HibernateCase.java
        TestHibernate.java
        TestMessage.java
     com.bbs.util
        MySessionFatory.java
     hibernate.cfg.xml
   JRE System Library	  
   Referenced Libraries
   JUnit 4
   lib

Message.java

package com.bbs.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 留言持久化类
 * @author ZealerDrm
 *
 */
@Table(name="t_message")
@Entity

public class Message {
	private int msgid;
	//userid   外键关联; orm里,是映射的一个对象
	//@ManyToOne
	private User user;
	private String msgtopic;
	private String msgcontents;
	
	private String msgtime;
	private String msgip;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO) //策略,自动适应底层数据库

	public int getMsgid() {
		return msgid;
	}
	
	@JoinColumn(name="userid",nullable=false)
	@ManyToOne
	public User getUser() {
		return user;
	}
	
	public void setUser(User user){
		this.user = user;
	}
	
	@Column(name="msgtopic")
	public String getMsgtopic() {
		return msgtopic;
	}
	@Column(name="msgcontents")
	public String getMsgcontents() {
		return msgcontents;
	}
	@Column(name="msgtime")
	public String getMsgtime() {
		return msgtime;
	}
	@Column(name="msgip")
	public String getMsgip() {
		return msgip;
	}
	public void setMsgid(int msgid) {
		this.msgid = msgid;
	}
	public void setMsgtopic(String msgtopic) {
		this.msgtopic = msgtopic;
	}
	public void setMsgcontents(String msgcontents) {
		this.msgcontents = msgcontents;
	}
	public void setMsgtime(String msgtime) {
		this.msgtime = msgtime;
	}
	public void setMsgip(String msgip) {
		this.msgip = msgip;
	}
	
}

Reply.java

package com.bbs.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 回复信息表,对应t_reply
 * @author Administrator
 *
 */
@Entity // 
@Table(name="t_reply")
public class Reply implements Serializable{
	
	private int replyid; //回复id
	// 多对一              回复对应帖子主体
	//改造----private int msgid;  // 回复的留言的id
	private Message message;
	// 多对一            回复对应一个用户  
	//private int userid;   // 回复用户id
	private User user;
	private String replycontents; // 回复内容 
	private String replytime;  //  回复时间 
	private String replyip; // 回复ip

	@Id  //主键
	@GeneratedValue(strategy = GenerationType.AUTO)//策略,自动适应底层数据库
	public int getReplyid() {
		return replyid;
	}
	@ManyToOne
	@JoinColumn(name="msgid", nullable=false)
	public Message getMessage() {
		return message;
	}

	@ManyToOne
	@JoinColumn(name="userid", nullable=false)
	public User getUser() {
		return user;
	}
	@Column(name="replycontents",nullable=false,length=30)
	public String getReplycontents() {
		return replycontents;
	}
	@Column(name="replyip",nullable=false,length=30)
	public String getReplyip() {
		return replyip;
	}

	@Column(name="replytime",nullable=false,length=30)
	public String getReplytime() {
		return replytime;
	}

	public void setMessage(Message replyMessage) {
		this.message = replyMessage;
	}

	public void setUser(User replyUser) {
		this.user = replyUser;
	}
	
	
	public void setReplyid(int replyid) {
		this.replyid = replyid;
	}


	public void setReplycontents(String replycontents) {
		this.replycontents = replycontents;
	}
	
	public void setReplyip(String replyip) {
		this.replyip = replyip;
	}
	public void setReplytime(String replytime) {
		this.replytime = replytime;
	}	
}

User.java

package com.bbs.bean;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 用户实体类,持久化类
 * @author ZealerDrm
 *
 */

@Table(name="t_user")
public class User{
//对应数据库中的表t_user
	private int userid;
	private String username;
	private String password;
	private String sex;
	private String hobbys;
	private String birthday;
	private String city;
	private String email;
	private String qq;
	private String createtime;
	private String userAddress;
	private int userAge;
	
	//@Column(name="userid")
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO) //策略,自动适应底层数据库
	public int getUserid() {
		return userid;
	}
	@Column(name="username")
	public String getUsername() {
		return username;
	}
	@Column(name="password")
	public String getPassword() {
		return password;
	}
	@Column(name="sex")
	public String getSex() {
		return sex;
	}
	@Column(name="hobbys")
	public String getHobbys() {
		return hobbys;
	}
	@Column(name="birthday")
	public String getBirthday() {
		return birthday;
	}
	@Column(name="city")
	public String getCity() {
		return city;
	}
	@Column(name="email")
	public String getEmail() {
		return email;
	}
	@Column(name="qq")
	public String getQq() {
		return qq;
	}
	@Column(name="createtime")
	public String getCreatetime() {
		return createtime;
	}
	public String getUserAddress() {
		return userAddress;
	}
	public int getUserAge() {
		return userAge;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public void setHobbys(String hobbys) {
		this.hobbys = hobbys;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}
	public void setUserAge(int userAge) {
		this.userAge = userAge;
	}
	
}

HibernateCasei.java

package com.bbs.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.bbs.bean.Message;
import com.bbs.util.MySessionFactory;

import junit.framework.TestCase;

/**
 * 单元测试
 * @author ZealerDrm
 *
 */
public class HibernateCase extends TestCase{
	SessionFactory sessionFactory = MySessionFactory.getSessionFactory();
	public void testSum(){
		int s = 1 + 2;
		assertEquals(3, s); // 预期结果是3,实际结果是3
	}
	public void testGetMessage(){
		Session session = sessionFactory.openSession();
		//执行查询
		Message message = (Message)session.get(Message.class,70);
		//关闭
		session.close();
		System.out.println(message.getMsgtopic());
		
		assertEquals("1",message.getMsgtopic());
	}
	public static void main(String[] args) {
		Session session = MySessionFactory.getSessionFactory().openSession();
		//执行查询
		Message message = (Message)session.get(Message.class,70);
		//关闭
		session.close();
		System.out.println(message.getMsgtopic());
	
	}
}

TestHibernate.java

package com.bbs.test;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


/**
 * 测试Hibernate
 * @author ZealerDrm
 *
 */
public class TestHibernate {
	public static void main(String[] args){
		 //读取 Hibernate 配置文件及信息 
		Configuration config = new Configuration().configure();
		 //通过初始化、加载、获取一个server服务(hibernate4.0)
		StandardServiceRegistry registryService = 
				new StandardServiceRegistryBuilder()
				.applySettings(config.getProperties())
				.build(); 
		 //生成sessionfactory
		SessionFactory sessionFactory = 
				config.buildSessionFactory(registryService);
		
		 //用读取的配置信息生成 SessionFactory 对象 
		System.out.println(sessionFactory);
		 // SessionFactory 对象获取一个 Session 对象 
		
		 // Session 对象生成事务 
		
		 //通过 Session 对象的方法进行操作 
		
		 //提交或回滚事务 
		
		 //释放 session and session factory 资源 
	}

}

TestMessage.java

package com.bbs.test;

public class TestMessage {
	public static void getMessage(){
		
	}
	public static void saveMessage(){
		
	}
	public static void getUser(){
		
	}
	public static void saveUser(){
	
	}
	public static void getReply(){
		
	}
	public static void saveReply(){
		
	}
	public static void main (String[] args){
		getMessage();
		saveMessage();
		getUser();
		saveUser();
		getReply();
		saveReply();
	}
}

MySessionFactory.java

package com.bbs.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * session 工厂类,单例模式
 * @author ZealerDrm
 *
 */
public class MySessionFactory {
	private static SessionFactory sessionFactory;

	static{
		Configuration config = new Configuration().configure();
		
		StandardServiceRegistry registry 
			= new StandardServiceRegistryBuilder()
			.applySettings(config.getProperties()).build();
		//sessionFactory赋值
		if(sessionFactory==null){
			sessionFactory = config.buildSessionFactory(registry);
		}
	}
	
	private MySessionFactory(){}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
		

}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	 <session-factory>
		 <!-- 数据库连接 -->
		 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		 <property name="connection.url">jdbc:mysql://localhost:3306/db_bbs</property>
		 <property name="connection.username">root</property>
		 <property name="connection.password">root</property>
		
		 <!-- 方言dialect -->
		 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		 
		 <property name="current_session_context_class">thread</property>
		 <!-- 连接池Jdbc connection pool C3P0-->
		 <property name="connection.pool_size">1</property>
		 
		
		 <!-- 是否显示sql语句Echo all executed SQL to stdout -->
		 <property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		
		<!-- 映射文件配置,配置文件名必须包含其相对于根的全路径 -->
		<mapping class="com.bbs.bean.User" />
	</session-factory>
</hibernate-configuration>