junit test用来测试hibernate的配置

javabean

package com.green.vo;

/**
 * Goods entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Goods implements java.io.Serializable {

	// Fields

	private int id;
	private Category category;
	private String num;
	private String name;
	private String image;
	private Double price;
	private Double amount;
	private Double specialflag;
	private String uploadtime;
	private Double salesflag;
	private String introduce;
	private String text;

	// Constructors

	/** default constructor */
	public Goods() {
	}

	/** full constructor */
	public Goods(Category category, String num, String name, String image,
			Double price, Double amount, Double specialflag, String uploadtime,
			Double salesflag, String introduce, String text) {
		this.category = category;
		this.num = num;
		this.name = name;
		this.image = image;
		this.price = price;
		this.amount = amount;
		this.specialflag = specialflag;
		this.uploadtime = uploadtime;
		this.salesflag = salesflag;
		this.introduce = introduce;
		this.text = text;
	}

	// Property accessors

	public int getId() {
		return this.id;
	}

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

	public Category getCategory() {
		return this.category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getNum() {
		return this.num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImage() {
		return this.image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Double getPrice() {
		return this.price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getAmount() {
		return this.amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Double getSpecialflag() {
		return this.specialflag;
	}

	public void setSpecialflag(Double specialflag) {
		this.specialflag = specialflag;
	}

	public String getUploadtime() {
		return this.uploadtime;
	}

	public void setUploadtime(String uploadtime) {
		this.uploadtime = uploadtime;
	}

	public Double getSalesflag() {
		return this.salesflag;
	}

	public void setSalesflag(Double salesflag) {
		this.salesflag = salesflag;
	}

	public String getIntroduce() {
		return this.introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public String getText() {
		return this.text;
	}

	public void setText(String text) {
		this.text = text;
	}

}

配置文件

Goods.hbm.xml

<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.green.vo.Goods" table="GOODS" schema="breem">
        <id name="id" >
            <generator class="native"></generator>
        </id>
        <many-to-one name="category" class="com.green.vo.Category" fetch="select">
            <column name="CATEGORYID"/>
        </many-to-one>
        <property name="num" type="java.lang.String">
            <column name="NUM" length="10" unique="true" />
        </property>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="50" />
        </property>
        <property name="image" type="java.lang.String">
            <column name="IMAGE" length="50" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="PRICE" precision="9" scale="0" />
        </property>
        <property name="amount" type="java.lang.Double">
            <column name="AMOUNT" precision="9" scale="0" />
        </property>
        <property name="specialflag" type="java.lang.Double">
            <column name="SPECIALFLAG" precision="9" scale="0" />
        </property>
        <property name="uploadtime" type="java.lang.String">
            <column name="UPLOADTIME" length="30" />
        </property>
        <property name="salesflag" type="java.lang.Double">
            <column name="SALESFLAG" precision="9" scale="0" />
        </property>
        <property name="introduce" type="java.lang.String">
            <column name="INTRODUCE" length="200" />
        </property>
        <property name="text" type="java.lang.String">
            <column name="TEXT" length="100" />
        </property>
    </class>
</hibernate-mapping>

Hibernate.cfg.xml

<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
    	<property name="connection.username">root</property>
    	<property name="connection.password"></property>
		<property name="connection.url">jdbc:mysql://localhost:3306/breem?useUnicode=true&characterEncoding=gb2312</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="myeclipse.connection.profile">MySQL_JDBC</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="show_sql">true</property>
		<mapping resource="com/green/vo/Goods.hbm.xml" />
		<mapping resource="com/green/vo/Category.hbm.xml" />
		<!--  
		<mapping resource="com/green/vo/Orders.hbm.xml" />
		<mapping resource="com/green/vo/Login.hbm.xml" />
		-->
    </session-factory>

</hibernate-configuration>

util

package Junit;

import org.hibernate.Session;
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;  
  

/*
 * 刚开始一个工程的时候,我需要测试一下数据库建立的表是否正常
 */
public final class HibernateUtil   
{  
    private static SessionFactory sessionFactory;  
      
    public static SessionFactory getSessionFactory() {  
        return sessionFactory;  
    }  
      
    public static Session getSession() {  
        return sessionFactory.openSession();  
    }  
    private HibernateUtil()  
    {  
          
    }  
    static  
    {  
        Configuration cfg=new Configuration();  
        cfg.configure();//如果说名字不是叫做“hibernate.cfg.xml”,需要在参数当中申明  
        sessionFactory=cfg.buildSessionFactory();  
    }  
} 

test

package Junit;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.green.vo.Goods;


public class test {
	
    static void addGood(Goods good)  
    {  
        Session s=null;  
        Transaction tx=null;  
        try {  
            s=HibernateUtil.getSession();  
            tx=s.beginTransaction();  
            s.save(good);  
            tx.commit();  
        }   
        finally  
        {  
            if(s!=null)  
                s.close();  
        }  
          
    }  
    
    @Test
    public  void test() {  
        
    	Goods good=new Goods();  
    	good.setNum("8888");
    	addGood(good);
        System.out.println("end");  
  
    }  

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值