spring_day4_04-ssh整合(spring整合hibernate)

来源:互联网 发布:华为软件开发招聘 编辑:程序博客网 时间:2024/06/08 22:14

项目结构:




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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://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>

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


原创粉丝点击