1.创建项目的时候勾选Hibernate,这里第三布一种是可以选择download从网上下载,也可以自己从网上下载hibernate的源码,然后把其lib/required的包都导入到intellij中,可以看到两种方法导入的包是一样的,只不过一种从网上下载,一种直接本地添加
可以看到idea 为我们生成了一个hibernate配置文件
2.在WEB-INF下新建两个文件夹lib 和classes,将工程所需要的jar包都放到lib目录下
3. 更改output路径和添加依赖
3.配置数据库源
[图片上传中...(image-741903-1510642137647-4)]
之后点击可显示数据库相关信息
接下来根据相应的表来建立相应的映射类及hbm映射配置文件。
生成的CstCustomer.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>
<class name="com.gentleni.domain.CstCustomer" table="cst_customer" schema="hibernate_demo">
<id name="custId">
<column name="cust_id" sql-type="bigint(32)"/>
</id>
<property name="custName">
<column name="cust_name" sql-type="varchar(32)" length="32"/>
</property>
<property name="custUserId">
<column name="cust_user_id" sql-type="bigint(32)" not-null="true"/>
</property>
<property name="custCreateId">
<column name="cust_create_id" sql-type="bigint(32)" not-null="true"/>
</property>
<property name="custSource">
<column name="cust_source" sql-type="varchar(32)" length="32" not-null="true"/>
</property>
<property name="custIndustry">
<column name="cust_industry" sql-type="varchar(32)" length="32" not-null="true"/>
</property>
<property name="custLevel">
<column name="cust_level" sql-type="varchar(32)" length="32" not-null="true"/>
</property>
<property name="custLinkman">
<column name="cust_linkman" sql-type="varchar(64)" length="64" not-null="true"/>
</property>
<property name="custPhone">
<column name="cust_phone" sql-type="varchar(64)" length="64" not-null="true"/>
</property>
<property name="custMobile">
<column name="cust_mobile" sql-type="varchar(16)" length="16" not-null="true"/>
</property>
</class>
</hibernate-mapping>
修改 hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--配置数据库连接-->
<!--比说明字符编码为utf-8-->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_demo?useSSL=false&useUnicode=true&characterEncoding=utf-8</property>
<!--若驱动导入报错,请检查是否倒入了相关数据库连接的Jar包-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!--相关的属性-->
<property name="hbm2ddl.auto">update</property>
<!--格式化控制台显示的MySQL语句-->
<property name="format_sql">true</property>
<!--在控制台显示执行的MySQL语句-->
<property name="show_sql">true</property>
<!--使用MySQL数据库的方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--相关的映射-->
<mapping resource="com/gentleni/domain/CstCustomer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试代码
public class Test1 {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void Init() {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@Test
public void test() {
CstCustomer cstCustomer = new CstCustomer();
cstCustomer.setCustName("张三");
session.save(cstCustomer);
}
@After
public void destroy() {
transaction.commit();
session.close();
sessionFactory.close();
}
}
控制台输出
数据添加成功