使用两种方法来实现将数据插入数据库的程序
第一种方法:传统的hibernate
首先此程序需要的jar包为:
1、数据库端的操作
创建数据库语句:
create database hibernate character set utf8;
创建表:
use hibernate;
create table student(id int primary key,name varchar(20),age int);
2、hibernate连接数据库的配置文件hibernate.cfg.xml,存放在src目录下。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</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/bjxst/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、封装数据库字段的类 Student.java
package com.bjxst.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
4、建立Student.java封装的字段与数据库表字段的映射文件 hibernate.hbm.xml(存放在与Student.java同一目录下)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bjxst.hibernate.model">
<class name="Student" table="student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
5、最后编写测试程序StudentTest.java(存放在src下默认的包中)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.bjxst.hibernate.model.Student;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
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();
}
}
编写完后,运行StudentTest.java,运行结果为:Hibernate: insert into student (name, age, id) values (?, ?, ?)
此时查看数据库中,已存在添加的记录:
此时,一个简单的hibernate连接mysql的程序已创建成功。
第二种方法:使用hibernate-annotation
需要的jar包:
1、数据库端的操作
创建数据库:
create database hibernate character set utf8;
建表:
use hibernate;
create table student(id int primary key,name varchar(20),age int);
2、编写配置文件:
hibernate.cfg.xml(存放在src目录下)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</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/bjxst/hibernate/model/Student.hbm.xml"/>
<mapping class="com.bjxst.hibernate.model.Teacher"/>
</session-factory>
</hibernate-configuration>
3、封装数据库字段的类Teacher.java
package com.bjxst.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Teacher {
private int id;
private String name;
private String title;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
4、编写测试程序TeacherTest.java(存放在src默认的包中)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.bjxst.hibernate.model.Student;
import com.bjxst.hibernate.model.Teacher;
public class TeacherTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher t=new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("初级");
Configuration cfg=new AnnotationConfiguration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
运行测试程序,查看数据库结果,插入成功。