資源描述:
《hibernate入門》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、大致課程安排Hibernateday01、hibernate人門(hibernate操作數(shù)據(jù)查詢、新增、刪除、修改)1回顧:jdbc操作數(shù)據(jù)庫(kù)1創(chuàng)建表personid主鍵name姓名age年齡status是否刪除狀態(tài)0表示有效,1表示已刪除狀態(tài)2創(chuàng)建web項(xiàng)目3創(chuàng)建實(shí)體類(pojo、Entity)Person類intidStringnameintageStringstatuspackagecom.chinasofti.pojo;importjava.io.Serializable;@SuppressWarning
2、s("serial")publicclassPersonimplementsSerializable{privateIntegerid;privateStringname;privateintage;privateStringstatus;publicPerson(){}publicPerson(Stringname,intage,Stringstatus){super();this.name=name;this.age=age;this.status=status;}publicIntegergetId(){r
3、eturnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetStatus(){returnstatus;}publicvoidsetStatus(Strin
4、gstatus){this.status=status;}}4寫獲得Connection的JdbcUtil類packagecom.chinasofti.util;importjava.io.IOException;importjava.io.InputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.util.Properties;publicclassJdbcU
5、til{privatestaticStringdriver;privatestaticStringurl;privatestaticStringusername;privatestaticStringpassword;privatestaticThreadLocaltlc=newThreadLocal();//靜態(tài)代碼塊static{Propertiesprop=newProperties();//jdbc.properties文件與JdbcUtil在同一目錄下In
6、putStreaminStream=JdbcUtil.class.getResourceAsStream("jdbc.properties");try{prop.load(inStream);driver=prop.getProperty("driver");url=prop.getProperty("url");username=prop.getProperty("user");password=prop.getProperty("password");}catch(IOExceptione){e.printS
7、tackTrace();}}publicstaticConnectiongetConnection(){//從ThreadLocal對(duì)象中獲得與當(dāng)前線程綁定的連接Connectioncon=tlc.get();if(con==null){try{//加載mysql驅(qū)動(dòng)包Class.forName(driver);//獲得一個(gè)新的數(shù)據(jù)庫(kù)的連接con=DriverManager.getConnection(url,username,password);}catch(Exceptione){e.printStackTr
8、ace();}}returncon;}publicstaticvoidcloseConnection(Connectioncon){if(con!=null){try{con.close();}catch(SQLExceptione){e.printStackTrace();}}}}5創(chuàng)建Dao類Dao是用來(lái)操作數(shù)據(jù)庫(kù)數(shù)據(jù)的對(duì)象packagecom.chinasofti.