hibernate5 —— hello world

最近在学习hibernate5,经历了比较痛苦的入门,踩了不少坑。好在,结果还是搞出了自己的hello hibernate.


开发环境:eclipse+jdk8
hibernate JAR包:hibernate-release-5.2.5.Final

  1. MySQL表脚本
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET gb2312 NOT NULL,
  `password` varchar(20) NOT NULL,
  `sex` varchar(2) CHARACTER SET gb2312 DEFAULT NULL,
  `email` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
  1. 数据模型 Person.java
    Note:每个属性都要有getter, setter
package hiber;

public class Person {

     private String id ;
     private String name ;
     private String password ;
     private String sex ;
     private String email ;

     public String getEmail() {
      return email;
     }
     public void setEmail(String email) {
      this.email = email;
     }
     public String getId() {
      return id;
     }
     public void setId(String id) {
      this.id = id;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getPassword() {
      return password;
     }
     public void setPassword(String password) {
      this.password = password;
     }
     public String getSex() {
      return sex;
     }
     public void setSex(String sex) {
      this.sex = sex;
     }

     public String toString(){
         return getId() + ":" 
                    + getName() +":"
                        + getEmail() +":"
                                + getSex()  +":";
     }
}
  1. 数据模型与表字段mapping,person.hbm.xml
    Note: generator要注意,这里踩了不少坑。
    参考:http://ryxxlong.iteye.com/blog/612446
    http://blog.csdn.net/huaweitman/article/details/39025553






<hibernate-mapping package="hiber">
    <class name="Person" table="PERSON">
       <id name="id" column="ID" type="string">
             <generator ***class="native"*** /> 
        id>
        <property name="name" column="NAME" type="string"  not-null="true" />
        <property name="password" column="PASSWORD" type="string"  not-null="true" />
        <property name="sex" column="SEX" type="string" />
        <property name="email" column="EMAIL" type="string" />
    class>
hibernate-mapping>
  1. hibernate.cfg.xml配置
    Note:记得把mysql驱动JAR加入编译路径;




<hibernate-configuration>

<session-factory>
 <property name="myeclipse.connection.profile">mysqlproperty>
 <property name="connection.url">jdbc:mysql://localhost:3306/testproperty>
 <property name="connection.username">rootproperty>
 <property name="connection.password">testproperty>
 <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
 <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>
 <property name="show_sql">trueproperty>
 **<mapping resource="person.hbm.xml" />**
session-factory>

hibernate-configuration>
  1. 数据操作类 PersonOperate.java
package hiber;


import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
public class PersonOperate {

    private Session session = null;

    public PersonOperate(){
        //get hibernate config
        Configuration configuration = new Configuration().configure();
        // get sessionFactory
        SessionFactory factory = configuration.buildSessionFactory();
        // get a ssesion
        this.session = factory.openSession();   
    }

    public void insert(Person person){
        Transaction transaction = this.session.beginTransaction();
        this.session.save(person);
        transaction.commit();
//      this.session.close();
    }

    public List query(){
        List persons = null;
        @SuppressWarnings("unchecked")
        Query query = this.session.createQuery("FROM Person");
        persons = query.getResultList();
        return persons;
    }
}
  1. 测试类 Test.java
package hiber;

import java.util.List;

public class test {

    public static void main(String[] args){

        PersonOperate operate = new PersonOperate();

        for(int i=1; i<11; i++){
            Person person = new Person();
            person.setEmail("[email protected]");
    //      person.setId("");   //db 自增主键
            person.setName("danny"+i);
            person.setPassword("pwd");
            person.setSex("m");
            operate.insert(person);
        }
        List persons;

        persons = operate.query();
        System.out.println(persons.size());
        for(Person person : persons){
            System.out.println(person.toString());
        }
    }

}

你可能感兴趣的:(Web)