SSH整合:Spring整合Hibernate并生成数据库

2024-11-01 22:01:49

1、新建一个Maven web项目:ssh。

SSH整合:Spring整合Hibernate并生成数据库

3、在web.xml文件中配置spring的listener:<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>

SSH整合:Spring整合Hibernate并生成数据库

5、加入hibernate,mysql数据库依赖包:<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.4.Final</version> </dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.2</version> </dependency>

SSH整合:Spring整合Hibernate并生成数据库

7、建立持久化类和其对应的.hbm.xml文件。<?垓矗梅吒xml version="1.0"?><!DOCTYPE h足毂忍珩ibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.gwolf.ssh.entity.Department" table="ssh_department"> <id name="id"> <column name="id" /> <generator class="native"/> </id> <property name="departmentName"> <column name="department_name"/> </property> </class></hibernate-mapping><?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.gwolf.ssh.entity.Employee" table="ssh_employee"> <id name="id"> <generator class="native"/> </id> <property name="lastName"> <column name="last_name" </property> <property name="email"> </property> <property name="birth" type="java.util.Date"> </property> <property name="createTime" type="java.util.Date"> <column name="create_time"/> </property> <!--单向n-1关联关系--> <many-to-one name="department" class="com.gwolf.ssh.entity.Department"> <column name="department_id"/> </many-to-one> </class></hibernate-mapping>

SSH整合:Spring整合Hibernate并生成数据库

8、hibernate和spring进行整合,在maven中加入c3p0依赖包。<!-- 数据库连接池,驱动 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1</version> </dependency>

SSH整合:Spring整合Hibernate并生成数据库

10、hibernate和spring进行整合,配置数据源。<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:property-placeholder location="classpath:dbconfig.properties"/> <!-- Spring的配置文件,主要配置和业务逻辑有关的 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> </beans>

11、hibernate和spring进行整合配置SessionFactory。<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="comboPooledDataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <property name="mappingLocations" value="classpath:com/gwolf/ssh/entity/*.hbm.xml"/> </bean>

SSH整合:Spring整合Hibernate并生成数据库

13、启动项目,会看到生成的数据表。

猜你喜欢