首页 > 代码库 > Hibernate总结之Hello,World
Hibernate总结之Hello,World
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(); } } }
Hibernate总结之Hello,World
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。