spring_day4_04-ssh整合(spring整合hibernate)

项目结构:




com.hlg.entity.User

package com.hlg.entity;

public class User {

	private Integer uid;
	private String username;
	private String address;
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	
}

/spring_day04_sshdemo1/src/com/hlg/entity/User.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>
	<!-- 1 配置类和表对应 
		class标签
		name属性:实体类全路径
		table属性:数据库表名称
	-->
	<class name="com.hlg.entity.User" table="t_user">
		<id name="uid" column="uid">
			<generator class="native"></generator>
		</id>
		<property name="username" column="username"></property>
		<property name="address" column="address"></property>

	</class>
</hibernate-mapping>

com.hlg.utils.HibernateUtils

package com.hlg.utils;

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

public class HibernateUtils {

	static Configuration cfg = null;
	static SessionFactory sessionFactory = null;
	
	//静态代码块实现
	static{
		//加载核心配置文件
		cfg = new Configuration();
		cfg.configure();
		sessionFactory = cfg.buildSessionFactory();
		
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static void main(String[] args){}
}


/spring_day04_sshdemo1/src/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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  	<property name="hibernate.connection.url">jdbc:mysql://*.36.138:3306/spring_day04?userUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull</property>
  	<property name="hibernate.connection.username">root</property>
  	<property name="hibernate.connection.password">*</property> -->
  	
  	<!-- 第二部分:配置hibernate信息  可选的 -->
  	<!-- 输出底层 sql语句  -->
  	<property name="hibernate.show_sql">true</property>
  	<!-- 输出底层 sql语句的格式  -->
  	<property name="hibernate.format_sql">true</property>
  	<!-- hibernate 创建表,需要配置之后
  		update:如果已经有表,就更新;如果没有,创建
  	 -->
  	<property name="hibernate.hbm2ddl.auto">update</property>
  	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  	
  	<!-- 第三部分 :把映射文件放到核心配置文件中  必须 -->
  	<mapping resource="com/hlg/entity/User.hbm.xml"/>
  	
  	
  </session-factory>

</hibernate-configuration>

/spring_day04_sshdemo1/src/spring3.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: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.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">
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<!-- & 需要转义成 & -->
		<property name="jdbcUrl" value="jdbc:mysql://*.36.138:3306/spring_day04?userUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull" />
		<property name="user" value="root"></property>
		<property name="password" value="*"></property>
	</bean>
	
	<!-- spring整合hibernate -->
	<!-- sessionFactory创建交给spring管理 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 因为在hibernate核心配置文件中,没有数据库配置,数据库配置在spring里面配置,注入dataSource -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 指定使用hibernate核心配置文件 -->
		<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
	</bean>
	
	<!-- 配置action对象 -->
	<bean id="userAction" class="com.hlg.action.UserAction" scope="prototype">
	
	</bean>

</beans>

启动时,看数据库的表是否重新创建


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值