转自:http://blog.csdn.net/qq_15096707/article/details/51419304 ,如需转载,请联系原博主。
参考:intellij配置hibernate自动生成hbm.xml文件 从零开始用Intellij idea14创建hibernate项目
下面我要讲的创建方式,可能更加原生态,更加类似于Eclipse下创建Hibernate项目的方式,我想这也有助于对在Intellij IDEA下创建Hibernate项目的理解。
首先需要在Intellij IDEA下创建一个项目Project,相当于Eclipse下的workspace(工作空间),当然如果你此时选择了创建Hibernate项目的方式,Intellij 在创建Project成功后会在Project下创建这一Hibernate项目。可能看起来有点奇怪,没关系,我们可以把默认创建的东西删除,然后创建我们的Module,相当于Eclipse下的Project。
创建Module --》选择 Java Enterprise选项卡,点击右侧,勾选Web Application 和 Hibernate,如下:
选择右下角的 Configure... ,选择Module Library:
点击下一步,输入Module的名称,这里我取名为:Hibernate_00,如:
等待 Hibernate 相关jar包下载完毕……
还需要添加mysql-jdbc jar包 和 junit jar包(junit jar包实际中可以不添加,根据实际需要进行添加):
对Hibernate_00 右键,选择 Open Module Settings,或者按F4:
选择 From Maven。
以同样的方式下载 junit jar包并进行添加:
为方便以后创建Hibernate项目,可以为 hibernate 的配置文件创建模板:
hibernate.cfg.xml 模板内容如下(实际中应该进行相应的修改):
- <?xmlversion=‘1.0‘encoding=‘utf-8‘?>
- <!DOCTYPEhibernate-configurationPUBLIC
- "-//Hibernate/HibernateConfigurationDTD//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <propertyname="connection.username">root</property>
- <propertyname="connection.password"></property>
- <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
- <propertyname="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
- <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
- <propertyname="show_sql">true</property>
- <propertyname="format_sql">true</property>
- <propertyname="hbm2ddl.auto">create</property>
- <mappingresource="Students.hbm.xml"/>
- <!--DBschemawillbeupdatedifneeded-->
- <!--<propertyname="hbm2ddl.auto">update</property>-->
- </session-factory>
- </hibernate-configuration>
同样地方式,创建对象/关系映射的配置文件模板,这里创建 entity2.hbm.xml 模板如下(实际中应该进行相应地修改):
- <?xmlversion=‘1.0‘encoding=‘utf-8‘?>
- <!DOCTYPEhibernate-mappingPUBLIC
- "-//Hibernate/HibernateMappingDTD3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <classname="Students"table="students">
- <idname="sid"type="int">
- <columnname="sid"/>
- <generator/>
- </id>
- <propertyname="sname"type="java.lang.String">
- <columnname="sname"/>
- </property>
- <propertyname="gender"type="java.lang.String">
- <columnname="gender"/>
- </property>
- <propertyname="birthday"type="java.util.Date">
- <columnname="birthday"/>
- </property>
- <propertyname="address"type="java.lang.String">
- <columnname="address"/>
- </property>
- </class>
- </hibernate-mapping>
- <?xmlversion=‘1.0‘encoding=‘utf-8‘?>
- <!DOCTYPEhibernate-configurationPUBLIC
- "-//Hibernate/HibernateConfigurationDTD//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <propertyname="connection.username">root</property>
- <propertyname="connection.password"></property>
- <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
- <propertyname="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>
- <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
- <propertyname="show_sql">true</property>
- <propertyname="format_sql">true</property>
- <propertyname="hbm2ddl.auto">create</property>
- <mappingresource="Students.hbm.xml"/>
- <!--DBschemawillbeupdatedifneeded-->
- <!--<propertyname="hbm2ddl.auto">update</property>-->
- </session-factory>
- </hibernate-configuration>
在src下创建 Students 类:
- importjava.util.Date;
- /**
- *CreatedbyDreamBoyon2016/5/15.
- */
- //学生类
- publicclassStudents{
- //1.必须为公有的类
- //2.必须提供公有的不带参数的默认的构造方法
- //3.属性私有
- //4.属性setter/getter封装
- privateintsid;//学号
- privateStringsname;//姓名
- privateStringgender;//性别
- privateDatebirthday;//出生日期
- privateStringaddress;//地址
- publicStudents(){
- }
- publicStudents(intsid,Stringsname,Stringgender,Datebirthday,Stringaddress){
- this.sid=sid;
- this.sname=sname;
- this.gender=gender;
- this.birthday=birthday;
- this.address=address;
- }
- publicintgetSid(){
- returnsid;
- }
- publicvoidsetSid(intsid){
- this.sid=sid;
- }
- publicStringgetSname(){
- returnsname;
- }
- publicvoidsetSname(Stringsname){
- this.sname=sname;
- }
- publicStringgetGender(){
- returngender;
- }
- publicvoidsetGender(Stringgender){
- this.gender=gender;
- }
- publicDategetBirthday(){
- returnbirthday;
- }
- publicvoidsetBirthday(Datebirthday){
- this.birthday=birthday;
- }
- publicStringgetAddress(){
- returnaddress;
- }
- publicvoidsetAddress(Stringaddress){
- this.address=address;
- }
- @Override
- publicStringtoString(){
- return"Students{"+
- "s,sname=‘"+sname+‘\‘‘+
- ",gender=‘"+gender+‘\‘‘+
- ",birthday="+birthday+
- ",address=‘"+address+‘\‘‘+
- ‘}‘;
- }
- }
在src下创建 对象/关系映射的配置文件(根据模板进行创建) Students.hbm.xml
- <?xmlversion=‘1.0‘encoding=‘utf-8‘?>
- <!DOCTYPEhibernate-mappingPUBLIC
- "-//Hibernate/HibernateMappingDTD3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <classname="Students"table="students">
- <idname="sid"type="int">
- <columnname="sid"/>
- <generator/>
- </id>
- <propertyname="sname"type="java.lang.String">
- <columnname="sname"/>
- </property>
- <propertyname="gender"type="java.lang.String">
- <columnname="gender"/>
- </property>
- <propertyname="birthday"type="java.util.Date">
- <columnname="birthday"/>
- </property>
- <propertyname="address"type="java.lang.String">
- <columnname="address"/>
- </property>
- </class>
- </hibernate-mapping>
在hibernate.cfg.xml 配置文件中添加Students对象的映射。(前面的配置文件中已添加)
- <mappingresource="Students.hbm.xml"/>
在Module下创建test目录,为了能在该目录中创建java类,需要将目录修改为 Sources Root:
此时才可以创建java类:StudentsTest.java
(这里使用到 junit 中的注解 @Test、@Before、@After)
- importorg.hibernate.Session;
- importorg.hibernate.SessionFactory;
- importorg.hibernate.Transaction;
- importorg.hibernate.cfg.Configuration;
- importorg.hibernate.service.ServiceRegistry;
- importorg.hibernate.service.ServiceRegistryBuilder;
- importorg.junit.After;
- importorg.junit.Before;
- importorg.junit.Test;
- importjava.util.Date;
- /**
- *CreatedbyDreamBoyon2016/5/15.
- */
- //测试类
- publicclassStudentsTest{
- privateSessionFactorysessionFactory;
- privateSessionsession;
- privateTransactiontransaction;
- @Before
- publicvoidinit(){
- //创建配置对象
- Configurationconfig=newConfiguration().configure();
- //创建服务注册对象
- ServiceRegistryserviceRegistry=newServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
- //创建会话工厂对象
- sessionFactory=config.buildSessionFactory(serviceRegistry);
- //会话对象
- session=sessionFactory.openSession();
- //开启事务
- transaction=session.beginTransaction();
- }
- @After
- publicvoiddestory(){
- transaction.commit();//提交事务
- session.close();//关闭会话
- sessionFactory.close();//关闭会话工厂
- }
- @Test
- publicvoidtestSaveStudents(){
- //生成学生对象
- Studentss=newStudents(1,"张三丰","男",newDate(),"武当山");
- session.save(s);//保存对象进入数据库
- }
- }
在hibernate.cfg.xml配置文件中,可以知道,这里我配置使用的数据库,名为hibernate,所以我们需要在运行程序前,使用mysql创建好 hibernate 数据库,不用创建Students表。
运行 StudentsTest.java ,可能会出现如下错误:
大概是java编译输出的位置出错了,所以要对output输出路径进行配置:
这里选择在当前Module下classes文件夹下:
再次运行程序:
程序运行成功,我们可以查看一下数据库,会发现程序已经帮我们自动创建好了数据表,并添加了一条数据:
成功运行Hibernate项目。
Intellij IDEA下的第一个Hibernate项目
原文地址:http://www.cnblogs.com/ixummer/p/8082375.html