In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The Hibernate framework development step project imports the required jar packages:
Http://pan.baidu.com/s/1eRQ19C2
Write hibernate.cfg.xml files
DOCTYPE hibernate-configuration PUBLIC
"- / / Hibernate/Hibernate Configuration DTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Jdbc:mysql://127.0.0.1:3306/test
Root
123456
Com.mysql.jdbc.Driver
Org.hibernate.dialect.MySQLDialect
True
True
Update
Write entity classes, create tables
Eg:
Package com.edu.bean
Public class User {
Private intid
Private String username
Private String password
Public intgetId () {
Return id
}
Public voidsetId (int id) {
This.id = id
}
Public String getUsername () {
Return username
}
Public voidsetUsername (String username) {
This.username= username
}
Public String getPassword () {
Return password
}
Public voidsetPassword (String password) {
This.password= password
}
Public User (Stringusername, String password) {
Super ()
This.username= username
This.password= password
}
Public User () {
/ / TODO Auto-generated constructor stub
}
}
Write a mapping file
Eg:
DOCTYPE hibernate-mapping PUBLIC "- / / Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Put the mapping file into hibernate.cfg.xml
Write code:
Eg:
Package com.edu.test
Import org.hibernate.Session
Import org.hibernate.SessionFactory
Import org.hibernate.Transaction
Import org.hibernate.cfg.Configuration
Import org.junit.Test
Import com.edu.bean.User
Public class HibernateTest {
@ Test
Public voidtest () {
/ / read the hibernate.cfg.xml main configuration file
Configurationcfg=new Configuration () .configure (hibernate.cfg.xml)
/ / create a SessionFactory factory
SessionFactorysf=cfg.buildSessionFactory ()
/ / obtain session
Sessionsession=sf.openSession ()
/ / create a transaction
Transactiontc=session.beginTransaction ()
/ / create a User object
Useruser=new User (Zhang San, 123456)
/ / persist object
Try {
/ / Save data
Session.save (user)
/ / commit transaction (indispensable)
Tc.commit ()
} catch (Exception e) {
/ / data operation failed, rollback transaction
Tc.rollback ()
}
/ / close session
Session.close ()
}
}
Code layering optimization: get the HibernateGetSession class of Session:
Package com.edu.dbconn
Import org.hibernate.Session
Import org.hibernate.SessionFactory
Import org.hibernate.cfg.Configuration
Public class HibernateGetSession {
/ / make sure that the SessionFactory factory is created only once
Private staticSessionFactory sf
Static {
If (sf==null) {
/ / read the hibernate.cfg.xml main configuration file
Configurationcfg=new Configuration () .configure (hibernate.cfg.xml)
/ / create a SessionFactory factory
Sf=cfg.buildSessionFactory ()
}
}
Public staticSession getSession () {
/ / create a session
Sessionsession=sf.openSession ()
Return session
}
}
The UserDao class of the Dao layer:
Package com.edu.dao
Import org.hibernate.Session
Import org.hibernate.Transaction
Import com.edu.bean.User
Import com.edu.dbconn.HibernateGetSession
Public class UserDao {
Public voidsaveUser () {
/ / obtain session
Sessionsession=HibernateGetSession.getSession ()
/ / create a transaction
Transactiontc=session.beginTransaction ()
/ / create a User object
Useruser=new User ("Li Si", "123456")
/ / persist object
Try {
/ / Save data
Session.save (user)
/ / commit transaction (indispensable)
Tc.commit ()
} catch (Exception e) {
/ / data operation failed, rollback transaction
Tc.rollback ()
}
/ / close session
Session.close ()
}
}
Test layer:
Package com.edu.test
Import org.junit.Test
Import com.edu.dao.UserDao
Public class HibernateTest {
@ Test
Public voidtest () {
UserDaoud=new UserDao ()
Ud.saveUser ()
}
}
Instance source code:
Http://pan.baidu.com/s/1eRPLFJO
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.