Hibernte配置以及junit測試筆記,hiberntejunit筆記

來源:互聯網
上載者:User

Hibernte配置以及junit測試筆記,hiberntejunit筆記
Hibernate配置
junit測試
import static org.junit.Assert.*;


public class HibernateTest{
SessionFactory  factory=null;
Session session=null;
//測試之前初始化資料
@Before
public void setUp() throws Eception{
System.out.println("初始化");
Configuration config=new Configuration


().configure();

ServiceRegistry sr=new 


ServiceRegistryBuilder.applySetting(config.getProperties());
factory=config.buildSessionFactory(sr);
session=factory.openSession();



}
@After
//測試之後釋放資料
public void tearDown() throws Exception{
System.out.println("結束,釋放資源");
if(session.isOpen()){
session.close();
}
}

}
@Test測試方法
public void tearDown() throws Exception{
System.out.println("初始化");



} @Test
public void testAdd(){
Transaction tx=session.beginTransaction();
Person p=new Person("admin",123456,new Date


());
session.persist(p);//新增需要事務
tx.commit();


}
}
//Hibernate深入配置
access
<hibernate-mapping default-access="property">
//field(可以不需要getter/setter方法操作對象屬性)
(盡量不要這樣設定)
Person p=(Person)session.get(Person.class,id);//根據反射建立


對象
System.out.println(p);
<!--
1.default-access="field"對象屬性狀態方式設定
property:表示這個屬性需要通過getter/settter方法搓澡
預設使用
2.package:指定本持久化設定檔的class的包名
auto-import:指定引入包
default-lazy:指定本持久化設定檔中的class消極式載入策略
default-cascade:指定本持久化設定檔中的class級聯操作策略
<class name="Person" table="t_person">
dynamic-insert:沒有賦值的屬性不會參與新增操作
若為true:沒有賦值的屬性不會參與操作
mutable:表明該類是否允許更新
false:不允許更新,但是可以新增刪除
select-before-update:true(執行更新之前先發起一條sql查詢,


看看資料是否與表不一致,不一致就更新,否則不更新)
where="t_name=test2":在查詢之前附帶一個where查詢條件
<property name="count" formula="(select (*)from 


t_Person) "/>
指定一個屬性的資料來源formula對應的查詢語句


public void testAdd(){


Person p=new Person();
p.setName("Xxx");
System.out.println(p);
session.beginTransaction();
session.persist(p);
tx.commit();
}
public void testUpdate(){


Person p=new Person();
p.setId(1);
p.setName("Xxx");
System.out.println(p);
session.beginTransaction();
session.Update(p);//更新需要Id
tx.commit();

}


-->
<!--property其他屬性對應配置
name:指定持久化類中需要映射的屬性名稱
column:指定表的列名(不寫表示使用預設屬性名)
length:指定列儲存資料長度
type:指定欄欄位類型


-->



怎在Eclipse SDK中實現Ant構建技術與編寫Junit測試範例

eclipse 早已整合junit 直接建立測試案例即可
ant同樣整合著呢
詳情請自行翻閱 eclipse 內建文檔
 
整合ssh2遇到一個問題,好心人解答

用servlet/jsp的時候,你寫的是web.xml檔案,這個是特殊的,會自動reload的

但是spring的,hibernate的,struts的配置
是不會reload的

做SSH開發,要會用測試單元進行測試!
(我用的是JUnit4,你可以用Spring內建的測試載入器,可以直接測試SSH整合的!)

Spring和hibernte還有Struts2.x
都是不依賴web環境的!(struts2.x Action部分)
也就是說,你開發,測試的時候,都可以不部署到Tomcat上!,而改用main或者測試載入器進行編碼的測試!

這個是SSH的開發方式,也是螺旋遞增式開發的一部分!
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.