基于maven搭建hibernate运行环境
Posted 成那个林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于maven搭建hibernate运行环境相关的知识,希望对你有一定的参考价值。
准备案例需要的数据库表和测试数据
建表语句:
create table DEPARTMENT ( DEPT_ID integer not null, DEPT_NAME varchar(255) not null, DEPT_NO varchar(20) not null, LOCATION varchar(255), primary key (DEPT_ID), unique (DEPT_NO) ); create table EMPLOYEE ( EMP_ID bigint not null, EMP_NAME varchar(50) not null, EMP_NO varchar(20) not null, HIRE_DATE date not null, IMAGE longblob, JOB varchar(30) not null, SALARY float not null, DEPT_ID integer not null, MNG_ID bigint, primary key (EMP_ID), unique (EMP_NO) ); create table SALARY_GRADE ( GRADE integer not null, HIGH_SALARY float not null, LOW_SALARY float not null, primary key (GRADE) ); create table TIMEKEEPER ( Timekeeper_Id varchar(36) not null, Date_Time datetime not null, In_Out char(1) not null, EMP_ID bigint not null, primary key (Timekeeper_Id) ); alter table EMPLOYEE add index FK75C8D6AE269A3C9 (DEPT_ID), add constraint FK75C8D6AE269A3C9 foreign key (DEPT_ID) references DEPARTMENT (DEPT_ID); alter table EMPLOYEE add index FK75C8D6AE6106A42 (EMP_ID), add constraint FK75C8D6AE6106A42 foreign key (EMP_ID) references EMPLOYEE (EMP_ID); alter table EMPLOYEE add index FK75C8D6AE13C12F64 (MNG_ID), add constraint FK75C8D6AE13C12F64 foreign key (MNG_ID) references EMPLOYEE (EMP_ID); alter table TIMEKEEPER add index FK744D9BFF6106A42 (EMP_ID), add constraint FK744D9BFF6106A42 foreign key (EMP_ID) references EMPLOYEE (EMP_ID);
初始化数据
insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION) values (10, \'ACCOUNTING\', \'D10\', \'NEW YORK\'); insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION) values (20, \'RESEARCH\', \'D20\', \'DALLAS\'); insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION) values (30, \'SALES\', \'D30\', \'CHICAGO\'); insert into Department (DEPT_ID, DEPT_NAME, DEPT_NO, LOCATION) values (40, \'OPERATIONS\', \'D40\', \'BOSTON\'); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7839, \'KING\', \'E7839\', Str_To_Date(\'17-11-1981\', \'%d-%m-%Y\'), \'PRESIDENT\', 5000, 10, null); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7566, \'JONES\', \'E7566\', Str_To_Date(\'02-04-1981\', \'%d-%m-%Y\'), \'MANAGER\', 2975, 20, 7839); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7902, \'FORD\', \'E7902\', Str_To_Date(\'03-12-1981\', \'%d-%m-%Y\'), \'ANALYST\', 3000, 20, 7566); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7369, \'SMITH\', \'E7369\', Str_To_Date(\'17-12-1980\', \'%d-%m-%Y\'), \'CLERK\', 800, 20, 7902); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7698, \'BLAKE\', \'E7698\', Str_To_Date(\'01-05-1981\', \'%d-%m-%Y\'), \'MANAGER\', 2850, 30, 7839); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7499, \'ALLEN\', \'E7499\', Str_To_Date(\'20-02-1981\', \'%d-%m-%Y\'), \'SALESMAN\', 1600, 30, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7521, \'WARD\', \'E7521\', Str_To_Date(\'22-02-1981\', \'%d-%m-%Y\'), \'SALESMAN\', 1250, 30, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7654, \'MARTIN\', \'E7654\', Str_To_Date(\'28-09-1981\', \'%d-%m-%Y\'), \'SALESMAN\', 1250, 30, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7782, \'CLARK\', \'E7782\', Str_To_Date(\'09-06-1981\', \'%d-%m-%Y\'), \'MANAGER\', 2450, 30, 7839); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7788, \'SCOTT\', \'E7788\', Str_To_Date(\'19-04-1987\', \'%d-%m-%Y\'), \'ANALYST\', 3000, 20, 7566); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7844, \'TURNER\', \'E7844\', Str_To_Date(\'08-09-1981\', \'%d-%m-%Y\'), \'SALESMAN\', 1500, 30, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7876, \'ADAMS\', \'E7876\', Str_To_Date(\'23-05-1987\', \'%d-%m-%Y\'), \'CLERK\', 1100, 20, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7900, \'ADAMS\', \'E7900\', Str_To_Date(\'03-12-1981\', \'%d-%m-%Y\'), \'CLERK\', 950, 30, 7698); insert into Employee (EMP_ID, EMP_NAME, EMP_NO, HIRE_DATE, JOB, SALARY, DEPT_ID, MNG_ID) values (7934, \'MILLER\', \'E7934\', Str_To_Date(\'23-01-1982\', \'%d-%m-%Y\'), \'CLERK\', 1300, 10, 7698); insert into Salary_Grade (GRADE, HIGH_SALARY, LOW_SALARY) values (1, 9999, 3001);
使用idea新建一个空的maven工程,并在该项目中增加hibernate的依赖、jdbc驱动依赖、log4j日志依赖、junit依赖。
该项目的pom.xml文件依赖内容如下:
<dependencies> <!--junit依赖,用于单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!--添加hibernate的核心依赖--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.0.12.Final</version> </dependency> <!--添加mysql的jdbc驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <!--添加log4j依赖--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
在resource目录下新建hibernate配置文件,hibernate.cfg.xml,该配置文件为hibernate的全局配置文件,用于定义hibernate的一些全局配置。目前仅配置数据库连接的相关信息
<?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.url">jdbc:mysql://localhost:3306/dept?serverTimezone=UTC</property> <property name="connection.username">root</property> <property name="connection.password">12345678</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect ,定义数据库方言--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate\'s automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> </session-factory> </hibernate-configuration>
每种数据库都有一个单独的方言, 例如:
Oracle方言:
- org.hibernate.dialect.Oracle10gDialect(Dùngcho 10g&11g)
- org.hibernate.dialect.Oracle12cDialect
SQL Server方言:
- org.hibernate.dialect.SQLServerDialect并
- org.hibernate.dialect.SQLServer2012Dialect
- org.hibernate.dialect.SQLServer2008Dialect
MySQL方言
- org.hibernate.dialect.MySQLDialect
- org.hibernate.dialect.MySQL5Dialect
什么是方言?
Dialect
是一个使用Hibernate的方式将数据库的数据类型转换为Java的数据类型,反之亦然。此外,它用于定义将HSQL(Hibernate SQL)的函数转换为数据中的函数的方式,如下列出的一部分 -
Java SQL类型 | Oracle | MySQL | SQL Server |
---|---|---|---|
Types.BIT | number(1,0) | bit | bit |
Types.BIGINT | number(19,0) | bigin | bigint |
Types.DATE | date | date | date |
……. | … | … | … |
Types.CLOB | clob | longtext | varchar(MAX) |
Types.BLOB | blob | longblob | varbinary(MAX) |
创建HibernateUtils类,用于加载hibernate配置信息,以及提供获取session的方法,代码如下:
package com.deng.hibernate.util; import com.deng.hibernate.bean.Department; import com.deng.hibernate.bean.Employee; import org.hibernate.Session; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; /** * hibernate工具类 */ public class HibernateUtils { private static org.hibernate.SessionFactory sessionFactory; private static Configuration configuration = new Configuration(); private static StandardServiceRegistry serviceRegistry; static { try { configuration.configure("hibernate.cfg.xml"); serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } /** * 获取session,所有的hibernate操作均基于session进行 * @return */ public static Session getSession(){ return sessionFactory.openSession(); } }
若运行main方法结果出现
代表环境搭建成功。
以上是关于基于maven搭建hibernate运行环境的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate基于Maven和Struts2搭建的增删改查
使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
Maven搭建SSH(Struts2+Spring+Hibernate)框架入门教程_1
Android 逆向修改运行中的 Android 进程的内存数据 ( 运行环境搭建 Android 模拟器安装 | 拷贝 Android 平台可执行文件和动态库到 /data/system )(代码片