spring-springmvc-mybatis三大框架整合
程序员文章站
2024-03-24 17:19:10
...
项目结构图
pom.xml
<?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.lanou3g.ssm</groupId>
<artifactId>sms</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>sms</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.1.8.RELEASE</spring.version>
<mybatis.version>3.5.1</mybatis.version>
<mybatis-spring.version>2.0.1</mybatis-spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<!-- Spring相关依赖(如果引入了spring-webmvc,可以不用显示引入该依赖) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- SpringMVC 相关开始 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- SpringMVC 相关结束 -->
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- 用于Spring、Mybatis整合 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- logback日志依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- JSTL标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 返回JSON格式数据转换器 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- 指定自定义配置文件(代码生成工具)路径 -->
<!--
<configurationFile></configurationFile>
-->
<includeCompileDependencies>true</includeCompileDependencies>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Spring相关配置文件
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/yanfa5?characterEncoding=utf8
jdbc.user=root
jdbc.password=root
jdbc.minIdle=1
jdbc.maxActive=10
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 配置注解扫描service层 -->
<context:component-scan base-package="com.ssm.service"/>
<!-- 开启mybatis自动扫描Mapper接口,将Mapper接口交给mybatis管理 -->
<mybatis:scan base-package="com.ssm.mapper"/>
<!-- 配置数据源 -->
<context:property-placeholder location="classpath:/jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.user}"
p:password="${jdbc.password}"
p:minIdle="${jdbc.minIdle}"
p:maxActive="${jdbc.maxActive}"
/>
<!-- 配置mybatis\spring整合bean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mapperLocations="classpath:/mappers/*"
p:configLocation="classpath:/mybatis_conf.xml"
/>
</beans>
mybatis相关配置文件
mybatis_conf.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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 获取数据库自增ID -->
<setting name="useGeneratedKeys" value="true" />
</settings>
<!-- 定义类型别名(全局),在所有的Mapper.xml中都可以用 -->
<typeAliases>
<typeAlias type="com.ssm.bean.Model" alias="Model" />
</typeAliases>
</configuration>
springmvc相关配置
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描web包下的所有controller,注:service和mapper在root ApplicationContext里处理,这里不用重复扫描 -->
<context:component-scan base-package="com.ssm.web"/>
<!-- 放行静态资源 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<!-- 开启SpringMVC注解相关的支持 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/json</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="defaultCharset" value="UTF-8"/>
<property name="supportedMediaTypes">
<list>
<!-- 添加text/html这种mime类型,主要是解决老版IE浏览器json数据弹出下载框问题 -->
<value>text/html</value>
<value>application/json</value>
</list>
</property>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd:HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置视图解析器 -->
<mvc:view-resolvers>
<!-- ContentNegotiatingViewResolver是一个特殊的视图解析器,本身不负责解析视图,
负责从管理的多个视图解析器中挑选一个最匹配的,返回给DispatcherServlet使用-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
</mvc:view-resolvers>
<!-- 视图控制器(不进handler,直接返回jsp) -->
<mvc:view-controller path="/" view-name="/model"/>
<!-- 处理文件上传servlet3.0 -->
<bean name="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截谁 -->
<mvc:mapping path="/*"/>
<!-- 不拦截谁 -->
<mvc:exclude-mapping path="/"/>
<!-- 拦截器 -->
<bean class="com.ssm.interceptor.ModelInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
web.xml
<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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- 指定root applicationContext配置文件路径,ContextLoaderListener负责初始化root applicationContext
注:工程中可以配置多个DispatcherServlet, root ctx中配置的bean在所有的DispatcherServlet*享
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决POST请求乱码 -->
<filter>
<filter-name>CharsetEncodingFilter</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>CharsetEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC大总管(DispatcherServlet) -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果工程中只有一个DispatcherServlet,也可以将多个配置文件都交给DispatcherServlet来初始化 -->
<!-- DispatcherServlet初始化时默认会去找WEB-INF/{servlet-name}-servlet.xml这个配置文件,
如果路径和文件名和默认行为一致,可以省略下面的配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!-- 开启Servlet3.0文件上传支持 -->
<multipart-config />
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 拦截"/" 代表除了*.jsp不拦截,其他请求都拦截(包括静态资源如:js、css) -->
<!-- 静态资源,我们通过SpringMVC中的<mvc:resources>放行 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
日志配置文件
推荐阅读
-
spring-springmvc-mybatis三大框架整合
-
SpringMVC+mybatis+DWR3注解 博客分类: 框架整合Spring springMVC注解SpringMVC+mybatis+DWR3SpringMVC+DWR3注解配置框架整合
-
SpringMVC_3.x_入门指南_4 博客分类: SpringMVC SpringMVC连接Hibernate框架整合
-
Spring+SpringMVC+Hibernate整合(封装CRUD操作) 博客分类: 框架整合 SpringSpringMVCHibernate框架整合CRUD
-
spring5 框架知识整合(IOC)
-
springboot flowable 整合框架项目源码 vue.js 前后分离 跨域
-
整合了一个小的爬取流程框架 博客分类: java综合
-
整合了一个小的爬取流程框架 博客分类: java综合
-
SSM框架整合遇到的问题汇总(Spring+SpringMVC+Mybatis+PostgreSQL+Tomcat7+Maven)v.18
-
Spring+Ibatis框架整合学习 博客分类: J2EE框架学习 springibatis框架daojava