Hibernate入门
创建一个实体类User
import java.io.Serializable;
public class User implements Serializable {
//用户ID
private Integer id;
//用户名
private String username;
//密码
private String password;
//创建时间
private String createtime;
//修改时间
private String updatetime;
//邮箱
private String email;
//状态
private String state;
//权限角色
private Integer role;
//默认构造方法
public User() {
}
//根据属性创建 构造方法
public Integer getId() {
return id;
}
public User(Integer id, String username, String password,
String createtime, String updatetime, String email, String state,
Integer role) {
super();
this.id = id;
this.username = username;
this.password = password;
this.createtime = createtime;
this.updatetime = updatetime;
this.email = email;
this.state = state;
this.role = role;
}
public void setId(Integer 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;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
}
创建Hibernatesessionfactory 类
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Hibernatesessionfactory {
private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
private static SessionFactory sessionFactory;
static {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure(CONFIG_FILE_LOCATION).build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
public static Session getSession(){
Session session = sessionFactory.getCurrentSession();
return session;
}
}
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Hibernatesessionfactory {
private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";
private static SessionFactory sessionFactory;
static {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure(CONFIG_FILE_LOCATION).build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
public static Session getSession(){
Session session = sessionFactory.getCurrentSession();
return session;
}
}
Hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 当前Session上下文 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 在控制台显示SQL语句 -->
<property name="show_sql">true</property>
<!--根据需要自动生成、更新数据表 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/tiamo/entity/hibernate_user_mapper.xml"/>
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 当前Session上下文 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 在控制台显示SQL语句 -->
<property name="show_sql">true</property>
<!--根据需要自动生成、更新数据表 -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/tiamo/entity/hibernate_user_mapper.xml"/>
</session-factory>
</hibernate-configuration>
hibernate_user_mapper.xml
<!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.tiamo.entity.User" table="USER">
<!-- 主键 -->
<id name="id" column="ID">
<generator class="native" />
</id>
<!-- 用户名 -->
<property name="username" column="USERNAME" type="string" not-null="true" />
<!-- 密码 -->
<property name="password" column="PASSWORD" type="string" not-null="true" />
<!-- 用户名 -->
<property name="createtime" column="CREATETIME" type="string" not-null="true" />
<!-- 用户名 -->
<property name="updatetime" column="UPDATETIME" type="string" not-null="true" />
<!-- 用户名 -->
<property name="email" column="EMAIL" type="string" not-null="true" />
<!-- 用户名 -->
<property name="state" column="STATE" type="string" not-null="true" />
<!-- 权限 -->
<property name="role" column="ROLE" type="java.lang.Integer" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.tiamo.entity.User" table="USER">
<!-- 主键 -->
<id name="id" column="ID">
<generator class="native" />
</id>
<!-- 用户名 -->
<property name="username" column="USERNAME" type="string" not-null="true" />
<!-- 密码 -->
<property name="password" column="PASSWORD" type="string" not-null="true" />
<!-- 用户名 -->
<property name="createtime" column="CREATETIME" type="string" not-null="true" />
<!-- 用户名 -->
<property name="updatetime" column="UPDATETIME" type="string" not-null="true" />
<!-- 用户名 -->
<property name="email" column="EMAIL" type="string" not-null="true" />
<!-- 用户名 -->
<property name="state" column="STATE" type="string" not-null="true" />
<!-- 权限 -->
<property name="role" column="ROLE" type="java.lang.Integer" not-null="true" />
</class>
</hibernate-mapping>
测试类:Test.java
package com.tiamo.session;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.tiamo.entity.Person;
import com.tiamo.entity.User;
public class Test {
public static void main(String[] args) {
Session session = Hibernatesessionfactory.getSession();
Transaction transaction = session.beginTransaction();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date=new Date();
String nowdate=format.format(date);
try {
User user = new User();
user.setId(new Integer(1));
user.setUsername("admin");
user.setPassword("123456");
user.setCreatetime(nowdate);
user.setUpdatetime(nowdate);
user.setEmail("2323438349@qq.com");
user.setState("1");
user.setRole(new Integer(100));
Person person = new Person();
person.setId(1L);
person.setName("admin");
person.setAge(100);
session.save(person);
session.save(user);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.tiamo.entity.Person;
import com.tiamo.entity.User;
public class Test {
public static void main(String[] args) {
Session session = Hibernatesessionfactory.getSession();
Transaction transaction = session.beginTransaction();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date=new Date();
String nowdate=format.format(date);
try {
User user = new User();
user.setId(new Integer(1));
user.setUsername("admin");
user.setPassword("123456");
user.setCreatetime(nowdate);
user.setUpdatetime(nowdate);
user.setEmail("2323438349@qq.com");
user.setState("1");
user.setRole(new Integer(100));
Person person = new Person();
person.setId(1L);
person.setName("admin");
person.setAge(100);
session.save(person);
session.save(user);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
}