标签:
参考: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 模板内容如下(实际中应该进行相应的修改):
<?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> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="Students.hbm.xml"/> <!-- DB schema will be updated if needed --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>
同样地方式,创建对象/关系映射的配置文件模板,这里创建 entity2.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="Students" table="students"> <id name="sid" type="int"> <column name="sid"/> <generator class="assigned"/> </id> <property name="sname" type="java.lang.String"> <column name="sname"/> </property> <property name="gender" type="java.lang.String"> <column name="gender"/> </property> <property name="birthday" type="java.util.Date"> <column name="birthday"/> </property> <property name="address" type="java.lang.String"> <column name="address"/> </property> </class> </hibernate-mapping>
<?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> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="Students.hbm.xml"/> <!-- DB schema will be updated if needed --> <!-- <property name="hbm2ddl.auto">update</property> --> </session-factory> </hibernate-configuration>
import java.util.Date; /** * Created by DreamBoy on 2016/5/15. */ //学生类 public class Students { //1. 必须为公有的类 //2. 必须提供公有的不带参数的默认的构造方法 //3. 属性私有 //4. 属性setter/getter封装 private int sid; //学号 private String sname; //姓名 private String gender; //性别 private Date birthday; //出生日期 private String address; //地址 public Students() { } public Students(int sid, String sname, String gender, Date birthday, String address) { this.sid = sid; this.sname = sname; this.gender = gender; this.birthday = birthday; this.address = address; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Students{" + "sid=" + sid + ", sname='" + sname + '\'' + ", gender='" + gender + '\'' + ", birthday=" + birthday + ", address='" + address + '\'' + '}'; } }
<?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="Students" table="students"> <id name="sid" type="int"> <column name="sid"/> <generator class="assigned"/> </id> <property name="sname" type="java.lang.String"> <column name="sname"/> </property> <property name="gender" type="java.lang.String"> <column name="gender"/> </property> <property name="birthday" type="java.util.Date"> <column name="birthday"/> </property> <property name="address" type="java.lang.String"> <column name="address"/> </property> </class> </hibernate-mapping>
在hibernate.cfg.xml 配置文件中添加Students对象的映射。(前面的配置文件中已添加)
<mapping resource="Students.hbm.xml"/>
在Module下创建test目录,为了能在该目录中创建java类,需要将目录修改为 Sources Root:
此时才可以创建java类:StudentsTest.java
(这里使用到 junit 中的注解 @Test、@Before、@After)
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.Date; /** * Created by DreamBoy on 2016/5/15. */ //测试类 public class StudentsTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void init() { //创建配置对象 Configuration config = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //创建会话工厂对象 sessionFactory = config.buildSessionFactory(serviceRegistry); //会话对象 session = sessionFactory.openSession(); //开启事务 transaction = session.beginTransaction(); } @After public void destory() { transaction.commit(); //提交事务 session.close(); //关闭会话 sessionFactory.close(); //关闭会话工厂 } @Test public void testSaveStudents() { //生成学生对象 Students s = new Students(1, "张三丰", "男", new Date(), "武当山"); session.save(s); //保存对象进入数据库 } }
在hibernate.cfg.xml配置文件中,可以知道,这里我配置使用的数据库,名为hibernate,所以我们需要在运行程序前,使用mysql创建好 hibernate 数据库,不用创建Students表。
运行 StudentsTest.java ,可能会出现如下错误:
大概是java编译输出的位置出错了,所以要对output输出路径进行配置:
这里选择在当前Module下classes文件夹下:
再次运行程序:
程序运行成功,我们可以查看一下数据库,会发现程序已经帮我们自动创建好了数据表,并添加了一条数据:
成功运行Hibernate项目。
标签:
原文地址:http://blog.csdn.net/qq_15096707/article/details/51419304