[关闭]
@chy282 2017-12-01T05:56:18.000000Z 字数 5199 阅读 1723

Hibernate二次学习一----------搭建Hibernate

Hibernate


© 版权声明:本文为博主原创文章,转载请注明出处

1. 项目结构

1.1 pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.imooc</groupId>
  5. <artifactId>Hibernate_001</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <properties>
  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  10. </properties>
  11. <dependencies>
  12. <!-- junit -->
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>4.12</version>
  17. <scope>test</scope>
  18. </dependency>
  19. <!-- Hibernate -->
  20. <dependency>
  21. <groupId>org.hibernate</groupId>
  22. <artifactId>hibernate-core</artifactId>
  23. <version>5.1.5.Final</version>
  24. </dependency>
  25. <!-- mysql-connector-java -->
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. <version>5.1.22</version>
  30. </dependency>
  31. </dependencies>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.apache.maven.plugins</groupId>
  36. <artifactId>maven-compiler-plugin</artifactId>
  37. <version>3.6.1</version>
  38. <configuration>
  39. <target>1.7</target>
  40. <source>1.7</source>
  41. <encoding>UTF-8</encoding>
  42. </configuration>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>

1.2 hibernate.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 数据库连接名 -->
  8. <property name="connection.username">root</property>
  9. <!-- 数据库连接密码 -->
  10. <property name="connection.password">20121221</property>
  11. <!-- 数据库驱动 -->
  12. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  13. <!-- 数据库连接url -->
  14. <property name="connection.url">
  15. jdbc:mysql://localhost:3306/hibernate?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8
  16. </property>
  17. <!-- 方言 -->
  18. <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  19. <!-- 展示SQL -->
  20. <property name="show_sql">true</property>
  21. <!-- 格式化SQL -->
  22. <property name="format_sql">false</property>
  23. <!-- 建表策略 -->
  24. <property name="hbm2ddl.auto">update</property>
  25. <!-- 指定映射文件 -->
  26. <mapping resource="hbm/student.hbm.xml"/>
  27. </session-factory>
  28. </hibernate-configuration>

1.3 entity

  1. package com.imooc.hibernate.model;
  2. import java.util.Date;
  3. public class Student {
  4. private int sid; // 学号
  5. private String sname; // 姓名
  6. private String gender; // 性别
  7. private Date birthday; // 出生日期
  8. private String address; // 地址
  9. public Student() {
  10. }
  11. public Student(int sid, String sname, String gender, Date birthday, String address) {
  12. this.sid = sid;
  13. this.sname = sname;
  14. this.gender = gender;
  15. this.birthday = birthday;
  16. this.address = address;
  17. }
  18. public int getSid() {
  19. return sid;
  20. }
  21. public void setSid(int sid) {
  22. this.sid = sid;
  23. }
  24. public String getSname() {
  25. return sname;
  26. }
  27. public void setSname(String sname) {
  28. this.sname = sname;
  29. }
  30. public String getGender() {
  31. return gender;
  32. }
  33. public void setGender(String gender) {
  34. this.gender = gender;
  35. }
  36. public Date getBirthday() {
  37. return birthday;
  38. }
  39. public void setBirthday(Date birthday) {
  40. this.birthday = birthday;
  41. }
  42. public String getAddress() {
  43. return address;
  44. }
  45. public void setAddress(String address) {
  46. this.address = address;
  47. }
  48. @Override
  49. public String toString() {
  50. return "Student [sid=" + sid + ", sname=" + sname + ", gender="
  51. + gender + ", birthday=" + birthday + ", address=" + address + "]";
  52. }
  53. }

1.4 entity.hbm.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6. <class name="com.imooc.hibernate.model.Student">
  7. <id name="sid" type="int">
  8. <column name="SID"/>
  9. <generator class="assigned"/> <!-- 主键生成策略 -->
  10. </id>
  11. <property name="sname" type="java.lang.String">
  12. <column name="SNAME"/>
  13. </property>
  14. <property name="gender" type="java.lang.String">
  15. <column name="GENDER"></column>
  16. </property>
  17. <property name="birthday" type="java.util.Date">
  18. <column name="BIRTHDAY"/>
  19. </property>
  20. <property name="address" type="java.lang.String">
  21. <column name="ADDRESS"></column>
  22. </property>
  23. </class>
  24. </hibernate-mapping>

2. 测试

  1. package com.imooc.hibernate.test;
  2. import java.util.Date;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6. import org.hibernate.cfg.Configuration;
  7. import org.junit.After;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import com.imooc.hibernate.model.Student;
  11. public class StudentTest {
  12. private SessionFactory sessionFactory;
  13. private Session session;
  14. private Transaction transaction;
  15. @Test
  16. public void testSaveStudent() {
  17. // 生成学生对象
  18. Student s = new Student(3, "张三丰", "男", new Date(), "武当山");
  19. session.save(s);
  20. }
  21. @Before
  22. public void init() {
  23. // 创建会话工厂对象
  24. sessionFactory = new Configuration().configure().buildSessionFactory();
  25. // 创建会话对象
  26. session = sessionFactory.openSession();
  27. // 开始事务
  28. transaction = session.beginTransaction();
  29. }
  30. @After
  31. public void destory () {
  32. // 提交事务
  33. transaction.commit();
  34. // 关闭会话
  35. session.close();
  36. // 关闭会话工厂
  37. sessionFactory.close();
  38. }
  39. }

3. 总结

Hibernate初步搭建很简单,主要是使用hibernate.cfg.xml进行数据库连接的配置,使用entity.hbm.xml进行数据库表结构与实体类进行映射。

参考:Hibernate初探之单表映射

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注