SpringMVC入门之SSM整合配置文件处理
-
引入依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dqw</groupId> <artifactId>SpringMVC_SSM</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <properties> <!-- <maven.compiler.source>17</maven.compiler.source> --> <!-- <maven.compiler.target>17</maven.compiler.target> --> <!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> --> <!--单元测试--> <junit.version>4.12</junit.version> <!--Spring的依赖--> <spring.version>5.3.24</spring.version> <!--mybatis的依赖--> <mybatis.version>3.5.11</mybatis.version> <!--mybatis与spring整合的依赖--> <mybatis.spring.version>1.3.1</mybatis.spring.version> <!--mysql驱动的依赖--> <mysql.version>8.0.32</mysql.version> <!--阿里的数据库连接池--> <druid.version>1.1.12</druid.version> <!--servlet的依赖--> <servlet-api.version>4.0.1</servlet-api.version> <!--jackson的依赖(json工具)--> <jackson.version>2.9.6</jackson.version> <!--分页插件的依赖--> <pagehelper.version>5.1.10</pagehelper.version> <!-- thymeleaf的依赖--> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <logback.version>1.2.11</logback.version> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>${thymeleaf.version}</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>${thymeleaf.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!--分页插件的依赖--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Servlet相关 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies> <!-- 插件配置 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <!--识别所有的配置文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
-
配置SqlMapConfig.xml文件
<?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="jdbc8.properties"></properties> --> <settings> <setting name="logImpl" value="SLF4J"/> </settings> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> <!-- <typeAliases> --> <!-- <package name="com.dqw.pojo"/> --> <!-- </typeAliases> --> <!-- <environments default="development"> --> <!-- <environment id="development"> --> <!-- <transactionManager type="JDBC"></transactionManager> --> <!-- <dataSource type="POOLED"> --> <!-- <property name="driver" value="${jdbc.driver}"/> --> <!-- <property name="url" value="${jdbc.url}"/> --> <!-- <property name="username" value="${jdbc.username}"/> --> <!-- <property name="password" value="${jdbc.password}"/> --> <!-- </dataSource> --> <!-- </environment> --> <!-- </environments> --> <!-- 注册Mapper --> <!-- <mappers> --> <!-- <!– <mapper class="com.dqw.Mapper.UsersMapper"></mapper> –> --> <!-- <package name="com.dqw.mapper"/> --> <!-- </mappers> --> </configuration>
-
创建编写applicationContext_mapper.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: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 https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 导入配置文件 --> <context:property-placeholder location="classpath:jdbc8.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="typeAliasesPackage" value="com.dqw.pojo"/> <property name="configLocation" value="classpath:SqlMapConfig.xml"/> </bean> <!-- 配置mapper映射 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dqw.mapper"/> </bean> </beans>
-
创建编写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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/context https://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/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.dqw.service.Impl"/> <!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="myadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*select*" read-only="true"/> <tx:method name="*find*" read-only="true"/> <tx:method name="*query*" read-only="true"/> <tx:method name="*search*" read-only="true"/> <tx:method name="*get*" read-only="true"/> <tx:method name="*insert*" propagation="REQUIRED"/> <tx:method name="*add*" propagation="REQUIRED"/> <tx:method name="*save*" propagation="REQUIRED"/> <tx:method name="*create*" propagation="REQUIRED"/> <tx:method name="*update*" propagation="REQUIRED"/> <tx:method name="*change*" propagation="REQUIRED"/> <tx:method name="*modify*" propagation="REQUIRED"/> <tx:method name="*set*" propagation="REQUIRED"/> <tx:method name="*delete*" propagation="REQUIRED"/> <tx:method name="*drop*" propagation="REQUIRED"/> <tx:method name="*remove*" propagation="REQUIRED"/> <tx:method name="*clear*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="myCut" expression="execution(* com.bjpowernode.service.impl.*.*(..))"/> <aop:advisor advice-ref="myadvice" pointcut-ref="myCut"></aop:advisor> </aop:config> </beans>
-
创建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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.bjpowernode.controller"/> <!-- 模板解析器--> <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/"/> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML"/> <property name="cacheable" value="false"/> <property name="characterEncoding" value="UTF-8"/> </bean> <!-- 模板引擎--> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> </bean> <!-- Thymeleaf的视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/> <property name="characterEncoding" value="UTF-8"/> </bean> <!-- 设置注解驱动--> <mvc:annotation-driven/> <!-- 设置放行静态资源--> <mvc:default-servlet-handler/> </beans>
-
配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <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>Archetype Created Web Application</display-name> <filter> <filter-name>encode</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 注册Spring框架,配置监听器,在项目启动时启动Spring容器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置Spring的核心配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext_*.xml</param-value> </context-param> <!-- 配置这个无法处理中文乱码,推荐采用请求转发的方式访问第一个页面 --> <welcome-file-list> <welcome-file>/WEB-INF/templates/login.html</welcome-file> </welcome-file-list> </web-app>
-
配置jdbc8.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true jdbc.username=root jdbc.password=123456
-
logback.xml
<configuration debug="false"> <!-- 一、日志文件存放的地址,可以更改 --> <property name="LOG_HOME" value="D:/kk"/> <!-- 这里更改 --> <!-- 二、日志输出到控制台的代码 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- 刷新时间 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</pattern> </encoder> </appender> <!-- 三、日志输出到本地文件的代码 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 当文件到达多大就自动新建一个文件继续存放数据 --> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME}/log.%d{yyyy-MM-dd}.%i.log </fileNamePattern> <maxFileSize>1000MB</maxFileSize> <MaxHistory>60</MaxHistory> </rollingPolicy> <!-- 刷新时间 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level %logger{50} -%msg%n</pattern> </encoder> </appender> <logger name="com.apche.ibatis" level="TRACE" /> <logger name="com.mysql.Connection" level="TRACE" /> <logger name="com.mysql.Statement" level="TRACE" /> <logger name="com.sql.PreparedStatement" level="TRACE" /> <!-- 四、总控制开关的代码 --> <root level="DEBUG"> <!-- 输出级别(多少),默认就是DEBUG。TRACE<DEBUG<INFO<WARN<ERROR --> <appender-ref ref="STDOUT"/> <!-- 输出到控制台,不想输出到控制台可以删掉 --> <appender-ref ref="FILE"/> <!-- 输出本地磁盘文件夹中,不想输出到文件夹中可以删掉 --> </root> </configuration>
-
录结构
templates 目录放到WEB-INF目录下,不然第一个页面会报404.
完成便可实现接口完成业务了