欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate入门Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

程序员文章站 2022-04-12 20:59:28
...

hibernate一般必要的步骤:导入jar包,设置配置文件,编写程序

1、jar包,先要导入数据库的驱动包,之后是hibernate必要的包,最后导入日志包

 2、设置配置文件

    首先Java的Bean对象需要设置映射文件,一般为xml文件,命名格式一般为XXX.hbm.xml

<?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="com.itheima.domain.Customer" table="cst_customer">
		<!-- 配置id
				name属性,Javabean的属性
				column属性,是表结构的字段
		 -->
		 <id name="cust_id" column="cust_id">
		 	<!-- 主键的生成策略 -->
		 	<generator class="native"></generator>
		 </id>
		 <!-- 配置其他属性 -->
		 <property name="cust_name" column="cust_name"/>
		 <property name="cust_user_id" column="cust_user_id"/>
		 <property name="cust_create_id" column="cust_create_id"/>
		 
		 <property name="cust_source" column="cust_source"/>
		 <property name="cust_industry" column="cust_industry"/>
		 <property name="cust_level" column="cust_level"/>
		 <property name="cust_linkman" column="cust_linkman"/>
		 
		 <property name="cust_phone" column="cust_phone"/>
		 <property name="cust_mobile" column="cust_mobile"/>
		 
	</class>
</hibernate-mapping>

之后是需要配置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">
<!-- 如果不能上网,编写配置文件是没有提示的,需要自己来配置  先复制http://www.... -> window -> 
	preferences -> 搜索xml -> 选择xml catalog -> 点击add -> 现在URI -> 粘贴复制的地址 ->
	 选择location,选择本地的DTD的路径 -->

<hibernate-configuration>
	<!-- 记住:先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 -->
	<session-factory>
		<!-- 必须要配置的有5个,4大参数,数据库方言 -->
		<!-- 四大参数 -->
			<!-- 驱动 -->
			<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
			<!-- 数据库地址 -->
			<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_day01</property>
			<!-- 数据库用户名 -->
			<property name="hibernate.connection.username">root</property>
			<!-- 数据库密码 -->
			<property name="hibernate.connection.password">root</property>
		
		<!-- 数据库的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
		
		<!-- 可选配置,以后慢慢添加 -->
		
		<!-- 映射配置文件,需要引入的配置文件(注意这里包之间用的是“/”) -->
		<mapping resource="com/itheima/domain/Customer.hbm.xml"/>
		
	</session-factory>
	
</hibernate-configuration>

3、编写程序就不用说了,这里先说一下编写的一个小小测试类吧

     /**一般步骤都如下:

* 1.先加载配置文件
* 2.创建SessionFactory对象,生成Session对象
* 3.创建Session对象
* 4.开启事务
* 5.编写保存代码
* 6.提交事务(事务回滚)
* 7.释放资源

*/

测试类如下:

package com.itheima.test;

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

import com.itheima.domain.Customer;

/**
 * 测试hibernate框架
 * @author BABY
 *
 */
public class Demo1 {
	/**
	 * 测试保存客户
	 */
	private static void testAdd() {
		/**
		 * 1.先加载配置文件
		 * 2.创建SessionFactory对象,生成Session对象
		 * 3.创建Session对象
		 * 4.开启事务
		 * 5.编写保存代码
		 * 6.提交事务(事务回滚)
		 * 7.释放资源
		 */
		//1.加载配置文件
		Configuration conf = new Configuration();
		//设置配置文件,默认加载src目录下的hibernate.cfg.xml的配置文件		
		conf.configure();    //这个一定要有,一点更要注意,否则会出现配置文件无法加载的问题,下面给个例子
		//2.建立SessionFactory对象
		SessionFactory factory = conf.buildSessionFactory();
		//3.打开session对象
		Session session = factory.openSession();
		//4.开启事务
		Transaction btr = session.beginTransaction();
		
		//5.编写保存代码
		Customer c = new Customer();
		c.setCust_level("20");
		c.setCust_name("测试一下");
		c.setCust_phone("12121212");
		c.setCust_industry("这是什么?");
		//5.1保存
		session.save(c);
		//6.提交事物
		btr.commit();
		//7.释放资源(session,SessionFactory)
		session.close();
		factory.close();
	}
	
	public static void main(String[] args) {
		testAdd();
	}
}
Configuration conf = new Configuration();
//设置配置文件,默认加载src目录下的hibernate.cfg.xml的配置文件		
conf.configure();    //这个一定要有,一点更要注意,否则会出现配置文件无法加载的问题,下面给个例子
//如果少了.configure();就会出现下面的异常,
//如果不使用configure()方法,这时hibernate会在classpath下面寻找hibernate.properties文件, 如果没有找到该文件,系统会打印如下信息并抛出HibernateException异常。

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at com.itheima.test.Demo1.testAdd(Demo1.java:34)
	at com.itheima.test.Demo1.main(Demo1.java:56)
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
	... 15 more

到这里基本就结束了:最后附上测试的Javabean对象,及数据库SQL语句

JavaBean对象

package com.itheima.domain;
/**
 * 客户的JavaBean
 * @author BABY
 *
 */
public class Customer {
/**
 * `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
		  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
		  `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
		  `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
		  
		  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
		  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
		  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
		  `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
		  
		  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
		  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
 */
	
	private Long cust_id;
	private String cust_name;
	private Long cust_user_id;
	private Long cust_create_id;
	
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_linkman;
	
	private String cust_phone;
	private String cust_mobile;
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public Long getCust_user_id() {
		return cust_user_id;
	}
	public void setCust_user_id(Long cust_user_id) {
		this.cust_user_id = cust_user_id;
	}
	public Long getCust_create_id() {
		return cust_create_id;
	}
	public void setCust_create_id(Long cust_create_id) {
		this.cust_create_id = cust_create_id;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_linkman() {
		return cust_linkman;
	}
	public void setCust_linkman(String cust_linkman) {
		this.cust_linkman = cust_linkman;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}	
}

数据库:

Create database hibernate_day01;
		Use hibernate_day01;
		CREATE TABLE `cst_customer` (
		  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
		  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
		  `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
		  `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
		  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
		  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
		  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
		  `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
		  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
		  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
		  PRIMARY KEY (`cust_id`)
		) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;