ssh整合思想 Spring与Hibernate的整合 项目在服务器启动则自动创建数据库表
Spring整合Hibernate
Spring的Web项目中,web.xml文件会自动加载,以出现欢迎首页。也可以在这个文件中对Spring的配置文件进行监听,自启动配置文件,
以及之前Struts2框架必备的启动过滤器StrutsPrepareAndExecuteFilter
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>2017-12-30_SSH</display-name> <!-- 监听的文件名 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <!-- 服务器启动自动加载XML配置文件 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- structs2过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
在Spring的核心配置文件中,进行数据库连接池配置,建立sessionFactory对象,直接dao操作,也可以在Spring配置文件中配置Struts2的Action对象
<?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连接池得到dataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lastday"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- Hibernate核心配置文件没有连接数据库,所以需要注入 --> <property name="dataSource" ref="dataSource"></property> <!-- Hibernate核心配置文件的位置 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean> </beans>
Hibernate的核心配置文件中,不需要在写连接数据库的属性,因为已经在Spring的配置文件中用连接池了
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/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://localhost:3306/sw_database</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property> <property name="hibernate.show_sql">true</property> <!-- create: 先删表,再建表。 create-drop: 启动时建表,退出前删表。 update: 如果表结构不一致,就创建或更新。 validate: 启动时验证表结构,如果不致就抛异常。 --> <property name="hibernate.hbm2ddl.auto">update</property> <!--指定映射文件,可映射多个映射文件 --> <mapping resource="com/swift/entity/User.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Hibernate的这个核心配置文件需要实体类映射文件,体现映射关系
<?xml version="1.0" encoding='UTF-8'?> <!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.swift.entity.User" table="t_user"> <!-- 主键 --> <id name="uid"> <generator class="native"></generator> </id> <!-- 其他属性 --> <property name="username"/> <property name="address"/> </class> </hibernate-mapping>
实体类的属性设置setter和getter方法即可
package com.swift.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; } }
Struts2的UserAction类代码
package com.swift.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("action.................."); return NONE; } }
需要继承ActionSupport类,覆写execute()方法
在浏览器中运行项目后的地址,加上对象名.action(如userAction.action)就可以了
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <!-- action的class不要写全名会创建两个对象,而写Spring配置文件中id的内容,只建一个对象 前提有struts2-spring-plugin-2.3.4.1.jar --> <action name="userAction" class="userAction"> </action> </package> </struts>
Never waste time any more, Never old man be a yong man