Hibernate 4.3.7 可编程方式+注解

1.复制jar文件到lib

antlr-2.7.7.jar
dbmysql.jar
dboracle.jar
dbsqljdbc2005.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar

 

2.注解学生类

package com.entity;



import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.Id;



@Entity

public class Student {

    private int id;

    private String name;



    @Id

    @GeneratedValue

    public int getId() {return id;}

    public void setId(int id) {this.id = id;}

    @Column(name = "name", length = 20)

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}



}

3.HibernateUtil.java 可编程方式类

package com;

import java.util.List;

import java.util.Properties;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import com.entity.Student;

public class HibernateUtil {

    public static void main(String[] args) {

        Properties m = new Properties();

        m.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        m.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");  

        m.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8");  

        m.put("hibernate.connection.username", "root");

        m.put("hibernate.connection.password", "fengze");  

        m.put("hibernate.hbm2ddl.auto","update");

        

        Properties o = new Properties();

        o.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");

        o.put("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");  

        o.put("hibernate.connection.url", "jdbc:oracle:thin:@localhost:1521:ORCL");  

        o.put("hibernate.connection.username", "system");

        o.put("hibernate.connection.password", "FengZe2012");  

        o.put("hibernate.hbm2ddl.auto","update");

        

        Properties s = new Properties();

        s.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2005Dialect");

        s.put("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver");  

        s.put("hibernate.connection.url", "jdbc:sqlserver://localhost:1433;databaseName=db");  

        s.put("hibernate.connection.username", "sa");

        s.put("hibernate.connection.password", "fengze");  

        s.put("hibernate.hbm2ddl.auto","update");

        

        //Configuration cfg = new Configuration().setProperties(m);

        Configuration cfg = new Configuration().setProperties(s);

        cfg.addAnnotatedClass(com.entity.Student.class);

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();

        SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);

        Session session = sf.openSession();  



        //插入数据

        Transaction tx = session.beginTransaction();

        Student s1 = new Student();

        s1.setName("张三丰");

        session.save(s1);

        tx.commit();

        

        List<Student> stu = session.createQuery("from Student").list();

        for(Student st : stu){

            System.out.println(st.getName());

        }

        session.close();  

        sf.close();  

    }

}

 

你可能感兴趣的:(Hibernate)