hibernate框架SQLServer配置,MySQL8.0配置,以及持久化类的配置

SQLServer配置

<?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="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databasename=User</property>
		<property name="hibernate.connection.username">sa</property>
		<property name="hibernate.connection.password">123456</property>
		<!-- 配置Hibernate的相关属性 -->
		<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
		<!-- 可选属性 -->
		<!-- 显示SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- hbm2ddl -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		<!--c3p0-->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">120</property>
        <property name="automaticTestTable">Test</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">120</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="c3p0.testConnectionOnCheckout">true</property>
        <property name="c3p0.idleConnectionTestPeriod">18000</property>
        <property name="c3p0.maxIdleTime">25000</property>
        <property name="c3p0.idle_test_period">120</property>
		<!-- 加载映射文件: -->
		<mapping resource="bean/Type.hbm.xml"/>
		<mapping resource="bean/Users.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

MySQL8.0配置

<?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要连接的数据库信息 -->
<hibernate-configuration>
		 <session-factory> <!-- 生产session的工厂   session是connection -->
		 		<!-- 数据的驱动 -->
		 		<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
		 		<!-- 数据库的地址 
		 			jdbc:mysql:///test ==jdbc:mysql://localhost:3306/test
		 		-->
		 		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useSSL=false&amp;serverTimezone=UTC</property>
		 		<!-- 数据库的用户名  -->
		 		<property name="hibernate.connection.username">root</property>
		 		<!-- 数据库的密码 -->
		 		<property name="hibernate.connection.password">123</property>
		 		<!-- 数据库的方言
		 			 分页: 
		 			   mysql:     select * from 表  limit ?,?
		 			   sqlserver: select * from 表  top ?,?
		 			   让hibernate生成符合我mysql数据库的sql语句 
		 		 -->
		 		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		 	
		 		<!-- 告诉hibernate要用c3p0 -->
		 		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		 		<!-- hibernate自动生成的sql语句在控制台显示出来 -->
		 		<property name="hibernate.show_sql">true</property>
		 		<!-- 显示的sql语句更加的格式化 -->
		 		<property name="hibernate.format_sql">true</property>
		 		
		 		<!-- 让hibernate根据映射关系自动生成数据库的表 默认hibernate不会主动创建表 
		 			 create:没有表创建表 有表删除掉创建表
		 			 create-drop:没有表创建表 有表删除掉创建表 用完就全删
		 			 					 做测试
		 			
		 			 update: 企业开发使用  没有表 创建表  有表 使用表
		 			 validate:默认 不创建
		 		-->
		 		<property name="hibernate.hbm2ddl.auto">update</property>
		 		
		 		<!-- 配置和当前线程绑定的session进行开启配置 -->
				<property name="hibernate.current_session_context_class">thread</property>
				
		 		<!-- 加载映射文件(Customer.hbm.xml)的地址 -->
		 		<mapping resource="bean/Customer.hbm.xml"/>
		 </session-factory>

</hibernate-configuration>

持久化类的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
		<!-- 
			class标签: 作用类和表的映射的
		 		name:类的全限定名(bean.Customer)
		 		table:表的全名(cst_customer)
		 -->
		<class name="bean.Customer" table="cst_customer">
			<!-- 
				id标签:做类中的某个属性 和 表的主键映射关系
				 name:类的某个属性名
				 column:表的主键字段名
			 -->
			<id name="cust_id" column="cust_id">
				<!-- 做主键的增长方式的   
					  native: AUTO_INCREMENT 让主键自动增长 -->
				<generator class="native"></generator>
			</id>
			
			<!-- 
				property标签:做其它属性和其它字段的映射关系 
				   name属性:类的其它属性名
				   column属性:表的其它字段名
				   ps:如果属性名和字段名一致 column可以省略不写
			-->
			<property name="cust_name" column="cust_name" ></property>
			<property name="cust_source" column="cust_source"></property>
			<property name="cust_industry" column="cust_industry"></property>
			<property name="cust_level" column="cust_level"></property>
			<property name="cust_address" column="cust_address"></property>
			<property name="cust_phone" column="cust_phone"></property>
		</class>
</hibernate-mapping>

hibernate工具类配置

package utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	static Configuration configuration=null;
	static SessionFactory sessionFactory=null;
	static Session session=null;
	static {
		configuration=new Configuration();
		configuration.configure();
		sessionFactory=configuration.buildSessionFactory();
		
	}
	public static Session openSession() {
		return sessionFactory.openSession();
	}
	//获取当前线程的连接
	// 好处: 在多层之间调用方法获取的都是同一个session
	/*特点: 1 默认是关闭的 需要配置开启
	   		2 会自动给你关闭连接
	   		不需要写session.close();
	  注意:hibernate.cfg.xml里面要配置getCurrentSession
	  <property name="hibernate.current_session_context_class">thread</property>
	   */
	public static Session getSession() {
		return sessionFactory.getCurrentSession();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_43122641/article/details/89005493
今日推荐