简单Hibernate的例子


在这里插入图片描述

sql

create database hibernateTest;

use hibernateTest;

create table user
(
    `id`   int          not null primary key,
    `name` varchar(200) not null,
    `sex`  varchar(200) not null
)

User.java

package com.cai.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description
 * @Author Cai
 * @Date 2021-04-01 13:49
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private Integer id;
    private String name;
    private String sex;
}

User.hbm.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.cai.domain">
    <class name="User" table="user">
        <id name="id" type="java.lang.Integer">
            <generator class="identity"/>
        </id>
        <property name="name"/>
        <property name="sex"/>
    </class>
</hibernate-mapping>

UserManager

package com.cai;

import com.cai.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * @Description
 * @Author Cai
 * @Date 2021-04-01 13:53
 */
public class UserManager {
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration().configure();
        SessionFactory sessionFactory = conf.buildSessionFactory();
        Session sess = sessionFactory.openSession();
        Transaction startTask = sess.beginTransaction();
        User user = new User();
        user.setName("wade");
        user.setSex("bad boy");
        sess.save(user);
        startTask.commit();
        sessionFactory.close();
    }

}

hibernate.cfg.xml

<?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>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernateTest</property>
        <property name="connection.username">root</property>
        <property name="connection.password">818181</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <mapping resource="com/cai/domain/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


结果

在这里插入图片描述

遇到的问题

Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

这个的话,我的情况是缺少了javassist的包,所以在依赖那里加了这个包就行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值