|
hibernate新手,我是。
首先,新建数据库,假设就是 student了、
sql 语句:
create database hibernate_01use hibernate_01;create table student(id int primary key,name varchar(20),age int,introduce text)
那么现在在myeclipse里新建项目(java project即可),
src目录下新建javabean,对应数据库里面的字段:student.java:
package com.guang.student;public class student {private int age;private int id;private String introduce;private String name;public int getAge() {return age;}public int getId() {return id;}public String getIntroduce() {return introduce;}public String getName() {return name;}public void setAge(int age) {this.age = age;}public void setId(int id) {this.id = id;}public void setIntroduce(String introduce) {this.introduce = introduce;}public void setName(String name) {this.name = name;}}
现在在src根目录下面新建文件:hibernate.cfg.xml.里面的代码可以参考文档里面的,我的如下:
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!-- Database connection settings --><property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property><property name="connection.url">jdbc:sqlserver://localhost:1433;databasehibernate_01</property><property name="connection.username">sa</property><property name="connection.password">1</property><!-- JDBC connection pool (use the built-in) --><!-- <property name="connection.pool_size">1</property>--><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.SQLServerDialect</property><!-- Enable Hibernate's automatic session context management --><property name="current_session_context_class">thread</property><!-- Disable the second-level cache --><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><!-- Echo all executed SQL to stdout --><property name="show_sql">true</property><!-- Drop and re-create the database schema on startup --><!-- <property name="hbm2ddl.auto">update</property>--><mapping resource="com/guang/student/student.hbm.xml"/></session-factory></hibernate-configuration>
上面的代码,针对的是Ms Sql Server 数据库。没什么特殊的,只是用户名密码数据库改成自己的就好,但是最后一句话:
<mapping resource="com/guang/student/student.hbm.xml"/>
这个是另一个xml文件的地址。可以根据自己的实际,改成相应的名字。这个文件最好与之前的那个javabean放在一个目录下面,名字也必须是javabeanname.hbm.xml。这个javabeanname就随便写成自己的就好了。
student.hbm.xml(名字确定的)
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.guang.student"><class name="student" table="student"><id name="id" column="id"></id><property name="name"></property><property name="age"></property><property name="introduce"></property></class></hibernate-mapping>
以上这些步骤下来,基本就完成了,下面测试:
随便的新建一个java类。在里面完成测试:
package com.guang.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.guang.student.student;public class studentTest {public static void main(String[] args) {student s=new student();s.setId(1);s.setName("yingg");s.setAge(23);s.setIntroduce("我很中意");Configuration cfg=new Configuration();SessionFactory sf=cfg.configure().buildSessionFactory();Session session=sf.openSession();session.beginTransaction();session.save(s);session.getTransaction().commit();session.close();sf.close();}}
现在就完成了、
需要注意的就是hibernate.cfg.xml文件里面的配置。
|
|
|
|
|
|
|