在使用Hibernate框架进行基础的配置
1.导入jar包
2.引入配置文件Hibernate.cfg.xml 该配置文件配置了连接数据库的基本配置
可选配置: hibernate.hbm2ddl.auto
none:不使用hibernate自动建表
create:如果数据库中已有表,删除原表,重新创建,如果没有表,创建新表
create-drop 如果已经有表,删除原表,执行操作,再删除这个表 如果没有表,新建表,使用后删除
当把sessionFactory close()后生效 一般做测试用
update 如果有表,使用原表 如果没有,重新建表
validate 检验映射结构和表结构是否一致,不一致报错
<!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:///hibernate?useUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置Hibernate的方言 -->
<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="Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.配置 类名.hbm.xml 即与数据库的映射文件
class标签:作用建立类与表的映射关系
属性:
name:类的全路径
table:表名
ID标签:作用建立类中的属性与表中的主键对应关系
属性:
name:类中的属性
column:表中主键字段
property标签:建立类中普通属性与表的对应关系
name:类中属性
column:表中字段
<?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.bullet.domain.Customer" table="customer">
<!--建立类中的属性与表中的主键对应-->
<id name="cust_id" column="cust_id" >
<generator class="native"/>
</id>
<!--建立类中的普通的属性和表的字段的对应-->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
<generator 主键生成策略>
increment: 自动增长策略 long int short 在单线程中使用
identity: 自动增长,使用数据库底层的增长策略,适用于有自动增长机制的数据库(MySQL支持 Oracle不支持)
sequence:自动增长,采用序列增长方式,必须得要支持序列的数据库(Oracle支持 MySQL不支持)
UUID
native:主键自动增长,根据数据库的不同自动切换策略
assigned:手动生成来管理主键
POJO类:
package com.bullet.domain;
import lombok.Getter;
import lombok.Setter;
@Setter@Getter
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
}
测试类:
package com.bullet.test;
import com.bullet.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test1(){
//1.加载配置文件
Configuration configure = new Configuration().configure();
//2.创建sessionFactory
SessionFactory sessionFactory = configure.buildSessionFactory();
//获取session
Session session = sessionFactory.openSession();
Customer customer = new Customer();
customer.setCust_name("LeBron");
customer.setCust_level("1");
//保存
session.save(customer);
//释放资源
session.close();
sessionFactory.close();
}
}
可能出现的问题,在测试时,控制台打印Unknown initial character set index '255' received from server. Initial client character ,原因是MySQL驱动和数据库字符集设置不搭配
因此要在url后面配置,加上 ?useUnicode=true&characterEncoding=utf8
如果是在xml中配置 则&要改用转义字符 &;