如何创建第一个hibernate项目
1.创建项目
2.导包
3.写配置文件
4.创建实体类和数据库
5.创建测试类
以上是需要导入的包
创建一个Customer.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.tz.domain.Customer" table="cst_customer"> <!-- 复制Customer完整的类名到name,把表名填到table -->
<id name="cust_id" column="cust_id"> <!-- 把表中的cust_id复制到name,column列名也是这个 -->
<generator class="native"></generator> <!-- class中填写native先不介绍 -->
</id>
<property name="cust_name" column="cust_name"></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>
再创建一个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>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 驱动 -->
<property name="hibernate.connection.url">jdbc:mysql:///nice</property> <!-- 地址 三个斜杠可以省略localhost-->
<property name="hibernate.connection.username">root</property> <!-- 用户名 -->
<property name="hibernate.connection.password">nice</property> <!-- 密码 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 数据库方言
不同的数据库中,sql语句略有区别,指定方言可以让hibernate框架生成sql语句时,针对数据库的方言生成
sql语句都基于sql99标准
DDL:定义语言 数据库表的增删改查
DCL: 控制语言 事务 权限
DML: 操作语言 增删该查
注意事项:mysql在选择方言时,请选择最短的方言 hibernate.dialect org.hibernate.dialect.MySQLDialect
-->
<property name="hibernate.show_sql">true</property> <!-- 把hibernate生成的sql语句显示在控制台上 -->
<property name="hibernate.format_sql">true</property> <!-- 把打印的sql语句格式化 不然sql语句都显示在一行上-->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/tz/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建测试类
package com.tz.test.a_hello;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.tz.domain.Customer;
//测试Hibernate框架
public class Demo {
@Test
//保存客户
public void fun1(){
Configuration conf = new Configuration().configure();
SessionFactory sessionFactory = conf.bulidSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//-----华丽的分割线-----
Customer c = new Customer();
c.setCust_name("潭州教育");
session.save(c);//执行保存
//-----华丽的分割线-----
tx.commit();
session.close();
sessionFactory.close();
}
}
创建实体类
package com.tz.domain;
public class Customer {
/**
* CREATE TABLE `cst_customer` (
`cust_id` bigint(20) NOT NULL AUTO_INCREMENT,
`cust_name` varchar(255) DEFAULT NULL,
`cust_source` varchar(255) DEFAULT NULL,
`cust_industry` varchar(255) DEFAULT NULL,
`cust_level` varchar(255) DEFAULT NULL,
`cust_linkman` varchar(255) DEFAULT NULL,
`cust_phone` varchar(255) DEFAULT NULL,
`cust_moblie` varchar(255) DEFAULT NULL,
`cust_mobile` varchar(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;
*/
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_moblie;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_moblie() {
return cust_moblie;
}
public void setCust_moblie(String cust_moblie) {
this.cust_moblie = cust_moblie;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
}
}