首先是hibernate.cfg.xml配置,该文件放在src目录下
<!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://192.168.1.81:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate‘s automatic session conttext 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 show -->
<property name="show_sql">true</property>
<!-- Format SQL -->
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.java 文件
package com.bjsxt.hibernate;
public class Student {
private String uname;
private int age;
private String sex;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Student.hbm.xml的配置
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Student" table="userinfo">
<id name="uname"></id>
<property name="age"/>
<property name="sex"/>
</class>
</hibernate-mapping>
测试的Test文件
package com.bjsxt.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class Test {
public static void main(String args[]) {
query();
}
// 插入数据
public static void insert() {
Student s = new Student();
s.setUname("xiaoming");
s.setSex("女");
s.setAge(13);
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
}
// 删除数据
public static void delete() {
Student s = new Student();
s.setUname("xiaoming");
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.delete(s);
;
session.getTransaction().commit();
}
// 修改数据
public static void updata() {
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s = (Student) session.get(Student.class, "xiaoming");
if (s != null) {
s.setSex("女");
s.setAge(100);
session.update(s);
;
session.getTransaction().commit();
}
}
// 查询数据
public static void query() {
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Query query = session
.createQuery("from Student where uname=‘小明‘ ");
List<Student> list = query.list();
for(Student s:list){
System.out.println(s.getAge()+"\n");
}
}
}