标签:
首先,
1,创建Project,然后在library中加入hibernate所需的包。
hibernate.core
/required
slf-nop.jar
2,引入mysql的JDBC驱动包
3,在mysql中建立对应的数据库以及表
a)create database hibernate;
b)use hibernate;
c)create table student (id int primary key auto_increment, name varchar(20), age int);
4,建立Hibernate配置文件hibernate.cfg.xml
a)从参考文档的index.xml中参考
b)修改对应的数据库连接信息
c)注释掉不需要的内容
<?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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">111111</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.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="com/bjsxt/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration
>
5,建立Student类
package com.bjsxt.hibernate.model;
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;
}
}
6,建立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="com.bjsxt.hibernate.model">
<class name="Student" table="student">
<id name="id" column="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping
>
见参考文档api
7,将映射文件加入到hibernate.cfg.xml中,
8,建立StudentTest类,
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.bjsxt.hibernate.model.Student;
public class StudentTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.setId(1);
s.setAge(1);
s.setName("s1");
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();;
session.close();
sf.close();
}
}
搭建完成。
执行后
Hibernate: insert into student (name, age, id) values (?, ?, ?)
在mysql中查询,已经插入数据。
标签:
原文地址:http://blog.csdn.net/yuchao1076244056/article/details/51347916