Hibernate入门

关于ThreadLocal
 
一个Demo:
1、PO=POJO+持久化注解(或者对象映射文件)
 1 @Entity
 2 @Table(name="news_inf")
 3 public class News {
 4     //标识属性
 5     @Id
 6     @GeneratedValue(strategy=GenerationType.IDENTITY)
 7     private Integer id;
 8     //消息标题
 9     private String title;
10     //消息内容
11     private String content;
12     public Integer getId() {
13         return id;
14     }
15     public void setId(Integer id) {
16         this.id = id;
17     }
18     public String getTitle() {
19         return title;
20     }
21     public void setTitle(String title) {
22         this.title = title;
23     }
24     public String getContent() {
25         return content;
26     }
27     public void setContent(String content) {
28         this.content = content;
29     }    
30  
31 }
View Code

 

 

2、hibernate配置文件(hibernate.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- configuration properties for JDBC -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
 
        <!-- configuration properties for the c3p0 connection pool -->
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.validate">true</property>
 
        <!-- database dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
 
           <!-- Echo all executed SQL to standard out -->
        <property name="show_sql">true</property>
 
        <!-- format SQL before print -->
        <property name="hibernate.format_sql">true</property>
 
        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
 
            <!-- mapping -->
        <mapping class="com.wj.app.pojo.News"/>
 
    </session-factory>
</hibernate-configuration>
View Code

 

3、JAR包(lib下required子目录 + mysql-connector-java-5.1.30-bin.jar + lib下optional\C3P0子目录)
 
4、操作步骤
  •         获取Configuration
  •         获取SessionFactory
  •         获取Session,打开事务
  •         用面向对象的方式操作数据库
  •         关闭事务,关闭session
 1 public class NewsManager {
 2  
 3     public static void main(String[] args) {
 4         Configuration conf = new Configuration()
 5                                  .configure();
 6         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
 7                                                 .applySettings(conf.getProperties())
 8                                                 .buildServiceRegistry();
 9         SessionFactory sessionFactory = conf.buildSessionFactory(serviceRegistry);
10         Session session = sessionFactory.openSession();
11  
12         Transaction transaction = session.beginTransaction();
13  
14         News news =new News();        
15         news.setTitle("news1");        
16         news.setContent("news1Content");
17  
18         session.save(news);        
19         transaction.commit();
20  
21         session.close();
22         sessionFactory.close();        
23     }
24  
25 }
View Code

 

 
posted @ 2016-08-08 14:19  JillWen  阅读(235)  评论(0编辑  收藏  举报