最近看那本深入淺出hibernate 真是很不錯啊。。講的也很細。。
剛剛小試了一把,真的很過隱。。
我用的是MYSQL數(shù)據(jù)庫
表結(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:評論表
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)出來的。。
表建好了。接下來寫vo 類了..
這是文章表的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() {
}
// 自己寫上get set 方法。。我就不貼上來了 評論表的。
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 方法自己加上。。 接下來 寫映射文件了..
我用的是hibernate.properties 文件連接數(shù)據(jù)庫。
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.sourceforge.net/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 >
配置文件 那些字段 屬性是什么意思。。你到首頁搜索一下,很多的 。
<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/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 >
其實這些都是可以自動生成的。。你去看看http://blog.csdn.net/javamxj/category/111072.aspx
他講的很詳細。。
一切都準(zhǔn)備好了。。該寫個類來小試一下了。。
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初試化的時候會自動調(diào)用
一般用來初試化公共資源。。
這里用來初試化Hibernate Session
*/
protected void setUp() throws Exception {
Configuration config = new Configuration();
config.addClass(articleVO. class ).addClass(remarkVO. class );
sessionFactory = config.buildSessionFactory();
ss = sessionFactory.openSession();
}
/**/ /*
* 這個方法junit TestCase執(zhí)行完畢時,會自動調(diào)用tearDown方法。
* 一般用于釋放資源,我這里是關(guān)閉在setUp()方法里打開的Session
*/
protected void tearDown() throws Exception {
try {
ss.close();
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
// 測試添加一篇文章
public void testAddArticle() throws Exception {
try {
wjjcms.vo.articleVO vo = new articleVO();
vo.setA_author( " wujunjun " );
vo.setA_body( " 熱愛祖國,堅決抗日! " );
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());
}
}
// 測試添加一篇評論
public void testAddRemark() throws Exception {
try {
wjjcms.vo.remarkVO vo = new remarkVO();
vo.setR_body( " 有是你個小日本。。。。 " );
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());
}
}
// 測試同時添加一騙文章和5篇評論
public void testAddAll()
{
wjjcms.vo.articleVO vo = new articleVO();
vo.setA_author( " wujunjun " );
vo.setA_body( " 熱愛祖國,堅決抗日! " );
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( " 有是你個小日本。。。。 " );
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();
}
}
// 測試顯示文章。。和評論。。
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());
}
}
}
}
運行一下看看。
OK,,成功了。數(shù)據(jù)也已經(jīng)進數(shù)據(jù)庫了。。
哈。。我是菜鳥。專家多指點啊。。