最简单的hibernate5连接mysql5

这个例子可以没有表,但是库必须有;没有表通过hibernate的
update配置去创建表

public void test2(){
       Customer c=new Customer();
       c.setCustName("赵宇集团");
       c.setCustAddress("海南省市");
       c.setCustPhoen("2522700");
       c.setCustSource("自由有来过的了a");
       StandardServiceRegistry sr=new StandardServiceRegistryBuilder().configure().build();
      SessionFactory sf=new MetadataSources(sr).buildMetadata().buildSessionFactory();
       Session session=sf.openSession();
       //4开启事务
       Transaction tx=session.beginTransaction();
       //5执行操作(保存)
       session.save(c);
       //6提交事务
       tx.commit();
       //7释放资源
       session.close();
       sf.close();
    }
<?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.url">jdbc:mysql://192.168.0.62:3306/abc?useUnicode=true&amp;characterEncoding=UTF8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">abc</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/domain/Customer.hbm.xml"/>

    </session-factory>
</hibernate-configuration> 
package com.domain;


import java.io.Serializable;

public class Customer implements Serializable {
    private Long custId;
    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;
    private String custAddress;
    private String custPhoen;

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustSource() {
        return custSource;
    }

    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }

    public String getCustIndustry() {
        return custIndustry;
    }

    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }

    public String getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    public String getCustPhoen() {
        return custPhoen;
    }

    public void setCustPhoen(String custPhoen) {
        this.custPhoen = custPhoen;
    }
}

<?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.domain">
    <class name="Customer" table="cst_customer">
            <id name="custId" column="cust_id">
                <generator class="native"></generator>
            </id>
        <property name="custName" column="cust_name"></property>
        <property name="custSource" column="cust_source"></property>
        <property name="custIndustry" column="cust_industry"></property>
        <property name="custAddress" column="cust_address"></property>
        <property name="custPhoen" column="cust_phone"></property>
        <property name="custLevel" column="cust_level"></property>

    </class>
</hibernate-mapping>
发布了109 篇原创文章 · 获赞 56 · 访问量 5708

猜你喜欢

转载自blog.csdn.net/zhang6132326/article/details/104365366