您的位置:首页 > 其它

SSM整合搭建

2016-07-03 13:00 309 查看
1. 导入jar包

2. 配置Spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">

<context:component-scan base-package="com.bjsxt.smallming"></context:component-scan>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 自动扫描那个包,把包中的接口和mapper文件自动管理,注意,接口名和文件名相同,里面命名空间和id="" 要和方法对应上 -->
<property name="basePackage" value="com.bjsxt.smallming.mapper"></property>
<!-- 获取到名称,String类型,不要使用ref -->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>

<!-- 创建Hibernate事务管理类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 声明service.impl.X 下哪些方法被事务管理 -->
<!-- 设置当前方法和事务之间的关系 REQUIRED:如果当前已经有事务,使用当前事务,如果没有,创建事务。默认值 SUUPORTS:
如果当前已经有事务,使用当前事务,如果没有,则不使用事务 MANDATORY:必须有有事务,没有事务报异常 NEVER:必须没有事务,有的话报异常
NOT_SUPPORTED:要求执行方法时没有事务,如果有事务,挂起 REQUIRES_NEW:每次执行这个方法都创建新事务,如果已经有事物,创建嵌套子事务
read-only:true 当前方法只能读取,没有提交事务 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP,让某些方法与声明式事务进行关联 -->
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.smallming.service.impl.*.*(..))"
id="mypoint" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint" />
</aop:config>
</beans>
3. 配置Web.xml 加载spring配置文件,通过配置出SpringMVC的DispatcherServlet,如果出现解决中文乱码,也可以配置出来

<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>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 统一控制所有请求以.do结尾,否则jsp等内容可能被影响 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 请求参数中文乱码 -->
<filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4. 配置SpringMVC的配置文件
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.bjsxt.smallming"></context:component-scan>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 当为了保护jsp,把所有jsp都放入到web-info中时,前缀常用 -->
<property name="prefix" value="/"></property>
<!-- 注意,并不是每次跳转都是.jsp,如果不能控制每次都是jsp时,value为空 -->
<property name="suffix" value=""></property>
</bean>
</beans>

5. 配置MyBatis的全局配置文件
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.bjsxt.smallming.entity"/>
</typeAliases>
<mappers>
<package name="com.bjsxt.smallming.mapper"/>
</mappers>
</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: