自動調用Spring的bean.xml配置文件 需要web.xml啟動文件 代碼如下: 其中調用了過濾器和監聽器 Spring核心配置文件bean.xml代碼 配置文件註入對象屬性,註意需要類當中聲明屬性並設置setter方法 層層調用 UserAction類代碼如下: UserService類代碼 ...
自動調用Spring的bean.xml配置文件
需要web.xml啟動文件
代碼如下:
<?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>2018-01-03_Spring_Hibernate</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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核心配置文件bean.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連接池得到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/sw_database"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </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> <bean id="userService" class="com.swift.service.UserService"> <property name="userDao" ref="userDaoImplements"></property> </bean> <bean id="userDaoImplements" class="com.swift.dao.UserDaoImplements"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <!-- 前一個sessionFactory是HibernateTemplate類內部的 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
配置文件註入對象屬性,註意需要類當中聲明屬性並設置setter方法
層層調用
UserAction類代碼如下:
package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.swift.service.UserService; public class UserAction extends ActionSupport { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { System.out.println("action.................."); userService.add(); return NONE; } }
UserService類代碼如下:
package com.swift.service; import com.swift.dao.UserDao; public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("UserService................."); userDao.add(); } }
UserDao介面代碼如下:
package com.swift.dao; public interface UserDao { public void add(); }
UserDao介面的實現類UserDaoImplement代碼如下:
package com.swift.dao; import org.springframework.orm.hibernate5.HibernateTemplate; import com.swift.entity.User; public class UserDaoImplements implements UserDao{ private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public void add() { User user=new User(); user.setUsername("small-fly"); user.setAddress("War of Mercenaries"); hibernateTemplate.save(user); } }
hibernate核心配置文件hibernate.cfg.xml文件代碼
<?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="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>
運行action出現問題
原因,jar包問題,重新整理OK的SSH jar包合集
下載包地址: