记录Hibernate新增数据的练习部分
实体类
package com.maty.entity;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月26日 下午1:40:30 类说明 本类为ORM的实体类
*/
public class User
{
private int id;
private String username;
private String password;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = 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;
}
@Override
public String toString()
{
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
Hibernate的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入映射的约束条件,位于hibernate-core包中的org.hibernate.hibernate-configuration-3.0.dtd中 -->
<!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.maty.entity.User" table="t_user">
<!-- 设定实体类id与表的id -->
<id name="id" column="id">
<!-- 设定表中id的增长策略,使用native则表明根据数据库类型来决定主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 设置其他属性的对应关系 -->
<property name="username" column="username"></property>
<property name="password" column="password"></property>
</class>
</hibernate-mapping>
Hibernate的核心配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入Hibernate的核心配置文件的约束,该约束位于hibernate-core包中的org.hibernate.hibernate-configuration-3.0.dtd -->
<!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.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 配置数据库的驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置数据库的url -->
<property name="hibernate.connection.url">jdbc:mysql://localhost/tx</property>
<!-- 配置数据库的用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 配置数据库的密码 -->
<property name="hibernate.connection.password">wangxiaowei</property>
<!-- 第二步:配置Hibernate本身的设置 -->
<!-- 是否在console显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对sql语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 是否自动建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 第三步:加载hibernate mapping文件 -->
<mapping resource="com/maty/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类
package com.maty.dao;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.maty.entity.User;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月26日 下午3:21:23
* 类说明 该类用来演示hibernate的添加数据
*/
public class UserDao
{
@Test
public void add()
{
//1.加载hibernate的核心配置文件
Configuration cfg = new Configuration().configure();
//2.创建SessionFactory对象
SessionFactory sessionFactory = cfg.buildSessionFactory();
//3.创建session对象
Session session = sessionFactory.openSession();
//4.操作实体类
User user = new User();
user.setUsername("tom");
user.setPassword("512181");
//5.开启事务
Transaction transaction = session.beginTransaction();
session.save(user);
//6.提交操作
transaction.commit();
//7.关闭资源
session.close();
sessionFactory.close();
System.out.println("新增数据成功");
}
}