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:子从上下文
<!--方案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>
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>
推荐阅读
-
PHP与已存在的Java应用程序集成_PHP
-
5分钟集成桌面摄像头或高拍仪到Web程序
-
在Eclipse JavaEE开发环境上集成GlassFish开发插件(截图多多)
-
SpringBoot与Quartz集成实现分布式定时任务集群的代码实例
-
springboot集成rabbitMQ之对象传输的方法
-
Springboot2.X集成redis集群(Lettuce)连接的方法
-
使用eclipse + maven一步步搭建SSM框架教程详解
-
iOS Crash常规跟踪方法及Bugly集成运用详细介绍
-
SpringBoot集成slf4j+log4j2的示例代码
-
SpringBoot集成ElaticJob定时器的实现代码