第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

使用Hibernate框架的demo--打印數(shù)據(jù)庫信息

標(biāo)簽:
Java

版本信息:hibernate-release-5.1.0.Final
使用数据库:mysql
工程介绍:打印数据库信息到控制栏

  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();
    }

}
點(diǎn)擊查看更多內(nèi)容
6人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消