hibernate 简单回顾

 Message  类的映射文件

//表id,title ,content,userid

import java.io.Serializable;

public class Message implements Serializable {

 private static final long serialVersionUID = 1L;

 private String id;
 private String title;
 private String content;
 private Userinfo userid;

 public Message(String id, String title, String content, Userinfo userid) {
  this.id = id;
  this.title = title;
  this.content = content;
  this.userid = userid;
 }
 public Message(){}
 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public Userinfo getUserid() {
  return userid;
 }

 public void setUserid(Userinfo userid) {
  this.userid = userid;
 }

 @Override
 public String toString() {
  // TODO Auto-generated method stub
  return super.toString();
 }

}

 

 

<?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节点定义了Province类和对应数据库表之间的关联关系 --> 
     <class 
        name="domain.Message" 
        table="message" 
        
     >  
     <id name="id" 
         type="java.lang.String" 
         column="id">
         <generator class="uuid.hex" />
     </id>
     <property 
        name="title" 
        type="java.lang.String" 
        column="title" 
        not-null="true" 
     >   
     </property> 
     <property 
        name="content" 
        type="java.lang.String" 
        column="content" 
        not-null="true" 
     >   
     </property> 
     
     <!-- "多对一"型关联关系 --> 
     <many-to-one 
        name="userid" 
        class="domain.Userinfo" 
        cascade="save-update" 
     	column="userid" 
     >
      
     </many-to-one> 
      
     </class>
	 		  
     
     </hibernate-mapping>


Userinfo 类映射文件

表id , name ,password

<?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 package="domain">
		<class name="Userinfo" table="userinfo" >
			<id name="id" type="java.lang.String">
				<generator class="uuid.hex"/>
			</id>
	        <property name="name" type="java.lang.String" column="name" not-null="true"/>
			<property name="password" type="java.lang.String" column="password" not-null="true"/>
 			<set name="message" inverse="true" cascade="save-update" lazy="true" fetch="join">
				<key column="userid" />
				<one-to-many class="Message" />
			</set>
 			 
		</class>
  
	</hibernate-mappin


userinfo 类

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;


 

public class Userinfo implements Serializable {

	private static final long serialVersionUID = 1L;
	private String id ;
	private String name;
	private String password;
	private Set message = new HashSet(0);
	public Userinfo() {
		
	}
	public Userinfo(String name){
		this.name = name;
	}
	public Userinfo(String id, String name, String password, Set message) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.message = message;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Set getMessage() {
		return message;
	}
	public void setMessage(Set message) {
		this.message = message;
	}
	
	
}


。hibernate 库连接配置

<?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> 
     <!--用户名 --> 
     <property name="connection.username">sa</property> 
     <!--url信息 --> 
     <property name="connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=hib</property> 
     <!--数据库方言信息--> 
     <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> 
     <!--密码 --> 
     <property name="connection.password">1234</property> 
     <!--数据库驱动信息 --> 
     <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> 
       <property name="show_sql">true</property> 
     <!--指定Hibernate映射文件路径 --> 
     <mapping class="Message" resource="domain/Message.hbm.xml" />
     <mapping class="Userinfo" resource="domain/Userinfo.hbm.xml" /> 
     </session-factory> 
</hibernate-configuration> 
 
 

     


jar 文件

 

13 个+1个即可运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值