织梦素材网站模板,重庆外贸网站建设公司排名,新闻类wordpress模板下载,做集群网站一、Hibernate的开发步骤 1、引入jar文件2、配置3、apihibernate的映射文件的配置是不容易的#xff0c;是重点学习的地方。二、Hello Hibernate
1、数据库表准备 数据库名 #xff1a;test表#xff1a;DROP TABLE IF EXISTS users;
CREATE TABLE users (id int(11) NOT N…一、Hibernate的开发步骤
1、引入jar文件2、配置3、apihibernate的映射文件的配置是不容易的是重点学习的地方。二、Hello Hibernate
1、数据库表准备
数据库名 test表DROP TABLE IF EXISTS users;
CREATE TABLE users (id int(11) NOT NULL auto_increment,name varchar(255),birthday date,PRIMARY KEY (id)
);ps:主键idint类型必须设置 auto_increment 自增长。如果不设置hibernate在操作mysql数据库保存数据时会报错误java.sql.SQLException: Field id doesnt have a default value 2、引入jar文件
*hibernate3.6 hibernate3.jar核心 required文件夹里的jar jpa文件夹里的jar 数据库驱动包。hibernate3.jar核心hibernate-distribution-3.6.0.Final \ hibernate3.jar required文件夹里的jarhibernate-distribution-3.6.0.Final \ lib \ required jpa文件夹里的jarhibernate-distribution-3.6.0.Final \ lib \ jpa 数据库驱动包本例用mysql。 --------------------必须的包如下9个---------------------------antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate3.jar javassist-3.12.0.GA.jar jta-1.1.jar mysql-connector-java-5.1.8-bin.jar slf4j-api-1.6.1.jar3、写对象以及对象的映射
User.javapackage hello.hibernate;import java.util.Date;public class User {private int userId;private String username;private Date birthday;//省略构造getset方法
}User.hbm.xml ?xml version1.0?
!DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//ENhttp://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
hibernate-mapping packagehello.hibernateclass nameUser tableusers!-- 主键映射 --id nameuserId columnidgenerator classnative //id!-- 非主键映射 --property nameusername columnname/propertyproperty namebirthday columnbirthday/property/class
/hibernate-mapping PShibernate-mapping packagehello.hibernate 的 package必须写如果不写会报Could not parse mapping document from resource hello/hibernate/User.hbm.xml4、src/hibernate.cfg.xml 主配置文件
1、数据库连接配置2、加载所用的映射(*.hbm.xml)!DOCTYPE hibernate-configuration PUBLIC-//Hibernate/Hibernate Configuration DTD 3.0//ENhttp://www.hibernate.org/dtd/hibernate-configuration-3.0.dtdhibernate-configurationsession-factory!-- 数据库连接配置 --property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/propertyproperty namehibernate.connection.urljdbc:mysql:///test/propertyproperty namehibernate.connection.usernameroot/propertyproperty namehibernate.connection.password123456/property!-- sql方言告诉hibernate你在用哪个数据库 --property namehibernate.dialectorg.hibernate.dialect.MySQL5Dialect/propertyproperty nameshow_sqltrue/property!-- 加载所用的映射(*.hbm.xml) --mapping resourcehello/hibernate/User.hbm.xml//session-factory
/hibernate-configuration 5、测试类编写使用api Testpublic void test(){User usernew User();user.setUsername(hello);user.setBirthday(new Date());//获取加载配置文件的管理类对象Configuration confignew Configuration();//读取配置文件默认读取加载src/hibernate.cfg.xmlconfig.configure();//创建session工厂对象SessionFactory sfconfig.buildSessionFactory();//创建会话对象代表与数据库的一次会话Session sessionsf.openSession();//开启事务Transaction txsession.beginTransaction();//保存数据session.save(user);//提交事务tx.commit();//关闭会话session.close();//关闭会话工厂sf.close();}效果控制台打印出hibernate执行了的sql语句mysql数据库test--users表多了一条记录