hibernate01
hibernate01
1 创建数据表
CREATE TABLE `m_user` (
`user_id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`address` varchar(60) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`groupp` varchar(50) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1135 DEFAULT CHARSET=utf8;
创建实体类
package com.xmetc.hibernate.zyz;
public class MUser {
private Integer user_id;
private String username;
private String password;
private String address;
private Integer age;
private String sex;
private String groupp;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGroupp() {
return groupp;
}
public void setGroupp(String groupp) {
this.groupp = groupp;
}
public MUser(Integer user_id, String username, String password, String address, Integer age, String sex,
String groupp) {
super();
this.user_id = user_id;
this.username = username;
this.password = password;
this.address = address;
this.age = age;
this.sex = sex;
this.groupp = groupp;
}
@Override
public String toString() {
return "MUser [user_id=" + user_id + ", username=" + username + ", password=" + password + ", address="
+ address + ", age=" + age + ", sex=" + sex + ", groupp=" + groupp + "]";
}
}
3创建映射
映射需要通过xml的配置文件来完成,这个配置文件可以随意命名。尽量统一命名规范(类名。hbr。xml)
约束头文件 /org/hibernate/hibernate-mapping-3.0.dtd
Muser.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.xmetc.hibernate.zyz.MUser" table="m_user">
<!-- id 建立类中的属性与表中的主键对应 -->
<id name="user_id" column="user_id">
<generator class="native"></generator>
</id>
<!-- 建立类中的普通属性和表的字段对应 -->
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="address" column="address"></property>
<property name="age" column="age"></property>
<property name="sex" column="sex"></property>
<property name="groupp" column="groupp"></property>
</class>
</hibernate-mapping>
4 创建一个hibernate的核心配置文件
hibernate的核心配置文件的名称:hibernate.cfg.xml 一定要放在src下
约束头文件/org/hibernate/hibernate-configuration-3.0.dtd
<?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>
<!-- Hibernate 连接数据库的基本信息 -->
<property name="hibernate.connection.dirver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///music?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- Hibernate 使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 运行时是否打印 SQL -->
<property name="show_sql">true</property>
<!-- 运行时是否格式化 SQL -->
<property name="format_sql">true</property>
<!-- 需要关联的 hibernate 映射文件 .hbm.xml -->
<mapping resource="com/xmetc/hibernate/zyz/Muser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
乱码问题 jdbc:mysql:///music
改为jdbc:mysql:///music?useUnicode=true&characterEncoding=UTF-8
其他可选配置
1)Hibernate.show_sql:是否在运行时候sql语句输出到控制台,编码阶段便于测试的。(默认设置为true)
(2)Hibernate.format_sql:输出在控制台sql语句是否进行排版,便于阅读。(默认设置为true)
(3)Hbm2ddl.auto:可帮助由java代码生成数据库脚本,进而生成具体表结构。如:create/update/create-drop/validate。
Create:表示每次创建新的表格,原来的表删除,同时数据全部清楚重新插入数据。(每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。)
Update:表示在原有的表上更新,保存原来的数据。(也就是原有数据不删除,依然存在,不需要重新插入,加载hibernate自动更新数据库结构)
Create-drop:加载hibernate时创建,退出是删除表结构
Validate:加载hibernate时,验证创建数据库表结构,如果是不同的话则不创建表。
(4)Hibernate.default_schema:默认数据库,如果设置了,则创建表的时候,所有表有前缀。
(5)Hibernate.dialect:配置hibernate方言,可针对特定的数据库优化。
编写测试代码
package com.xmetc.hibernate.zyz;
/**
* hibernate 入门案例
* @author Administrator
*
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateDemo1 {
@Test
public void demo1() {
//1.加载hibernate核心配置文件
Configuration configuration = new Configuration().configure();
//2.创建一个sessionFactory对象,类似于jdbc中的连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通过sessionFactory获取到session对象:类似于jdbc中的connect
Session session = sessionFactory.openSession();
//4.手动开启事务
Transaction transaction = session.beginTransaction();
//5.编写代码
MUser user = new MUser();
user.setUsername("张三");
user.setPassword("88888888888");
session.save(user);
//6.事务提交
transaction.commit();
//7.资源释放
session.close();
}
}
完成 注意mysql数据库驱动的问题 编码问题 约束头规范问题 核心jar hibernate-release-5.4.18.Final\lib\ required