码迷,mamicode.com
首页 > Web开发 > 详细

hibernate之初学项目搭建

时间:2017-06-24 20:47:53      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:source   文件中   drive   ima   根据   实体类   tostring   nbsp   junit   

首先是项目的jar包

以下是hibernate一般会用到的jar包,要注意jar包的版本,不然开发过程会很难受

技术分享

然后是配置文件:

先是在src下创建一个hibernate.cfg.xml

<!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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/webproject</property><!--
                反正下面这段话我直接跑去hibernate.dialect包下复制文件路径的时候gg了要指定特定的方言
        --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property><!--
                如果不加下面这句话,引入你自己写的配置文件,你将会很绝望
        --><mapping resource="com/xinzhi/dao/UserEntity.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

然后是在你写的当前包下创建第二个配置文件UserEntity.hbm.xml(命名方式:实体类名.hbm.xml),记得要映射到hibernate的配置文件中去

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping 
    package="com.xinzhi.dao">
    
    <class name="UserEntity"  table="user"><!--
        这里是主键,name是你实体类中的主键名称 , column是你数据库中字段的主键
        --><id name="id" column="id">
            <generator class="native"/>
        </id><!--
        下面的分别是实体类中的属性对应数据库中的字段
        --><property name="username" column="username"></property>
        <property name="pwd" column="pwd"></property>
    </class>

</hibernate-mapping>

下面是我测试用的实体类:有的人会在实体类上@Entity或者让实体类implements Serilizable,但是目前我没加这些测试也没问题,应该是版本问题,如果出现Unkown Entity的错误再加上也不迟

package com.xinzhi.dao;

public class UserEntity {
    private int id;
    private String username;
    private String pwd;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "UserEntity [id=" + id + ", pwd=" + pwd + ", username="
                + username + "]";
    }

}

最后就是测试类了,hibernate的api不是很多用到的,如果你习惯了一种的就一直用这种就好下面是我的测试类,后面我也会更新我学到的新的方式

package com.xinzhi.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

public class TestHibernate {
    static SessionFactory sf;
    static {
        /**
         * 通过配置组来创建session工厂,因为这个工厂一般只要创建一次
         */
        sf = new Configuration().configure().buildSessionFactory();
    }

    @Test
    public void testname() throws Exception {
        /**
         * 查询所有用户
         */
        // 开启一个会话
        Session session = sf.openSession();
        // 通过会话开启一个交易或者说一个事务
        Transaction transaction = session.beginTransaction();
        // 根据主键查询
        // UserEntity user = (UserEntity) session.get(UserEntity.class, 1);
        // 获取一个查询集
        Query q = session.createQuery("from UserEntity where id=1 or id=2 ");

        List<UserEntity> list = q.list();
        System.out.println(list);
        // 提交交易/事务
        transaction.commit();
        // 关闭会话
        session.close();
    }
}

 

hibernate之初学项目搭建

标签:source   文件中   drive   ima   根据   实体类   tostring   nbsp   junit   

原文地址:http://www.cnblogs.com/ShaoXin/p/7074183.html

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