一、最原始的hibernate框架的配置(不整合spring)

版权声明:本篇文章由IT_CREATE整理 https://blog.csdn.net/IT_CREATE/article/details/87369196

一、首先需要引入相关jar包,这里是一个maven项目

pom.xml配置文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.ge</groupId>
	<artifactId>hibernatexml</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>

	<name>hibernatexml</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.12</junit.version>
		<log4j.version>1.2.17</log4j.version>
		<spring.version>4.3.14.RELEASE</spring.version>
		<hibernate.version>4.3.11.Final</hibernate.version>
		<mysql.version>5.1.38</mysql.version>
	</properties>

 
	<dependencies>

		<!-- 导入spring容器相关JAR文件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
	 		<version>${log4j.version}</version>
		</dependency>

		<!-- 导入hibernate持久层框架JAR包 -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
 

		<!-- 导入hibernate持久层框架的C3P0连接池JAR包 -->

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- 导入JDBC的JAR包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>

	</dependencies>




	<build>
		<pluginManagement>

			<!-- 配置maven 在构建项目时,采用相关插件 -->
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.8.0</version>
					<configuration>
						<source>1.8</source>
						<target>1.8</target>
					</configuration>
				</plugin>


			</plugins>
		</pluginManagement>
	</build>

</project>

二、开启spring的自动扫描功能

applicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
						http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--开启spring的自动扫描功能 -->
	<context:component-scan base-package="com.ge.hibernatexml"></context:component-scan>

</beans>

三、配置hibernate的基本连接信息

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>

		<!-- 定义数据库方言 -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
			<!-- org.hibernate.dialect.SQLServerDialect -->
			<!-- org.hibernate.dialect.OracleDialect -->
		</property>

		<!-- 配置JDBC连接数据库的4个最基本的元素 -->
		<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
		<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8</property>
		<property name="connection.username">root</property>
		<property name="connection.password">1234</property>

		<!-- 使用C3P0作为连接池技术 -->
		<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
		<!-- 配置C3P0连接池中,最大连接数为10 -->
		<property name="hibernate.c3p0.max_size">10</property>
		<!-- 指定连接池最小连接数 -->
		<property name="hibernate.c3p0.min_size">1</property>
		<!-- 指定连接池里连接超时时长 -->
		<property name="hibernate.c3p0.timeout">5000</property>
		<!-- 指定连接池里最大缓存多少个PreparedStatement对象 -->
		<property name="hibernate.c3p0.max_statements">100</property>
		<!-- 指定校验空闲连接的时间为:半分钟 -->
		<property name="hibernate.c3p0.idle_test_period">30000</property>
		<!-- 当连接不足时,需要再次获取的连接数量 -->
		<property name="hibernate.c3p0.acquire_increment">5</property>
		
		<!--显示SQL语句-->
		<property name="show_sql">true</property>
		<!--以格式良好的方式显示SQL语句-->
		<property name="format_sql">true</property>
		
		<!-- 如果使用resource,从com包开始写,引入对象与数据库关联的配置文件,参见下面:二   引入多个对象配置文件就在后面追加就可以了-->
		<mapping resource="com/ge/hibernatexml/xml/UserBean.hbm.xml"/>
    <!--<mapping resource="com/gezhi/hibernatexml/xml/TeacherBean.hbm.xml"/>-->

	</session-factory>


</hibernate-configuration>

二、hibernate中关于对象与数据库中数据的对应关系配置文件的编写(xx.hbm.xml配置文件的编写):https://blog.csdn.net/IT_CREATE/article/details/87370051

猜你喜欢

转载自blog.csdn.net/IT_CREATE/article/details/87369196
今日推荐