零、SSM框架demo下载
以下配置与demo均为maven项目。
一、依赖添加(pom.xml)
主要配置的依赖包有:
spring框架(spring-context),springMVC框架(spring-webmvc),MyBatis框架(mybatis与mybatis-spring),模板解析框架(thymeleaf与thymeleaf-spring4),JDBC(spring-jdbc与mysql-connector-java)
可选依赖包:
数据库连接池(commons-dbcp),Junit单元测试(junit)
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies>
二、web.xml配置
配置springMVC(DispatcherServlet的配置)
第8行表示:在resource文件夹下寻找spring与springmvc的配置文件spring-mvc.xml(名称可改)
第15行表示:对所有以.do结尾的请求进行拦截对其分配处理方法(SpringMVC原理)。
配置全局编码
固定写法
<!-- 配置springMVC的DispatcherServlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- param-value节点:配置需要加载的Spring的配置文件的位置 --> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern节点:配置SpringMVC框架处理哪些请求 --> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 配置全局编码 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
三、spring-dao.xml与spring-mvc配置
spring-dao.xml与spring-mvc.xml存放在resource文件夹下。
1.spring-dao.xml
spring-dao即mybatis相关的配置信息。需要自行改动的两个地方:
- mapper xml文件位置:在下面配置中,<property name="mapperLocations">中的value表示resource文件夹下的mappers下的所有.xml文件,value为可能改动的属性。
- mapper接口位置:在下面配置中,<property name="basePackage">中的value表是mapper对应的mapper接口所在的包,value为可能改动的属性。
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 读取jdbc.properties --> <util:properties id="jdbc" location="classpath:jdbc.properties"></util:properties> <!-- 配置数据源:BasicDataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="#{jdbc.url}"></property> <property name="driverClassName" value="#{jdbc.driver}"></property> <property name="username" value="#{jdbc.username}"></property> <property name="password" value="#{jdbc.password}"></property> <property name="initialSize" value="#{jdbc.initialSize}"></property> <property name="maxActive" value="#{jdbc.maxActive}"></property> </bean> <!-- 配置MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.tedu.mybatis"></property> </bean> <!-- SqlSessionFactoryBean --> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定配置SQL语句的XML文件在哪里 --> <property name="mapperLocations" value="classpath:mappers/*.xml"></property> <!-- 自定连接数据库的配置文件源 --> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
2.spring-mvc.xml
spring-mvc.xml即spring与springMVC的配置文件,名字可以任意,但是要和web.xml中引用时保持一致。
其中,需要自定义的地方有:
- 组件扫描根包:base-package
- 模板解析器的perfix与suffix,分别表示前缀与后缀,前缀表示页面模板所在的地址(这里为resource文件夹下的templates文件夹),后缀表示模板类型(这里为 .html)
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 组件扫描 --> <!-- 可修改处1:base-package属性:被扫描的根包 --> <context:component-scan base-package="cn.tedu.spring"></context:component-scan> <!-- 配置模版解析器 --> <!-- ClassLoaderTemplateResolver:当把模版页面放在src/main/resources下时,必须使用这个模版解析器 --> <!-- ServletContextTemplateResolver:当把模版页面放在webapp下时,必须使用这个模版解析器 --> <!-- 注入prefix属性:配置前缀 --> <!-- 注入suffix属性:配置后缀 --> <!-- 前缀 + 处理请求的方法的返回值 + 后缀 必须对应html文件的位置 --> <bean id="templateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver"> <property name="prefix" value="/templates/"></property> <property name="suffix" value=".html"></property> <property name="characterEncoding" value="utf-8"></property> <property name="cacheable" value="false"></property> <property name="templateMode" value="HTML"></property> </bean> <!-- 配置模版引擎 --> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"></property> </bean> <!-- 配置Thymeleaf视图解析器 --> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"></property> <property name="characterEncoding" value="utf-8"></property> </bean> </beans>
四、properties文件配置
1.jdbc.properties文件
文件存放在resource文件下,文件名称不可改,文件中数据可改。
其中,此字符串不改:useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
url=jdbc:mysql://localhost:3306/tedu_ums?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai driver=com.mysql.cj.jdbc.Driver username=root password=xiaohei1120 initialSize=10 maxActive=50