hibernate入门实例

来源:互联网 发布:专车软件有哪些 编辑:程序博客网 时间:2024/04/28 03:22

说明:本实例使用的是mysql数据库

1、下载hibaernate开发包:http://hibernate.org/orm/downloads/;

2、连接数据库需要的jar文件,不同数据库需要下载不同的jar文件;

3、解压下载的hibernate,将hibernate-release-4.2.17.Final\lib\required中的jar文件导入项目中,如何创建java项目这里不再详细说明;

4、在项目根目录创建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.username">zhou</property>          <!--连接数据库的密码-->        <property name="connection.password">123456</property>           <!--连接数据库的url-->        <property name="connection.url">jdbc:mysql://localhost:3306/user</property>                 <!-- JDBC connection pool (use the built-in)         <property name="connection.pool_size">1</property>-->        <!-- SQL dialect -->         <!--数据库方言-->        <property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</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.internal.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/lanqiao/model/Course.hbm.xml"/>-->       <!--映射文件路径-->     <mapping class="com.lanqiao.model.Course"/>    </session-factory></hibernate-configuration>


4、配置文件的各数据库对应的方言如下:

Microsoft SQL Server 2000                 org.hibernate.dialect.SQLServerDialect
Microsoft SQL Server 2005                 org.hibernate.dialect.SQLServer2005Dialect
Microsoft SQL Server 2008                 org.hibernate.dialect.SQLServer2008Dialect
Microsoft SQL Server 2012                 org.hibernate.dialect.SQLServer2012Dialect
MySQL                                                org.hibernate.dialect.MySQLDialect
MySQL with InnoDB                            org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM                            org.hibernate.dialect.MySQLMyISAMDialect
MySQL5                                               org.hibernate.dialect.MySQL5Dialect
MySQL5 with InnoDB                           org.hibernate.dialect.MySQL5InnoDBDialect
Oracle 8i                                              org.hibernate.dialect.Oracle8iDialect
Oracle 9i                                             org.hibernate.dialect.Oracle9iDialect
Oracle 10g and later                          org.hibernate.dialect.Oracle10gDialect

5、在对应的实体类包路径下创建对应的映射文件,映射文件命名格式:实体类名.hbm.xml,例如:User.hbm.xml,该映射文件和对应的实体类在同一目录下;第三步中配置的映射文件对应的就是该文件的路径;

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.bjsxt.hibernate.model"> <!--name为实体类名,默认和数据库表名一致,不同时添加table属性设置表名--> <class name="User">     <!--主键设置,name为实体属性名,column表字段名,和实体类属性相同时可以省略不写-->   <id name="username" column="username"></id>  <!--非主键表属性的映射-->   <property name="password"></property>   <property name="email"></property>   <property name="tel"></property>   <property name="content"></property>  </class></hibernate-mapping>


6、创建测试类

import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.Service;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import com.bjsxt.hibernate.model.User;public class StudentTest { public static void main(String[] args) {  User s=new User();  s.setUsername("lz2");  s.setPassword("123");  s.setEmail("aqeq@qq.com");  s.setContent("adad");  s.setTel("14214124");  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();   }}
7、在数据库中查询对应的表,查到对应的数据则说明插入成功。
 
第一次写博客,有的地方写的不够好,自己以后会好好更正,希望大家给出建议,谢谢!
0 0
原创粉丝点击