最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

hibernate5.2的基本配置方法(詳解)

 更新時(shí)間:2017年06月09日 08:23:09   投稿:jingxian  
下面小編就為大家?guī)硪黄猦ibernate5.2的基本配置方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

目標(biāo):將Student實(shí)體對(duì)象加入數(shù)據(jù)庫

1、首先需要下載三個(gè)東西:hibernate,slf4j,mysql。

2、分別取他們的包導(dǎo)入新建的項(xiàng)目中,我這里的版本是:hibernate-release-5.2.10里面lib目錄下的required中的全部文件 slf4j-1.7.25下的受slf4j-nop-1.7.25.jar mysql的mysql-connector-java-5.1.42-bin.jar

3、在src下配置hibernate.cfg.xml(建議直接去文檔復(fù)制然后改)

<?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://localhost/text02</property>
<property name="connection.username">root</property>
<property name="connection.password">6530033197</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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="student/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

4、在mysql中創(chuàng)建student表,字段:id age name

5、創(chuàng)建自己的實(shí)體類在src下建包student,然后建Class:Student.java

package student;

public class Student {
private int id;
private int age;
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}

public Student() {
// TODO Auto-generated constructor stub
}

}

6、在對(duì)應(yīng)package即student下配置文件:Student.hbm.xml

<?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="student">
<class name="Student" table="student">
<id name="id" column="id">
</id>
<property name="name" type="string" column="name"/>
<property name="age" type="int" column="age"/>
</class>
</hibernate-mapping>

7、創(chuàng)建測(cè)試類:StudentText.java

package student;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class StudentText {

public static void main(String[] args) {
Student stu = new Student();
stu.setId(4);
stu.setName("小明");
stu.setAge(12);

Configuration con = new Configuration();
SessionFactory sf = con.configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(stu);
s.getTransaction().commit();
s.close();
sf.close();

}

}

輸出結(jié)果,完成:

以上這篇hibernate5.2的基本配置方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

探索| 河源市| 南投县| 区。| 金乡县| 墨江| 阿瓦提县| 武威市| 新干县| 佛教| 屏边| 涟源市| 苍山县| 蒙山县| 内江市| 常宁市| 富锦市| 虹口区| 岚皋县| 社旗县| 泽州县| 安西县| 南平市| 富源县| 运城市| 扶风县| 烟台市| 德令哈市| 平泉县| 大厂| 泰来县| 中宁县| 疏附县| 托里县| 探索| 晋城| 宁都县| 正宁县| 察雅县| 班戈县| 洛浦县|