[hibernate]创建hibernate项目并通过xml文件配置生成数据表

来源:互联网 发布:决策支持软件 编辑:程序博客网 时间:2024/06/12 22:37

创建项目步骤

1.使用idea的maven工具创建好项目

2.在pom.xml文件中导入hibernate和mysql数据库的jar包(默认junit包已经存在,真实项目下要导入c3p0类似的连接池的jar包)

代码如下:

<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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>Mapping-0711</groupId>  <artifactId>Mapping-0711</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>Mapping-0711 Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <!--hibernate包-->    <dependency>      <groupId>org.hibernate</groupId>      <artifactId>hibernate-core</artifactId>      <version>5.2.10.Final</version>    </dependency>    <!--mysql版本-->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.19</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>RELEASE</version>    </dependency>  </dependencies>  <build>    <finalName>Mapping-0711</finalName>  </build></project>

3.在main目录下的resources资源目录下配置hibernate.cfg.xml文件,示例如下:

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD//EN"    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>  <session-factory>    <!--  mysql账户名  -->    <property name="connection.username">root</property>    <!--  mysql密码  -->    <property name="connection.password">root</property>    <!--  mysql驱动  -->    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <!--  mysql连接URL  jdbc:mysql://10.3.152.227:3306/test  -->    <property name="connection.url">jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</property>    <!--  数据库方言  -->    <property name="dialect">org.hibernate.dialect.MySQL55Dialect</property>    <!--  显示sql语句  -->    <property name="show_sql">true</property>    <!--  格式化sql语句  -->    <property name="format_sql">true</property>    <!--  根据需要创建数据库  -->    <property name="hbm2ddl.auto">update</property>     <!--  注册务必要  -->    <mapping resource="one-to-one.hbm.xml"/>    <mapping resource="one-to-many.hbm.xml"/>    <mapping resource="many-to-many.hbm.xml"/>  </session-factory></hibernate-configuration>

4.创建javabean对象(如Husband和Wife)

/** * Created by kimhuhg@163.com */public class Husband {    private Integer hid;    private String name;    private Wife wife;    public Wife getWife() {        return wife;    }    public void setWife(Wife wife) {        this.wife = wife;    }    public Husband() {    }    public Integer getHid() {        return hid;    }    public void setHid(Integer hid) {        this.hid = hid;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Husband{" +                "hid=" + hid +                ", name='" + name + '\'' +                '}';    }}
/** * Created by kimhuhg@163.com */public class Wife {    private Integer wid;    private String name;    private Husband husband;    public Integer getWid() {        return wid;    }    public void setWid(Integer wid) {        this.wid = wid;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Husband getHusband() {        return husband;    }    public void setHusband(Husband husband) {        this.husband = husband;    }}

5.创建对应javabean映射数据库表的xml文件

<?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 package="com.yztc.demo.bean">    <class name="Husband" table="TB_HUSBAND" schema="test">        <id name="hid">            <column name="HID"/>            <generator class="native"/>        </id>        <property name="name">            <column name="NAME"/>        </property>        <one-to-one name="wife" cascade="all"/>    </class>    <class name="Wife" table="TB_WIFE" schema="test">        <id name="wid">            <column name="WID"/>            <generator class="native"/>        </id>        <property name="name">            <column name="NAME"/>        </property>        <many-to-one name="husband" column="HID" unique="true" cascade="all"/>    </class></hibernate-mapping>

6.创建测试类

public class Test {    private SessionFactory sessionFactory;    private Session session;    @Before    public void init () {        Configuration configure = new Configuration().configure();        sessionFactory = configure.buildSessionFactory();        session = sessionFactory.openSession();    }    @After    public void destroy () {        session.close();    @org.junit.Test    public void testAdd () {        Transaction transaction = session.beginTransaction();        Husband husband = new Husband();        husband.setName("小明");        Wife wife = new Wife();        wife.setName("潘金莲");        wife.setHusband(husband);        husband.setWife(wife);        session.save(husband);        transaction.commit();        session.close();    }}
阅读全文
0 0