使用Hibernate框架的demo--打印数据库信

来源:互联网 发布:德州扑克 技巧 知乎 编辑:程序博客网 时间:2024/05/22 09:44

工程介绍:打印数据库信息到控制栏

  1. 新建java项目名:hibernateDemo001,建立下图结构,将Hibernate中java8和required文件夹下的包导入项目
    图片描述

Student.java的代码为:

package com.feng.dao;public class Student {    @Override    public String toString() {        // TODO Auto-generated method stub        return "\r id:"+id+"\r name:"+name+"\r sex:"+sex+"\r birthday:"+birthday+"\r state:"+state;    }    private Integer id; //id    private String name; //name    private String sex; //sex    private String birthday; //birthday    private Integer state;//state//  private Integer a;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getBirthday() {        return birthday;    }    public void setBirthday(String birthday) {        this.birthday = birthday;    }    public Integer getState() {        return state;    }    public void setState(Integer state) {        this.state = state;    }}

student.hbm.xml代码为:

<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.feng.dao">    <class name="Student" table="student">        <id name="id" type="java.lang.Integer" column="id" >            <generator class="identity" />        </id>        <property name="name" type="java.lang.String" column="name" insert="false"/>        <property name="sex" type="java.lang.String" column="sex"></property>        <property name="birthday" type="java.lang.String" column="birthday"></property>        <property name="state" type="java.lang.Integer" column="state"></property>    </class></hibernate-mapping>

hibernate.cfg.xml

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:     GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the     lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --><!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="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/student</property>        <property name="connection.username">root</property>        <property name="connection.password"></property>        <property name="connection.characterEncoding">utf8</property>        <property name="connection.pool_siez">2</property>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <mapping resource="com/feng/dao/student.hbm.xml" />    </session-factory></hibernate-configuration>

Test.java

package com.feng.test;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import com.feng.dao.Student;public class Test {    public static void main(String[] args) {        // TODO Auto-generated method stub        // 1.进行配置文件的读取        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();        StandardServiceRegistry sr = ssrb.configure().build();        // 2.由config得到一个会话工厂//得到sessionFactory        SessionFactory sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();        //3.由sessionFacatory得到session        Session session = sf.openSession();        //循环打印出来        List<Student> list = session.createQuery("from Student").list();        for (Student a : list) {            System.out.println(a);        }        //4.记得关闭session        session.close();        sf.close();    }}

作者: 我有一双明亮的眼睛 
链接:http://www.imooc.com/article/13296
来源:慕课网
0 0
原创粉丝点击