01-下载Hibernate的压缩包。
02-导入数据库驱动包 + log4j日志包 + hibernate压缩包的required文件夹下面的jar包
03-导入log4j.properties文件
### set log levels ###
log4j.rootLogger = INFO , console , debug , error
### console ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### log file ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = ../logs/springmvc-demo.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = INFO
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### exception ###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = ../logs/springmvc-demo_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
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{ISO8601} %l %c%n%p: %m%n
### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout
04-创建一个名为hib01的空数据库
05-准备实体类(JavaBean)
public class Customer implements Serializable{
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
// setter and getter 略
}
06-编写映射配置文件:名称:实体类名.hbm.xml, 位置:建议与JavaBean在同一个包
<?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>
<!-- 配置实体类与表结构的映射 -->
<class name="com.hngy.domain.Customer" table="cst_customer">
<!-- 配置主键 -->
<id name="cust_id" column="cust_id">
<!-- 配置主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 配置其他属性 -->
<property name="cust_name" column="cust_name"></property>
<property name="cust_user_id" column="cust_user_id"></property>
<property name="cust_create_id" column="cust_create_id"></property>
<property name="cust_source" column="cust_source"></property>
<property name="cust_industry" column="cust_industry"></property>
<property name="cust_level" column="cust_level"></property>
<property name="cust_linkman" column="cust_linkman"></property>
<property name="cust_phone" column="cust_phone"></property>
<property name="cust_mobile" column="cust_mobile"></property>
</class>
</hibernate-mapping>
07-编写核心配置文件:名称:hibernate.cfg.xml, 位置:建议在Src下
<?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>标签 -->
<session-factory>
<!-- 一、必须配置:4大参数 + 方言 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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>
<!-- 修改 (若无表,则创建;若有表,则修改) -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 三、引入映射配置文件 -->
<mapping resource="com/hngy/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
08-编写hibernateUtils工具类,简化一下crud操作
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate框架的工具类
* @author Administrator
*/
public class HibernateUtils {
private static final Configuration CONFIG;
private static final SessionFactory FACTORY;
// 编写静态代码块
static{
// 加载XML的配置文件
CONFIG = new Configuration().configure();
// 构造工厂
FACTORY = CONFIG.buildSessionFactory();
}
/**
* 从工厂中获取Session对象
* @return
*/
public static Session getSession(){
return FACTORY.openSession();
}
}
09-编写测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.hngy.domain.Customer;
import com.hngy.utils.HibernateUtils;
public class HibTest {
/**
* 测试2:使用工具类,添加数据
*/
@Test
public void testAddByUtils(){
// 01-创建Session对象
Session session = HibernateUtils.getSession();
// 02-开启事务
Transaction tr = session.beginTransaction();
// 03-编写crud代码
Customer c = new Customer();
c.setCust_name("周其");
c.setCust_level("★★");
// 保存
session.save(c);
// 04-提交事务
tr.commit();
// 05-释放资源
session.close();
}
/**
* 测试1:添加数据
*/
@Test
public void testAdd(){
// 01-加载配置文件
Configuration config = new Configuration();
config.configure();// 默认加载src下的hibernate.cfg.xml
// 02-创建SessionFactory对象
SessionFactory factory = config.buildSessionFactory();
// 03-创建Session对象
Session session = factory.openSession();
// 04-开启事务
Transaction tr = session.beginTransaction();
// 05-编写crud代码
Customer c = new Customer();
c.setCust_name("李四");
c.setCust_level("★");
// 保存
session.save(c);
// 06-提交事务
tr.commit();
// 07-释放资源
session.close();
factory.close();
}
}