标签:rollback maven依赖 replace rac ping pid auto generated utc
1. 引入相关maven依赖:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency>
2. 创建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.username">root</property> <property name="hibernate.connection.password">toor</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_test?serverTimezone=UTC&characterEncoding=utf8</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="format_sql">true</property> <mapping resource="org/cc11001100/hibernate/domain/User.hbm.xml" /> </session-factory> </hibernate-configuration>
3. 创建DO类
User:
package org.cc11001100.hibernate.domain; import java.util.Date; /** * 用户实体 * * @author CC11001100 * */ public class User { /**创建时间*/ private Date createTime; /**上次更新时间*/ private Date updateTime; /**主键*/ private String id; /**用户名*/ private String username; /**密码*/ private String passwd; public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } }
4. 创建user.hbm.xml文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-5-20 22:08:10 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="org.cc11001100.hibernate.domain.User" table="USER"> <id name="id" type="java.lang.String"> <column name="ID" /> <generator class="assigned" /> </id> <property name="createTime" type="java.util.Date"> <column name="CREATETIME" /> </property> <property name="updateTime" type="java.util.Date"> <column name="UPDATETIME" /> </property> <property name="username" type="java.lang.String"> <column name="USERNAME" /> </property> <property name="passwd" type="java.lang.String"> <column name="PASSWD" /> </property> </class> </hibernate-mapping>
5. 保存一个对象到数据库中
@Test public void test_001(){ SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory(); Session session=sessionFactory.openSession(); try { session.beginTransaction(); User user=new User(); user.setId(UUID.randomUUID().toString().replaceAll("-", "")); user.setCreateTime(new Date()); user.setUpdateTime(new Date()); user.setUsername("username"); user.setPasswd("passwd"); session.save(user); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); }finally{ try { session.close(); } catch (Exception e) { e.printStackTrace(); } } }
标签:rollback maven依赖 replace rac ping pid auto generated utc
原文地址:http://www.cnblogs.com/cc11001100/p/6884595.html