基本概念
SSM:Spring+SpringMVC+MyBatis
Spring
Spring 是一个开源框架,Spring 是于2003年兴起的一个轻量级的Java 开发框架,由 Rod Johnson 在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情。然而,Spring 的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。 简单来说,Spring 是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。
SpringMVC
Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring Web Flow 里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
MyBatis
MyBatis 本是 apache 的一个开源项目 iBatis, 2010年这个项目由 apache software foundation 迁移到了 google code,并且改名为 MyBatis 。MyBatis 是一个基于 Java 的持久层框架。iBATIS 提供的持久层框架包括 SQL Maps 和 Data Access Objects(DAO)MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 对象)映射成数据库中的记录。
SSM 框架整合是当下最流行的企业级项目技术选型,三个框架分别负责不同的功能,整合起来共同来支持企业级项目的开发需求,与 SSH 的思想是一样,只不过替换了更优秀的框架,用 SpringMVC 替代 Struts2,用 MyBatis 替代 Hibernate。SpringMVC 负责 MVC 设计模式的实现,MyBatis 负责数据持久层,Spring 的 IOC 来管理 SpringMVC 和 MyBatis 相关对象的创建注入,Spring 的 AOP 负责事务管理。
此次 SSM
整合,以 xml
配置为主
配置 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 <?xml version="1.0" encoding="UTF-8"?> <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.example</groupId > <artifactId > ssm-xml</artifactId > <version > 1.0.0-SNAPSHOT</version > <packaging > war</packaging > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <java.version > 1.8</java.version > <spring.version > 5.1.5.RELEASE</spring.version > <commons-logging.version > 1.2</commons-logging.version > <mysql.version > 8.0.16</mysql.version > <mybatis.version > 3.4.6</mybatis.version > <druid.version > 1.1.10</druid.version > <mybatis-spring.version > 1.3.2</mybatis-spring.version > <javax.servlet-api.version > 4.0.1</javax.servlet-api.version > <lombok.version > 1.18.8</lombok.version > <log4j.version > 1.2.17</log4j.version > <slf4j.version > 1.7.25</slf4j.version > <junit.version > 4.12</junit.version > </properties > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > commons-logging</groupId > <artifactId > commons-logging</artifactId > <version > ${commons-logging.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > ${mysql.version}</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > ${mybatis-spring.version}</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > ${mybatis.version}</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > ${druid.version}</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > <version > ${javax.servlet-api.version}</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lombok.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > ${junit.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-log4j12</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jcl-over-slf4j</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jul-to-slf4j</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > ${log4j.version}</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.7.0</version > <configuration > <source > ${java.version}</source > <target > ${java.version}</target > <encoding > ${project.build.sourceEncoding}</encoding > <showWarnings > true</showWarnings > </configuration > </plugin > </plugins > <resources > <resource > <directory > src/main/java</directory > <excludes > <exclude > **/*.java</exclude > </excludes > </resource > <resource > <directory > src/main/resources</directory > </resource > </resources > </build > </project >
配置 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 <?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"> <servlet > <servlet-name > springServlet</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath*:spring-mvc.xml</param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > springServlet</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring-context*.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <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 > <init-param > <param-name > forceEncoding</param-name > <param-value > true</param-value > </init-param > </filter > <filter-mapping > <filter-name > encodingFilter</filter-name > <url-pattern > /*</url-pattern > </filter-mapping > <servlet > <servlet-name > DruidStatView</servlet-name > <servlet-class > com.alibaba.druid.support.http.StatViewServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > DruidStatView</servlet-name > <url-pattern > /druid/*</url-pattern > </servlet-mapping > <welcome-file-list > <welcome-file > index.jsp</welcome-file > </welcome-file-list > </web-app >
配置 spring-mvc.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 <?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"> <description > Spring MVC Configuration</description > <context:property-placeholder ignore-unresolvable ="true" location ="classpath:mvc.properties" /> <context:component-scan base-package ="com.example.ssm.xml" use-default-filters ="false" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <mvc:annotation-driven /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="${web.view.prefix}" /> <property name ="suffix" value ="${web.view.suffix}" /> </bean > <mvc:resources mapping ="/static/**" location ="/static/" cache-period ="31536000" /> </beans >
配置 mvc.properties
1 2 3 4 5 6 7 web.view.prefix =/WEB-INF/views/ web.view.suffix =.jsp
配置 spring-context.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 <?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" 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"> <description > Spring Configuration</description > <context:annotation-config /> <context:component-scan base-package ="com.example.ssm.xml" > <context:exclude-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" /> </bean > <tx:annotation-driven transaction-manager ="transactionManager" /> <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" /> </bean > </beans >
配置 spring-context-druid.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 <?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" 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"> <context:property-placeholder ignore-unresolvable ="true" location ="classpath:jdbc.properties" /> <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" init-method ="init" destroy-method ="close" > <property name ="driverClassName" value ="${jdbc.driverClass}" /> <property name ="url" value ="${jdbc.connectionURL}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> <property name ="initialSize" value ="${jdbc.pool.init}" /> <property name ="minIdle" value ="${jdbc.pool.minIdle}" /> <property name ="maxActive" value ="${jdbc.pool.maxActive}" /> <property name ="maxWait" value ="60000" /> <property name ="timeBetweenEvictionRunsMillis" value ="60000" /> <property name ="minEvictableIdleTimeMillis" value ="300000" /> <property name ="validationQuery" value ="${jdbc.testSql}" /> <property name ="testWhileIdle" value ="true" /> <property name ="testOnBorrow" value ="false" /> <property name ="testOnReturn" value ="false" /> <property name ="filters" value ="stat" /> </bean > </beans >
配置 jdbc.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 jdbc.driverClass =com.mysql.cj.jdbc.Driver jdbc.connectionURL =jdbc:mysql://localhost:3306/myshop?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username =root jdbc.password =123456 jdbc.pool.init =1 jdbc.pool.minIdle =3 jdbc.pool.maxActive =20 jdbc.testSql =SELECT 'x' FROM DUAL
配置 spring-context-mybatis.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> <property name ="typeAliasesPackage" value ="com.example.ssm.xml.entity" /> <property name ="mapperLocations" value ="classpath:/mapperxml/**/*.xml" /> <property name ="configLocation" value ="classpath:/mybatis-config.xml" > </property > </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="basePackage" value ="com.example.ssm.xml.dao" /> </bean > </beans >
配置 mybatis-config.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 <?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 > <settings > <setting name ="cacheEnabled" value ="false" /> <setting name ="lazyLoadingEnabled" value ="true" /> <setting name ="aggressiveLazyLoading" value ="true" /> <setting name ="multipleResultSetsEnabled" value ="true" /> <setting name ="useColumnLabel" value ="true" /> <setting name ="useGeneratedKeys" value ="false" /> <setting name ="autoMappingBehavior" value ="PARTIAL" /> <setting name ="defaultExecutorType" value ="SIMPLE" /> <setting name ="mapUnderscoreToCamelCase" value ="true" /> <setting name ="localCacheScope" value ="SESSION" /> <setting name ="jdbcTypeForNull" value ="NULL" /> </settings > </configuration >
配置 log4j.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 log4j.rootLogger =INFO, console, file log4j.appender.console =org.apache.log4j.ConsoleAppender log4j.appender.console.layout =org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern =%d %p [%c] - %m%n log4j.appender.file =org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File =logs/log.log log4j.appender.file.layout =org.apache.log4j.PatternLayout log4j.appender.A3.MaxFileSize =1024KB log4j.appender.A3.MaxBackupIndex =10 log4j.appender.file.layout.ConversionPattern =%d %p [%c] - %m%n log4j.logger.com.example.ssm.xml.dao = TRACE
测试
编写 TestController
controller
1 2 3 4 5 6 7 8 9 10 11 @Controller public class TestController { @RequestMapping(value = "test", method = RequestMethod.GET) public String springMVCTest (@RequestParam("name") String name, Model model) { System.out.println("获取参数:" + name); model.addAttribute("result" , name); return "test" ; } }
编写 User
entity
1 2 3 4 5 6 7 8 9 10 @Data public class User { private Integer id; private Integer age; private String email; private String gender; private String name; private String password; private Date created; }
编写 UserMapper
dao(mapper)
1 2 3 4 5 public interface UserMapper { List<User> selectAll () ; }
编写 UserMapper.xml
mapperxml
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.example.ssm.xml.dao.UserMapper" > <resultMap id ="BaseResultMap" type ="com.example.ssm.xml.entity.User" > <id column ="id" jdbcType ="INTEGER" property ="id" /> <result column ="age" jdbcType ="INTEGER" property ="age" /> <result column ="email" jdbcType ="VARCHAR" property ="email" /> <result column ="gender" jdbcType ="VARCHAR" property ="gender" /> <result column ="name" jdbcType ="VARCHAR" property ="name" /> <result column ="password" jdbcType ="VARCHAR" property ="password" /> <result column ="created" jdbcType ="TIMESTAMP" property ="created" /> </resultMap > <sql id ="Base_Column_List" > id, age, email, gender, `name`, `password`, created </sql > <select id ="selectAll" resultMap ="BaseResultMap" > select <include refid ="Base_Column_List" /> from user </select > </mapper >
先编写 springContextTest
测试类:
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 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:spring-context.xml", "classpath:spring-context-druid.xml" , "classpath:spring-context-mybatis.xml" }) public class springContextTest { @Autowired private UserMapper userMapper; @Test public void mapperTest () { List<User> users = userMapper.selectAll(); for (User user : users) { System.out.println(user); } } @Autowired private JdbcTemplate jdbcTemplate; @Test public void jdbcTemplateTest () { String sql = "select * from user" ; List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); for (User user : users) { System.out.println(user); } } }
再编写 springMVCTest
测试类:
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 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-mvc.xml") @WebAppConfiguration public class springMVCTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void initMockMvc () { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void testController () throws Exception { mockMvc.perform(get("/test?name=Vincent" )) .andExpect(status().isOk()) .andExpect(view().name("test" )) .andExpect(model().attribute("result" , "Vincent" )); } }
案例源码:https://gitee.com/V_Vincen/ssm-xml
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !