创建第一个Hibernate程序(Hibernate基本配置)

创建第一个Hibernate应用

使用工具:

  • eclipse
  • jdk1.7
  • maven
  • *tomcat7.0

项目结构

这里写图片描述

User.java

public class User implements Serializable {

    private int id;
    private String name;
    private String password;
    private Date birthday;
    private String status;

    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 Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", password=" + password + ", birthday=" + birthday + ", status="
                + status + "]";
    }


}

User.hbm.xml

<?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 package="com.hibernate.pojo">
    <class name="User" table="user" >
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name" length="20"></property>
        <property name="password" column="password" length="50"></property>
        <property name="status"  column="status" length="1"></property>
        <property name="birthday" column="birthday" ></property>
    </class>
</hibernate-mapping>

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>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 连接数据库 -->
        <property name="hibernate.connection.url">jdbc:mysql://localhost/mydb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"> root</property>
        <!-- 可以将向数据库发送的SQL语句显示出来 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 配置hibernate的映射文件所在的位置 -->
        <mapping resource="com/hibernate/pojo/User.hbm.xml" />
    </session-factory>
</hibernate-configuration> 

测试代码

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Date date = new Date();
        User user = new User();
        user.setName("han");
        user.setPassword("123456");
        user.setStatus("A");
        user.setBirthday(date);

        Configuration config = new Configuration().configure(); // Hibernate框架加载hibernate.cfg.xml文件
        SessionFactory sessionFactory = config.buildSessionFactory();
        Session session = sessionFactory.openSession();

        //开启事务
        session.beginTransaction();
        //HQL查询  分页
        List reList  = session.createQuery("from User where id = ?").setInteger(0, 1).list();
        System.out.println(reList.size());
        System.out.println(reList.get(0).toString());
        System.out.println("-----------------------------------");
        //QBC 查询 分页
        Criteria Qlist = session.createCriteria(User.class);
        Qlist.setFirstResult(1);
        Qlist.setMaxResults(2);

        List<User> U = Qlist.list();
        for(User u : U){
            System.out.println(u);
        }
        //事务提交
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }


    public static Date str2Date(String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(str);
            return date;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

}

查询结果

Hibernate: select user0_.id as id1_0_, user0_.name as name2_0_, user0_.password as password3_0_, user0_.status as status4_0_, user0_.birthday as birthday5_0_ from user user0_ where user0_.id=?
1
User [id=1, name=hanzhikadasdasd, password=这是U1的密码, birthday=2018-06-13 11:09:57.0, status=A]
-----------------------------------
Hibernate: select this_.id as id1_0_0_, this_.name as name2_0_0_, this_.password as password3_0_0_, this_.status as status4_0_0_, this_.birthday as birthday5_0_0_ from user this_ limit ?, ?
User [id=12, name=YYY, password=123456, birthday=2018-06-13 11:03:08.0, status=A]
User [id=13, name=hanzhikadasdasd, password=123456, birthday=2018-06-13 11:10:24.0, status=A]

自己写的,如果有问题欢迎指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值