Intellij idea 配置hibernate web项目

_我不喜欢吃芋头IP属地: 江苏
字数 244阅读 2,901

1.创建项目的时候勾选Hibernate,这里第三布一种是可以选择download从网上下载,也可以自己从网上下载hibernate的源码,然后把其lib/required的包都导入到intellij中,可以看到两种方法导入的包是一样的,只不过一种从网上下载,一种直接本地添加

image
image

可以看到idea 为我们生成了一个hibernate配置文件

image

2.在WEB-INF下新建两个文件夹lib 和classes,将工程所需要的jar包都放到lib目录下

image

3. 更改output路径和添加依赖

image
image
image
image
image

3.配置数据库源

[图片上传中...(image-741903-1510642137647-4)]

image

之后点击可显示数据库相关信息

image

接下来根据相应的表来建立相应的映射类及hbm映射配置文件。

image
image

生成的CstCustomer.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>

    <class name="com.gentleni.domain.CstCustomer" table="cst_customer" schema="hibernate_demo">
        <id name="custId">
            <column name="cust_id" sql-type="bigint(32)"/>
        </id>
        <property name="custName">
            <column name="cust_name" sql-type="varchar(32)" length="32"/>
        </property>
        <property name="custUserId">
            <column name="cust_user_id" sql-type="bigint(32)" not-null="true"/>
        </property>
        <property name="custCreateId">
            <column name="cust_create_id" sql-type="bigint(32)" not-null="true"/>
        </property>
        <property name="custSource">
            <column name="cust_source" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custIndustry">
            <column name="cust_industry" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custLevel">
            <column name="cust_level" sql-type="varchar(32)" length="32" not-null="true"/>
        </property>
        <property name="custLinkman">
            <column name="cust_linkman" sql-type="varchar(64)" length="64" not-null="true"/>
        </property>
        <property name="custPhone">
            <column name="cust_phone" sql-type="varchar(64)" length="64" not-null="true"/>
        </property>
        <property name="custMobile">
            <column name="cust_mobile" sql-type="varchar(16)" length="16" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>

修改 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>
        <!--配置数据库连接-->
        <!--比说明字符编码为utf-8-->
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_demo?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8</property>
        <!--若驱动导入报错,请检查是否倒入了相关数据库连接的Jar包-->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <!--相关的属性-->
        <property name="hbm2ddl.auto">update</property>
        <!--格式化控制台显示的MySQL语句-->
        <property name="format_sql">true</property>
        <!--在控制台显示执行的MySQL语句-->
        <property name="show_sql">true</property>
        <!--使用MySQL数据库的方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--相关的映射-->
        <mapping resource="com/gentleni/domain/CstCustomer.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

测试代码

public class Test1 {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;
    @Before
    public void Init() {
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }
    @Test
    public void test() {
        CstCustomer cstCustomer = new CstCustomer();
        cstCustomer.setCustName("张三");
        session.save(cstCustomer);
    }
    @After
    public void destroy() {
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

控制台输出


image.png

数据添加成功


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
1人点赞
更多精彩内容,就在简书APP
"小礼物走一走,来简书关注我"
还没有人赞赏,支持一下
总资产1共写了1.3W字获得26个赞共29个粉丝

推荐阅读更多精彩内容

  • 本文包括: 1、CRM 项目的整体介绍 2、Hibernate 框架概述 3、Hibernate 快速入门 4、H...
    廖少少阅读 3,485评论 9 66
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,975评论 6 342
  • Hibernate中配置主要分为两种:一种包含了Hibernate与数据库的基本连接信息,在Hibernate工作...
    FTOLsXD阅读 2,157评论 0 10
  • 谁的凝望烫醒你原始的流水和芬芳盘旋而来 翩翩而舞我愿把所有精魂撒到你的身上 把愿意和抛弃和情绪种进体内用我赤裸的情...
    1a43998ab2a4阅读 266评论 1 6