1、应该在springmvc中配置的东西
1、对于使用注解的处理器方式,在类前用@Controller标识,表示这是一个可以用于处理请求的handler。
为了能够这样做,需要先扫描得到它,在springmvc.xml中配置扫描controller的包,注意,不要将service包也扫描进来,
service包下面可能有@Transactional事务配置注解,如果扫描进来,那么就按照普通bean来处理,达不到生成代理对象的目的,controller类上面和方法上面,不加@Transactional注解,事务管理在service中完成 。dao中是mapper接口文件,mapper.xml放在了web的resource下。也不存在@Transactional注解,在applicationContext-dao.xml中配置了context:property-placeholder,扫描properties文件,产生properties引用。然后配置dbcp的datasource,配置sqlSessionFactory,配置mapperScanConfigure,自动为dao包下的接口类,生成aop类bean。这样mybatis就完成了整合,以后直接在service包中,用自动注入的方式取得dao的bean,调用其中的方法进行数据库操作。对于事务处理,需要额外配置:在applicationContext-dao.xml中配置transactionManager,配置事务的注解驱动,上面的springmvc.xml配置的是处理器handerl,即Controller的注解驱动。dao中的bean由mapperScanConfigure完成了扫描到spring容器中。Service中的bean需要applicationContext-service.xml中加入<context:component-scan basepackage,指定扫包范围,来加载bean和自动注入依赖的dao层的bean。用@Resource依赖注入。事实上applicationContext-service.xml中就这一个扫包配置。
总结一下,在dao包中存放的是mybatis的mapper接口文件,mapper.xml映射文件放在了web的resource中,在分模块开发中web包下放controller,在resource中放spring配置xml,properties文件,mappers映射文件。在src-webapp-WEB-INF下放jsp文件
上一级目录放置statics静态文件。
在service层中配置@Service,@Transactional进行事务处理,事务处理的相关配置,数据源的配置,sqlSessionFactory的配置都在里面dao.xml中配置好了,service.xml只需配置扫描service组件的配置。
在springmvc.xml中需要先配置扫描controller的扫包配置<context:component-scan ,然后配置注解处理器适配器,注解处理器映射器。这两个的分别配置可以合成一个<mvc:annotation-driven配置,这个配置不仅配置了上面两个,而且还加载了很多参数绑定方法。就配置这两条就能用了,但是由于不配置视图解析器,在生成modelandview对象时,需要指定jsp的全路径名如:
ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/TEST.jsp");
可以通过配置视图解析器来简化配置
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
注意配置了视图解析器,就不能用全路径了,否则就成了这个样子
/WEB-INF/jsp//WEB-INF/jsp/TEST.jsp.jsp
在全路径/WEB-INF/jsp/TEST.jsp前面加了/WEB-INF/jsp后面加了.jap
其实视图解析器就是方便不用写全路径的,只需要写jsp文件名就可以了
上面就是ssm框架整合的基本配置了
其他的如果需要配置springmvc的各种组件,需要在springmvc.xml中专门配置,如果需要额外配置mybatis的settings,需要加一个mybatisConfig.xml然后再dao中加载SqlSessionFactory中引用这个配置。
有了上面的基本配置,然后需要统一在web.xml中注册扫描各个配置文件,注册servlet,listener,context-param
各文件如下所示:
1 web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2. springmvc.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:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- springmvc 扫包范围 -->
<context:component-scan base-package="com.anlysqx.controller" />
<!-- 配置了mvc:annotation-driven 代替下边注解映射器 -->
<!-- mvc:annotation-driven默认还加载了很多的参数绑定方法,比如
json转换解析器就默认加重了 -->
<mvc:annotation-driven/>
<!-- 视图跳转类型 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3. applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:properties/db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${MySQL_Driver}"></property>
<property name="url" value="${MySQL_Url}"></property>
<property name="username" value="${MySQL_User}"></property>
<property name="password" value="${MySQL_Pwd}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 别名 -->
<property name="typeAliasesPackage" value="com.anlysqx.entity"></property>
<property name="mapperLocations" value="classpath:mappings/*.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.anlysqx.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<aop:config proxy-target-class="true"></aop:config>
</beans>
4. applicationContext-service.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:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- springmvc 扫包范围 -->
<context:component-scan base-package="com.anlysqx.service" />
</beans>
maven pom 文件配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anlysqx</groupId>
<artifactId>anlysqx-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 数据源 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.14</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>