最近看那本深入浅出hibernate 真是很不错啊。。讲的也很细。。 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`) ) 表结构我直接导出来的。。 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.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 > 配置文件 那些字段 属性是什么意思。。你到首页搜索一下,很多的 。 <? 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 >
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初试化的时候会自动调用 一般用来初试化公共资源。。 这里用来初试化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执行完毕时,会自动调用tearDown方法。 * 一般用于释放资源,我这里是关闭在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()); } } } } 运行一下看看。 |
|