欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM集成

程序员文章站 2022-07-09 19:53:17
...

单独集成Spring

1.创建javaweb项目
2.导入springjar
3.准备配置文件applicationContext.xml,并且配置对象
4.测试
能从spring中或bean就ok

public class SpringTest extends BaseTest
{

    @Autowired
    private Date myDate;
    @Test
    public void test()throws Exception{

        System.out.println(myDate);
    }
}

Spring+SpringMVC

导入jar
配置web.xml
字符编码过滤器
方案1:子从上下文

<?xml version="1.0" encoding="UTF-8"?>

<!--方案1:独立容器方案,spring和springmvc都有自己context-->
<!--1.1 spring容器初始化-->
<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>

<!--1.2 配置springmvc-->
<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:applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--/*会拦截所有包括jsp-->
    <!--/会拦截所有但是不包含jsp-->
    <url-pattern>/</url-pattern>
</servlet-mapping>

<!--
    post请求乱码解决
   get请求实在tomcat猫中配置
-->
<filter>
    <filter-name>EncodingFilter</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>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!--方案2:只有儿子容器-->
<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:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--/*会拦截所有包括jsp-->
    <!--/会拦截所有但是不包含jsp-->
    <url-pattern>/</url-pattern>
</servlet-mapping>

SSM集成SSM集成

applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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">
    <!--扫描controller-->
    <context:component-scan base-package="cn.itsource.ssm.controller" />
    <!--静态资源处理-->
    <mvc:default-servlet-handler />
    <!--识别@requestMapper等注解支持-->
    <mvc:annotation-driven />
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

Spring集成到MyBatis

jdbc.properties—>Datasource ---->SqlSessionFactory----扫描Repository—>事务管理器-开启注解事务

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入数据源 -->
	<property name="dataSource" ref="dataSource" />
	<!-- 配置mybatis (mapper)映射器路径 -->
	<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml" />
	<!-- 配置mybatis 类型别名 -->
	<property name="typeAliasesPackage">
		<value>
			cn.itsource.ssm.domain
			Cn.itsource.ssm.query  可能有查询对象
		</value>
	</property>
</bean>

一个一个配置

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<property name="mapperInterface" value="cn.itsource.ssm.mapper.UserMapper" />
	</bean>
一劳永逸
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.itsource.ssm.mapper"></property>
	</bean>