`

Hibernate入门

阅读更多
Java代码
1.public class User {  
2.      
3.    private String id;  
4.    private String name;  
5.    private String password;  
6.    private Date createTime;  
7.    private Date expireTime;  
8. 
9.         public User(){}  
10. 
11.         // getter and setter methods  
12.         // .....  
13.} 
public class User {

private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;

         public User(){}

         // getter and setter methods
         // .....
}


Java代码
1.<hibernate-mapping>  
2.    <class name="org.darkness.hibernate.User">  
3.        <id name="id">  
4.            <generator class="uuid"/>  
5.        </id>  
6.        <property name="name"/>  
7.        <property name="password"/>  
8.        <property name="createTime"/>  
9.        <property name="expireTime"/>  
10.    </class>  
11.</hibernate-mapping> 
<hibernate-mapping>
<class name="org.darkness.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>


Java代码
1.import org.hibernate.cfg.Configuration;  
2.import org.hibernate.tool.hbm2ddl.SchemaExport;  
3. 
4.public class ExportDB {  
5. 
6.    public static void main(String[] args) {  
7.          
8.        //读取hibernate.cfg.xml文件  
9.        Configuration cfg = new Configuration().configure();  
10.          
11.        SchemaExport export = new SchemaExport(cfg);  
12.          
13.        export.create(true, true);  
14.    }  
15.} 
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);
}
}



Java代码
1.import java.util.Date;  
2. 
3.import org.hibernate.Session;  
4.import org.hibernate.SessionFactory;  
5.import org.hibernate.cfg.Configuration;  
6. 
7.public class Client {  
8. 
9.    public static void main(String[] args) {  
10.          
11.        //读取hibernate.cfg.xml文件  
12.        Configuration cfg = new Configuration().configure();  
13.          
14.        //创建SessionFactory  
15.        SessionFactory factory = cfg.buildSessionFactory();  
16.          
17.        Session session = null;  
18.        try {  
19.            session = factory.openSession();  
20.              
21.            //开启事务  
22.            session.beginTransaction();  
23.              
24.            User user = new User();  
25.            user.setName("张三");  
26.            user.setPassword("123");  
27.            user.setCreateTime(new Date());  
28.            user.setExpireTime(new Date());  
29.              
30.            //保存数据  
31.            session.save(user);  
32.              
33.            //提交事务  
34.            session.getTransaction().commit();  
35.        }catch(Exception e) {  
36.            e.printStackTrace();  
37.            //回滚事务  
38.            session.getTransaction().rollback();  
39.        }finally {  
40.            if (session != null) {  
41.                if (session.isOpen()) {  
42.                    //关闭session  
43.                    session.close();  
44.                }  
45.            }  
46.        }  
47.          
48.    }  
49.} 
import java.util.Date;

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

public class Client {

public static void main(String[] args) {

//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();

//创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory();

Session session = null;
try {
session = factory.openSession();

//开启事务
session.beginTransaction();

User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());

//保存数据
session.save(user);

//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}

}
}



Java代码
1.<hibernate-configuration>  
2.    <session-factory>  
3.        <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>  
4.        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
5.        <property name="hibernate.connection.username">root</property>  
6.        <property name="hibernate.connection.password">depravedAngel</property>  
7.        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
8.        <property name="hibernate.show_sql">true</property>  
9.          
10.        <mapping resource="org/darkness/hibernate/User.hbm.xml"/>  
11.    </session-factory>  
12.</hibernate-configuration>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics