码迷,mamicode.com
首页 > 数据库 > 详细

[Hibernate] - mysql

时间:2014-05-06 09:31:29      阅读:454      评论:0      收藏:0      [点我收藏+]

标签:sql   mysql   ber   mysq   nat   hibernate   

Hibernate使用mysql例子:

1) 新建一个bean: User.java

bubuko.com,布布扣
package com.my.bean;

import java.util.Date;

public class User {
    
    private int userid;
    private String username;
    private String password;
    private char status;
    private int isBuild;
    private Date createTime;
    private Date lastUpdateTime;
    
    public int getUserID() {
        return userid;
    }
    public void setUserID(int userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public char getStatus() {
        return status;
    }
    public void setStatus(char status) {
        this.status = status;
    }
    public int getIsBuild() {
        return isBuild;
    }
    public void setIsBuild(int isBuild) {
        this.isBuild = isBuild;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public Date getLastUpdateTime() {
        return lastUpdateTime;
    }
    public void setLastUpdateTime(Date lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

}
bubuko.com,布布扣

 

2) 新建一个hibernate mapping xml file: User.hbm.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="com.my.bean.User" table="sys_user">
        <id name="UserID" type="int">
            <column name="UserID" />
            <generator class="assigned" />
        </id>
        <property name="Username" type="java.lang.String" length="50">
            <column name="Username" />
        </property>
        <property name="Password" type="java.lang.String" length="50">
            <column name="Password" />
        </property>
        <property name="Status" type="char" length="1">
            <column name="Status" />
        </property>
        <property name="IsBuild" type="int">
            <column name="IsBuild" />
        </property>
        <property name="CreateTime" type="java.util.Date">
            <column name="CreateTime" />
        </property>
        <property name="LastUpdateTime" type="java.util.Date">
            <column name="LastUpdateTime" />
        </property>
    </class>

</hibernate-mapping>
bubuko.com,布布扣

 

3)新建一个hibernate config file: hibernate.cfg.xml

bubuko.com,布布扣
<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1/obs</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate‘s automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/my/hbm/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
bubuko.com,布布扣

 

4)新建一个类:HibernateUtil.java

bubuko.com,布布扣
package com.my.dao.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration configuration = new Configuration();
            return configuration.configure().buildSessionFactory(
                    new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}
bubuko.com,布布扣

这是hibernate 4的sessionFactory方法。

 

5)测试:

bubuko.com,布布扣
package com.my.test;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.my.bean.User;
import com.my.dao.util.HibernateUtil;

public class ConsoleTest {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        String hqlSelect = "from User where UserID=:UserID";
        Query query = session.createQuery(hqlSelect);
        query.setParameter("UserID", 1);
        List<User> users = query.list();
        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();

        for (User user : users) {
            System.out.println("User name: " + user.getUsername());
        }
    }

}
bubuko.com,布布扣

 

[Hibernate] - mysql,布布扣,bubuko.com

[Hibernate] - mysql

标签:sql   mysql   ber   mysq   nat   hibernate   

原文地址:http://www.cnblogs.com/HD/p/3708936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!