@chy282
2017-12-01T13:56:18.000000Z
字数 5199
阅读 1820
Hibernate
© 版权声明:本文为博主原创文章,转载请注明出处
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.imooc</groupId>
<artifactId>Hibernate_001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.5.Final</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<target>1.7</target>
<source>1.7</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?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="connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="connection.password">20121221</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate?useSSL=true&useUnicode=true&characterEncoding=UTF-8
</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 展示SQL -->
<property name="show_sql">true</property>
<!-- 格式化SQL -->
<property name="format_sql">false</property>
<!-- 建表策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- 指定映射文件 -->
<mapping resource="hbm/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.imooc.hibernate.model;
import java.util.Date;
public class Student {
private int sid; // 学号
private String sname; // 姓名
private String gender; // 性别
private Date birthday; // 出生日期
private String address; // 地址
public Student() {
}
public Student(int sid, String sname, String gender, Date birthday, String address) {
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address + "]";
}
}
<?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.imooc.hibernate.model.Student">
<id name="sid" type="int">
<column name="SID"/>
<generator class="assigned"/> <!-- 主键生成策略 -->
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME"/>
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER"></column>
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY"/>
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS"></column>
</property>
</class>
</hibernate-mapping>
package com.imooc.hibernate.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.imooc.hibernate.model.Student;
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Test
public void testSaveStudent() {
// 生成学生对象
Student s = new Student(3, "张三丰", "男", new Date(), "武当山");
session.save(s);
}
@Before
public void init() {
// 创建会话工厂对象
sessionFactory = new Configuration().configure().buildSessionFactory();
// 创建会话对象
session = sessionFactory.openSession();
// 开始事务
transaction = session.beginTransaction();
}
@After
public void destory () {
// 提交事务
transaction.commit();
// 关闭会话
session.close();
// 关闭会话工厂
sessionFactory.close();
}
}
Hibernate初步搭建很简单,主要是使用hibernate.cfg.xml进行数据库连接的配置,使用entity.hbm.xml进行数据库表结构与实体类进行映射。