最简单的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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值