1.先配置好文件hibernate.cfg.xml文件。
<?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>
<!-- 配置连接信息 -->
<!-- hibernate.connection.driver_class jdbc驱动类
hibernate.connection.url jdbc URL
hibernate.connection.username 数据库用户
hibernate.connection.password 数据库用户密码
hibernate.connection.pool_size 连接池容量上限数目
hibernate.dialect org.hibernate.dialect.MySQLDialect 数据库方言 -->
<session-factory>
<!-- 必选配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/数据库名称</property>
<property name="hibernate.connection.username">数据库账号</property>
<property name="hibernate.connection.password">数据库密码</property>
<!-- 数据库方言 使用的数据库类型 -->
<property name="hibernate.dialect org.hibernate.dialect.MySQLDialect"></property>
<!-- 可选配置 -->
<!-- 显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 配置c3p0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 最大空闲连接120秒 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 自动建表 删除,创建,删除:creat-drop 创建:creat 如果没有表就创建,有就同步:update(一般使用) 验证类和数据表结构是否一致:validate-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件 -->
<mapping resource="model/Student.hbm.xml"/>
<!-- 映射文件 -->
</session-factory>
</hibernate-configuration>
2.model包Student.hbm.xml映射文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- javabean与表之间的对应关系 -->
<class name="model.Student" table="student">
<!-- 主键对应 -->
<id name="stu_Id" column="stuID">
<!-- 主键策略(与自增长相关) -->
<generator class="native"></generator>
</id>
<!-- 其他字段 -->
<property name="stu_Name" column="stuName"></property>
<property name="stu_Address" column="stuAddress"></property>
</class>
</hibernate-mapping>
3.model包Student类。
package model;
public class Student {
private long stu_Id;
private String stu_Name;
private String stu_Address;
public long getStu_Id() {
return stu_Id;
}
public void setStu_Id(long stu_Id) {
this.stu_Id = stu_Id;
}
public String getStu_Name() {
return stu_Name;
}
public void setStu_Name(String stu_Name) {
this.stu_Name = stu_Name;
}
public String getStu_Address() {
return stu_Address;
}
public void setStu_Address(String stu_Address) {
this.stu_Address = stu_Address;
}
@Override
public String toString() {
return "Stduent [stu_Id=" + stu_Id + ", stu_Name=" + stu_Name + ", stu_Address=" + stu_Address + "]";
}
}
4.util包的HibernateUtils类(创建SessionFactory)
package util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
//使用一个静态块创建SessionFactory
private static Configuration CONFIG;
private static SessionFactory FACTORY;
static {
CONFIG = new Configuration().configure();
FACTORY = CONFIG.buildSessionFactory();
}
//从连接池中获取连接
public static Session openSession() {
return FACTORY.openSession();
}
}
5.log4j.properties文件配置,控制台输出。
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:\\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
###info or off stdout or file
log4j.rootLogger=info, stdout
6.test包TestStudent1类。增删改查
package test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import model.Student;
import util.HibernateUtils;
public class TestStudent1 {
Session session;
Transaction tr;
@Before
public void init() {
session = HibernateUtils.openSession();
tr = session.beginTransaction();
}
/*
* get
* */
@Test
public void get() {
//第一个参数是javabean类,第二个参数是数据表的id值
Student s = session.get(Student.class, 10001L);
System.out.println(s);
}
/*
* save
**/
@Test
public void save() {
//第一个参数是javabean类,第二个参数是数据表的id值
Student s = new Student();
s.setStu_Name("阿啦啦");
session.save(s);
}
/*
* update
**/
@Test
public void update() {
//第一个参数是javabean类,第二个参数是数据表的id值
Student s = session.get(Student.class, 10001L);
s.setStu_Name("阿卡里");
session.update(s);
}
/*
* delete
**/
@Test
public void delete() {
//第一个参数是javabean类,第二个参数是数据表的id值
Student s = session.get(Student.class, 10005L);
session.delete(s);
}
/*
* creatQuery
**/
@After
public void destory() {
tr.commit();
session.close();
}
}
正在学习hibernate,有什么错误请大家指出。
需要引入的jar包。