springMvc-09 SSM框架整合
框架整合的方便之处
1)单独使用myBatis的时候,要操作数据库,每次都需要通过SqlSessionFactory和SqlSession方式获取Mapper对象
2)在Spring和Mytis整合之后,无需再通过SqlSessionFactory和SqlSession方式获取Mapper对象,所有Mapper和Dao对象都交给了Spring框架管理,获取Bean的时候,通过ApplicationContext对象获取即可。
3)SSM框架整合是把三大框架整合在一起使用,Web.xml导入Spring配置和SpringMvc配置,在Web容器启动的时候,启动Spring框架容器,从Model,Dao,Service,Controller都采用注解方式,就可以实现直接在Controller里边使用Service的注入对象,无需再使用ApplicationContext获取Bean对象。
1、pom.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://maven.apache.org/POM/4.0.0" 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>org.rui</groupId> <artifactId>02QuickAnnotation</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>7</source> <target>7</target> </configuration> </plugin> <!--热部署编译方式--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork> true </fork> </configuration> </plugin> </plugins> </build> <dependencies> <!-- springMVC官方文档 --> <!-- https: //www.w3cschool.cn/spring_mvc_documentation_linesh_translation --> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-instrument --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>2.6.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--jdbcTemplate--> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.0</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.0</version> </dependency> <!--spring测试--> <!-- https: //mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency> <!--阿里的数据源--> <!-- https: //mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.0</version> </dependency> <!--MySQL连接的依赖包--> <!-- https: //mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- https: //mvnrepository.com/artifact/org.mybatis/mybatis --> <!-- https: //mybatis.org/mybatis-3/getting-started.html --> <!-- https: //mybatis.org/mybatis-3/zh/index.html --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mybatis Spring对接包 单独引入--> <!-- https: //mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--单元测试--> <!-- https: //mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!--日志输出--> <!-- https: //mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--JSON数据转换--> <!-- https: //mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.2</version> </dependency> <!-- https: //mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency> <!-- https: //mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations-java5</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> </project> |
2、web.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation= "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version= "4.0" > <!--对接Spring的主配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!--配置上下文监听器,在Web启动时同时启动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 > <!--用来指定要记载的springMvc核心配置文件位置--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!--容器在启动时立即加载SpringMvc框架及配置文件--> <load- on -startup>1</load- on -startup> </servlet> <!--控制器映射, /代表所有的请求交给SpringMVC处理--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置SpringMVC的编码格式 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class > <!--设置统一采用utf-8编码--> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!--设置request编码为encoding的属性值--> <init-param> <param-name>forceRequestEncoding</param-name> <param-value> true </param-value> </init-param> <!--设置response编码为encoding的属性值--> <init-param> <param-name>forceResponseEncoding</param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--默认首页--> <welcome-file-list> <welcome-file>index</welcome-file> </welcome-file-list> </web-app> |
3、spring.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?xml version= "1.0" encoding= "UTF-8" ?> <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= "http://www.springframework.org/schema/beans" 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"> <!--加载属性配置文件--> <context:property-placeholder location= "classpath:jdbc.Properties" /> <!--配置数据源--> <bean id= "druidDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method= "init" destroy-method= "close" > <property name= "driverClassName" value= "${jdbc.driver}" /> <property name= "url" value= "${jdbc.url}" /> <property name= "username" value= "${jdbc.username}" /> <property name= "password" value= "${jdbc.password}" /> <!--最小连接数--> <property name= "initialSize" value= "${jdbc.pool.initialSize}" /> <!--最小空闲连接数--> <property name= "minIdle" value= "${jdbc.pool.minIdle}" /> <!--最大并发使用数--> <property name= "maxActive" value= "${jdbc.pool.maxActive}" /> <!--最大等待时间--> <property name= "maxWait" value= "${jdbc.pool.maxWait}" /> </bean> <!--配置事务管理器,使用jdbc数据源的事务管理器--> <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref = "druidDataSource" /> </bean> <!--开启基于注解的事务管理方式--> <tx:annotation-driven transaction-manager= "transactionManager" /> <!--myBatis和Spring整合,定义sessionFactory,配置所用数据源和主配置文件位置--> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref = "druidDataSource" /> <property name= "configLocation" value= "classpath:mybatis.xml" /> </bean> <!--自动扫描方式装配myBatis的映射器,无需提供实现类--> <!--配置之后,myBatis配置文件内可以省略mappers映射配置--> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basePackage" value= "rui.db.Dao" /> <property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" /> </bean> <!--配置开启依赖注入的注解解析器,扫包范围--> <context:component-scan base -package= "rui" /> </beans> |
4、springmvc.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?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-4.1.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-4.1.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--设置处理器扫包范围--> <context:component-scan base -package= "rui.web" /> <!--让SpringMvc不处理静态资源--> <mvc: default -servlet-handler /> <!--开启自动注册处理器映射器和处理器适配器--> <mvc:annotation-driven /> <!--配置视图解析器,视图解析器用来解析处理器返回的ModelAndView对象--> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
5、mybatis.xml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > <configuration> <!-- 引入外部属性配置文件 --> <properties resource= "jdbc.Properties" > <!-- 内部额外定义的属性,这些属性都可以在配置文件中通过${属性名}使用 --> <property name= "jdbc.other" value= "other" /> </properties> <!-- 全局配置参数,相关的参数参照myBatis文档 --> <settings> <!-- 是否开启缓存 --> <setting name= "cacheEnabled" value= "true" /> <!-- 是否开启延迟加载 --> <setting name= "lazyLoadingEnabled" value= "true" /> <!--将积极加载修改成消极加载--> <setting name= "aggressiveLazyLoading" value= "false" /> <!--采用Log4j输出调试信息--> <setting name= "logImpl" value= "LOG4J" /> </settings> <!-- 类型别名,声明的短名称可以在Mapper文件中替代使用 --> <typeAliases> <!-- 自动为包内所有类创建类型别名,默认是首字母小写的类名称 也可以在类上方通过@Alias( "别名" )类设置所需要的别名 --> <package name= "db.Model" /> </typeAliases> <!-- Spring配置MapperScannerConfigurer之后,可以省略如下配置 需要在接口上增加@Mapper注解, 没验证成功,不加会报错。 --> <!-- 映射器配置 --> <!-- 引入表的接口类 --> <mappers> <!--一个个的单独导入,开发阶段,未开发好的,不要放入--> <mapper resource= "rui/db/Dao/ex_Order.xml" /> <mapper resource= "rui/db/Dao/ex_Customer.xml" /> <!--自动导入包内所有的Mapper文件--> <!--<package name= "rui.db.mapper" />--> </mappers> </configuration> |
欢迎阅读,有错误请留言指正。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义