SSH整合
来源:互联网 发布:求一个矩阵的k次方 编辑:程序博客网 时间:2024/06/06 05:29
项目整体架构:
SSH整合步骤:
1.找jar包
2.建立source 文件夹:resource
3.在resource里面建立applicationContext.xml
导入头文件:spring-beans--->factory ---> xml ---->spring-beans-4.0.xsd
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>4.配置数据库文件:jdbc.properties
connection.driver_class com.mysql.jdbc.Driverconnection.url jdbc:mysql://127.0.0.1:3306/mydbconnection.username rootconnection.password 123456hibernate.dialect org.hibernate.dialect.MySQLDialecthibernate.show_sql truehibernate.format_sql truehibernate.hbm2ddl.auto update
5.装载数据源属性文件:
spring-beans-->factory ---> config---->PropertyPlaceholderConfigurer
<!-- 装载数据源属性文件 --> <bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties"/> </bean>
6.配置数据源
spring-jdbc--->org.springframework.jdbc--->datasource--->DriverManagerDataSource
<!-- 配置数据源 --> <bean id = "myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><!-- 使用属性注入 --> <property name="driverClassName" value="${connection.driver_class}"/> <property name="url" value="${connection.url}"/> <property name="username" value="${connection.username}"/> <property name="password" value="${connection.password}"/> </bean>
7.整合hibernate的sessionFactory到spring容器管理
spring-orm--->org.springframework.orm--->hibernate4---->LocalSessionFactoryBean
建立hibernate的映射文件:hbm/Book.hbm.xml,Student.hbm.xml
为Book.hbm.xml,Student.hbm.xml配置头文件:
在hibernate-core--->org.hibernate--->hibernate-mapping-3.0.dtd
Book.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 package="com.xkf.ssh.entity"> <class name = "Book" table = "book"> <id name = "bookId" column="bookId"> <generator class="native"/> </id> <property name="bookName"/> <property name="bookPrice"/> <many-to-one name="student" column="stuId"/> </class></hibernate-mapping>
Student.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 package="com.xkf.ssh.entity"> <class name="Student" table="student"> <id name="stuId" column="stuId"> <generator class="native"/> </id> <property name="stuName"/> <property name="stuPass"/> <property name="stuAge"/> <set name="books" inverse="true"> <key column="bookId"/> <one-to-many class="Book"/> </set> </class></hibernate-mapping>
找到property的name属性为mappingLocations:spring-orm---->org.springframework.orm---->hibernate4--->LocalSessionFactoryBean---->mappingLocations
找到property的name属性为:hibernateProperties:spring-orm---->org.springframework.orm---->hibernate4--->LocalSessionFactoryBean---->hibernateProperties
<!-- 整合hibernate的sessionFactory到spring容器管理 --><bean id = "mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="myDataSource"/> <!-- 整合hibernate的hbm.xml文件到SessionFactory对象 --> <property name="mappingLocations"> <list> <value>hbm/*.hbm.xml</value> </list> </property>
<!-- 添加额外属性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> </props> </property></bean>
8.使用aop,配置事务管理器
1)导入命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/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">
2)找到事务管理的属性:
spring-orm--->org.springframework.orm--->hibernate4---->HibernateTracsactionManager
<!-- 配置事务管理器 --><bean id ="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <!-- 注入sessionFactory对象 --> <property name="sessionFactory" ref="mySessionFactory"/></bean> <!-- 配置声明性事务,用aop对方法增强 --> <!-- 分发需要增强的方法的通知 --> <tx:advice id = "txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="query*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- 组装通知和切点 --> <aop:config> <!-- 配置切入点第一个*:代表的是拦截的方法的返回值类型 一般拦截的都是service包下的 service后面的一个点表示只能访问一级包,两个点表示可以访问service下面的所有子包 第二个*:表示service下面所有子包的所有类 第三个*:表示service下面所有子包的所有类的所有方法(..)表示service下面所有子包的所有类的所有方法的参数,第一个点是参数类型,第二个是参数个数 --> <aop:pointcut expression="execution(* com.xkf.ssh.service..*.*)" id="cut"/> <!-- 组装通知 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="cut"/> </aop:config>
到此,整个框架搭建完成。
在resource文件夹下面建立context文件夹:applicationContext-framework.xml,将之前所有的框架配置文件全部移动到这里,为了维护的方便,让applicationContext.xml里面只是引入框架文件即可。
<span style="font-size:14px;"><!-- 引入框架配置文件 --> <import resource="context/applicationContext-framework.xml"/></span>
在context文件夹下建applicationContext-student.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置dao层 --> <bean id = "stuDao" class="com.xkf.ssh.dao.StudentDaoImpl"> <!-- 注入sessionFactory ref引入spring容器里面配置好的sessionFactory--> <property name="sessionFactory" ref = "mySessionFactory"></property> </bean> <!-- 配置service层 --> <bean id="stuService" class="com.xkf.ssh.service.StudentServiceImpl"> <property name="stuDao" ref="stuDao"/> </bean> <!-- 配置action 注意:这里要记得加上scope属性为prototype,因为spring容器默认是单例模式,应该设置成多例模式,避免每次实现添加的时候都要重启服务器创建新的entity--> <bean id = "stuAction" class="com.xkf.ssh.action.StudentAction" scope="prototype"> <!-- 注入service --> <property name="stuService" ref="stuService"/> </bean></beans>
在resource文件夹里面建立config文件夹:struts-student.xml
<?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="student" extends="base" namespace="/student"> <action name="studentAction_*" class="stuAction" method="{1}"> <result>/index.jsp</result> </action> <action name="goToAdd"> <result>/WEB-INF/student/add.jsp</result> </action> </package></struts>
在resource里面建:struts.xml:
将struts2创建action的权利交给spring容器管理:
在struts2-core---->org.apacge.struts2--->default.properties里面找到常量:struts.objectFactory = spring
<span style="font-size:14px;"><?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> <!-- 常量配置 --> <!-- 把struts2创建action的权利交给spring容器管理 --> <constant name="struts.objectFactory" value="spring"/> <!-- 修改请求后缀 --> <constant name="struts.action.extension" value="do"/> <!-- 开启开发者模式 --> <constant name="struts.devMode" value="true"/> <!-- 自动加载xml文件 --> <constant name="struts.configuration.xml.reload" value="true"/> <!--导入外部文件 --> <include file="config/struts-student.xml"/> <!-- 配置基本包 --> <package name="base" extends="struts-default" abstract="true"> </package></struts></span>
spring容器应该在服务器启动的时候交给网站来实例化:
在web.xml文件里面配置:
在spring-web---->org.springframework---->web---->context--->ContextLoaderListener
<span style="font-size: 18px;"> </span><span style="font-size:14px;"><!-- 配置服务器启动自动实例化Spring容器 --> <!-- 配置web容器监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></span>
web.xml配置文件:
<span style="font-size:14px;"><!-- 配置struts2启动过滤器 --> <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>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping></span>
<span style="font-size:14px;"><?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/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssh</display-name> <!-- 配置struts2启动过滤器 --> <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>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- 配置服务器启动自动实例化Spring容器 --> <!-- 配置web容器监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置容器的初始化参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app></span>
index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>index</title></head><body>主页 <a href = "${pageContext.request.contextPath}/student/goToAdd.do">添加</a></body></html>
add.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>学生信息添加</title></head><body> <form action="${pageContext.request.contextPath}/student/studentAction_add.do" method="post"> 姓名:<input type="text" name = "student.stuName"><br/> 密码:<input type="password" name = "student.stuPass"><br/> 年龄:<input type="text" name = "student.stuAge"><br/> <input type="submit" value="添加"/> </form></body></html>
- SSH 整合
- SSH整合
- ssh整合
- SSH整合
- SSH 整合
- SSH整合
- ssh整合
- SSH整合
- ssh整合
- SSH整合
- SSH整合
- SSH整合
- ssh整合
- SSH整合
- Ssh整合
- ssh 整合
- SSH整合
- SSH整合
- mysql+python3
- 7 Managing Users and Securing the Database
- 图像缩放算法
- Android 新support库大集结
- InputMethodManagerService 学习摘要
- SSH整合
- Amaze UI定制
- php+xmapp中文乱码
- 使eclipse支持lombok.jar
- MSYS2镜像源
- Cocos2D游戏项目CCTableView在Xcode7.2下的无法滚动问题
- LeetCode 103:Binary Tree Zigzag Level Order Traversal
- online_judge_1150
- 《leetCode》:Decode Ways