SSM整合
整合策略
- 先Spring + Mybatis
- 再整合Spring mvc
示例需求:查询user表全部数据并展示的页面
1. Spring + Mybatis
-
整合目标
- 数据库连接池及事务管理器都交给Spring容器完成
- SqlSessionFactory对象放到Spring容器中,作为单例对象管理
- Mapper动态代理对象交给Spring管理,我们从容器中直接获得Mapper代理对象
-
整合所需依赖
- junit
- mybatis
- Spring(spring-context,spring-test,spring-jdbc,spring-tx,spring-aop,aspectjweaver)
- Mybatis-spring-**.jar
- mysql驱动
- Druid数据库连接池
-
pom
看示例代码 ssm-demo https://gitee.com/ixinglan/spring-demo.git
2.整合Srpign MVC
-
pom
<dependencies> <!-- junit--> <!-- mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- spring, 父pom中有的,这里不重复添加--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.12.RELEASE</version> </dependency> <!-- mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!-- mysql-connector-java--> <!-- druid--> <!-- spring webmvc--> <!-- servlet-api--> <!-- jsp-api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <!-- jstl表达式--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- jackson-core--> <!-- jackson-databind--> <!-- jackson-annotations--> </dependencies>
-
controller
@Controller @RequestMapping("/account") public class AccountController { /** * Spring容器和SpringMVC容器是有层次的(父子容器) * Spring容器:service对象+dao对象 * SpringMVC容器:controller对象,,,,可以引用到Spring容器中的对象 */ @Autowired private AccountService accountService; @RequestMapping("/queryAll") @ResponseBody public List<Account> queryAll() throws Exception { return accountService.queryAccountList(); } }
-
springmvc.sml
<?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:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <context:component-scan base-package="org.ssm.demo.controller"/> <!--配置springmvc注解驱动,自动注册合适的组件handlerMapping和handlerAdapter--> <mvc:annotation-driven/> </beans>
-
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>ssm demo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext*.xml</param-value> </context-param> <!-- spring框架启动--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 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*:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>