最近看那本深入淺出hibernate 真是很不錯(cuò)啊。。講的也很細(xì)。。
剛剛小試了一把,真的很過(guò)隱。。
我用的是MYSQL數(shù)據(jù)庫(kù)
表結(jié)構(gòu)。
1:文章表
CREATE TABLE `t_article` (
`a_id` int ( 11 ) NOT NULL auto_increment,
`a_sort` int ( 11 ) NOT NULL default ‘ 0 ‘ ,
`a_title` varchar ( 50 ) default NULL ,
`a_body` text ,
`a_author` varchar ( 11 ) default ‘‘ ,
`a_hit` int ( 11 ) NOT NULL default ‘ 0 ‘ ,
`c_id` int ( 11 ) default ‘ 0 ‘ ,
`a_date` varchar ( 20 ) default NULL ,
PRIMARY KEY (`a_id`)
)
2:評(píng)論表
CREATE TABLE `t_remark` (
`r_id` int ( 11 ) NOT NULL auto_increment,
`a_id` int ( 11 ) NOT NULL default ‘ 0 ‘ ,
`r_name` varchar ( 20 ) NOT NULL default ‘‘ ,
`r_title` varchar ( 50 ) default ‘‘ ,
`r_body` varchar ( 100 ) default NULL ,
`r_email` varchar ( 30 ) default NULL ,
`r_date` varchar ( 30 ) default NULL ,
PRIMARY KEY (`r_id`),
KEY `a_id` (`a_id`)
)
表結(jié)構(gòu)我直接導(dǎo)出來(lái)的。。
表建好了。接下來(lái)寫(xiě)vo 類(lèi)了..
這是文章表的VO
package wjjcms.vo;
import java.util. * ;


public class articleVO
{
private int a_id;
private int a_sort;
private int a_hit;
private int c_id;
private String a_title;
private String a_body;
private String a_author;
private String a_date;
private Set a_remark;

public articleVO()
{
}
// 自己寫(xiě)上get set 方法。。我就不貼上來(lái)了
評(píng)論表的。
package wjjcms.vo;


public class remarkVO
{
private int a_id;
private int r_id;
private String r_name;
private String r_title;
private String r_body;
private String r_email;
private String r_date;

public remarkVO()
{
}
//get set 方法自己加上。。
接下來(lái) 寫(xiě)映射文件了..
我用的是hibernate.properties 文件連接數(shù)據(jù)庫(kù)。
hibernate.query.substitutions true 1, false 0, yes ‘Y‘, no ‘N‘
## MySQL
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class org.gjt.mm.mysql.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/wjcms
hibernate.connection.username root
hibernate.connection.password wujun
hibernate.connection.pool_size 1
hibernate.proxool.pool_alias pool1
hibernate.show_sql true
hibernate.jdbc.batch_size 0
hibernate.max_fetch_depth 1
hibernate.cache.use_query_cache true
該文件記的放在classes目錄下面。。
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate./hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="wjjcms.vo.articleVO" table ="t_article" >
< id name ="a_id" column ="a_id" unsaved-value ="0" >
< generator class ="native" />
</ id >
< property name ="c_id" column ="c_id" />
< property name ="a_title" column ="a_title" />
< property name ="a_sort" column ="a_sort" />
< property name ="a_date" column ="a_date" />
< property name ="a_body" column ="a_body" />
< property name ="a_hit" column ="a_hit" />
< property name ="a_author" column ="a_author" />
< set name ="a_remark" cascade ="all" outer-join ="true" >
< key column ="a_id" />
< one-to-many class ="wjjcms.vo.remarkVO" />
</ set >
</ class >
</ hibernate-mapping >
配置文件 那些字段 屬性是什么意思。。你到首頁(yè)搜索一下,很多的 。
<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate./hibernate-mapping-2.0.dtd" >
< hibernate-mapping >
< class name ="wjjcms.vo.remarkVO" table ="t_remark" >
< id name ="r_id" column ="r_id" unsaved-value ="0" >
< generator class ="native" />
</ id >
< property name ="r_name" column ="r_name" />
< property name ="r_email" column ="r_email" />
< property name ="r_title" column ="r_title" />
< property name ="r_body" column ="r_body" />
< property name ="r_date" column ="r_date" />
< property name ="a_id" column ="a_id" />
</ class >
</ hibernate-mapping >
其實(shí)這些都是可以自動(dòng)生成的。。你去看看http://blog.csdn.net/javamxj/category/111072.aspx
他講的很詳細(xì)。。
一切都準(zhǔn)備好了。。該寫(xiě)個(gè)類(lèi)來(lái)小試一下了。。
package wjjcms.test;

import junit.framework. * ;
import net.sf.hibernate.cfg. * ;
import net.sf.hibernate. * ;
import wjjcms.vo.remarkVO;
import wjjcms.vo.articleVO;
import java.sql.SQLException;
import java.util. * ;


public class TestText extends TestCase
{

private SessionFactory sessionFactory;
private Session ss = null ;

public TestText(String name)
{
super (name);
}
/**/ /*
junit中setUp方法在TestCase初試化的時(shí)候會(huì)自動(dòng)調(diào)用
一般用來(lái)初試化公共資源。。
這里用來(lái)初試化Hibernate Session
*/
protected void setUp() throws Exception
{
Configuration config = new Configuration();
config.addClass(articleVO. class ).addClass(remarkVO. class );
sessionFactory = config.buildSessionFactory();
ss = sessionFactory.openSession();
}
/**/ /*
* 這個(gè)方法junit TestCase執(zhí)行完畢時(shí),會(huì)自動(dòng)調(diào)用tearDown方法。
* 一般用于釋放資源,我這里是關(guān)閉在setUp()方法里打開(kāi)的Session
*/
protected void tearDown() throws Exception
{

try
{
ss.close();

} catch (HibernateException ex)
{
ex.printStackTrace();
}
}
// 測(cè)試添加一篇文章
public void testAddArticle() throws Exception
{

try
{
wjjcms.vo.articleVO vo = new articleVO();
vo.setA_author( " wujunjun " );
vo.setA_body( " 熱愛(ài)祖國(guó),堅(jiān)決抗日! " );
vo.setA_date( " 2006-3-30 " );
vo.setA_hit( 33 );
vo.setA_sort( 1 );
vo.setA_title( " 小日本鬼子 " );
vo.setC_id( 1 );
ss.save(vo);
ss.flush();
ss.connection().commit();
ss.close();

} catch (HibernateException ex)
{
// junit.framework.Assert.
System.out.print(ex.getMessage());
}
}
// 測(cè)試添加一篇評(píng)論
public void testAddRemark() throws Exception
{

try
{
wjjcms.vo.remarkVO vo = new remarkVO();
vo.setR_body( " 有是你個(gè)小日本。。。。 " );
vo.setR_date( " 2006-1-1 " );
vo.setA_id( 1 );
vo.setR_email( " wujun1866@gmail.com " );
vo.setR_name( " wujunjun " );
vo.setR_title( " re:小日本,打的好 " );
ss.save(vo);
ss.flush();
ss.connection().commit();

} catch (HibernateException ex)
{
System.out.print(ex.getMessage());
}
}
// 測(cè)試同時(shí)添加一騙文章和5篇評(píng)論
public void testAddAll()

{
wjjcms.vo.articleVO vo = new articleVO();
vo.setA_author( " wujunjun " );
vo.setA_body( " 熱愛(ài)祖國(guó),堅(jiān)決抗日! " );
vo.setA_date( " 2006-3-30 " );
vo.setA_hit( 33 );
vo.setA_sort( 1 );
vo.setA_title( " 小日本鬼子 " );
vo.setC_id( 1 );

Set remarkSet = new HashSet();
for ( int i = 0 ;i < 5 ;i ++ )

{
wjjcms.vo.remarkVO reVO = new remarkVO();
reVO.setR_body( " 有是你個(gè)小日本。。。。 " );
reVO.setR_date( " 2006-1-1 " );
reVO.setA_id( 1 );
reVO.setR_email( " wujun1866@gmail.com " );
reVO.setR_name( " wujunjun " );
reVO.setR_title( " re:小日本,打的好 " );
remarkSet.add(reVO);
}
vo.setA_remark(remarkSet);
try
{
ss.save(vo);
ss.flush();
ss.connection().commit();
}
catch (Exception ex)

{
ex.printStackTrace();
}
}
// 測(cè)試顯示文章。。和評(píng)論。。
public void testShowArticle() throws SQLException, HibernateException
{
Query q = ss.createQuery( " from articleVO where c_id=? " );
q.setInteger( 0 , 1 );
List l = q.list();

for ( int i = 0 ; i < l.size(); i ++ )
{
articleVO showVO = (articleVO) l.get(i);
System.out.print(showVO.getA_author());
System.out.print(showVO.getA_title());
java.util.Iterator it = showVO.getA_remark().iterator();

while (it.hasNext())
{
remarkVO reVO = (remarkVO) it.next();
System.out.print(reVO.getR_email());
System.out.print(reVO.getR_title());
}
}
}
}
運(yùn)行一下看看。

OK,,成功了。數(shù)據(jù)也已經(jīng)進(jìn)數(shù)據(jù)庫(kù)了。。
哈。。我是菜鳥(niǎo)。專(zhuān)家多指點(diǎn)啊。。