如何最优雅地融合Spring框架?

2026-06-11 08:39:12 413阅读 0评论 SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计3110个文字,预计阅读时间需要13分钟。

这里使用 Maven 项目管理工具构建项目,初始化项目,并在 Intellij IDEA 中创建 Java Web 项目。

如何最优雅地融合Spring框架?

1. 打开 Intellij IDEA,点击 Create New Project。

2.选择 Maven 构建工具,配置 JDK 版本。

3.选择 maven-archetype-webapp 模板(Java Web 项目)。

如何最优雅地融合Spring框架?

4.填写项目信息至 Maven 仓库。

这里使用 Maven 项目管理工具构建项目

初始化项目

  • 打开 Intellij IDEA,点击 Create New Project
  • 选择 Maven 构建项目
  • 选择 JDK 版本

选择 maven-archetype-webapp 模板(Java Web 项目)

填写项目在 Maven 仓库中的坐标(在 Maven 仓库中根据这个坐标才能找到该项目)

  • 选择 Maven 路径
  • 选择 Maven 配置文件路径
  • 选择 Maven 本地仓库路径
  • 填写项目名
  • 选择工作目录

创建目录

src > main 目录下分别新建 java 源码目录 和 resource 配置文件目录

java 目录下创建基本的源码目录结构

webapp 目录下创建 static 目录,用于存放静态资源文件(css, js, img 等)

webapp > WEB-INF 目录下创建 views 目录,用于存放视图页面(jsp, html 等)

pom.xml

完整的 pom.xml 配置如下

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.antoniopeng</groupId> <artifactId>ssm-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>hello-ssm Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>antoniopeng.com</url> <properties> <!-- 环境配置 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 统一的依赖管理 --> <alibaba-druid.version>1.1.6</alibaba-druid.version> <apache-localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=123456 # JDBC Pool jdbc.pool.init=1 jdbc.pool.minIdle=3 jdbc.pool.maxActive=20 # Web View Location web.view.prefix=/WEB-INF/views/ web.view.suffix=.jsp # Upload Size web.maxUploadSize=10485760

Spring 核心配置

resources 目录下创建 spring-context.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启 Spring 相关注解 --> <context:annotation-config /> <!-- 使用 Annotation 自动注册 Bean, 并且只扫描 @Controller --> <context:component-scan base-package="com.antoniopeng.hello.ssm"> <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"/> </beans>

整合 Spring MVC

创建 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ssm.properties"/> <!-- 使用 Annotation 自动注册 Bean,只扫描 @Controller --> <context:component-scan base-package="com.antoniopeng.hello.ssm.controller" 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"/> <!-- 拦截器配置 --> <mvc:interceptors> <!-- 拦截登录 --> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/login" /> <mvc:exclude-mapping path="/static/**" /> <bean class="com.antoniopeng.hello.ssm.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!-- 上传文件拦截,设置最大上传文件大小 10M = 10*1024*1024(B) = 10485760 bytes --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="${web.maxUploadSize}"/> </bean> </beans>

MyBatis 配置

创建 mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 打印 SQL 语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="false"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否允许单条 SQL 返回多个数据集 (取决于驱动的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 允许 JDBC 生成主键。需要驱动器支持。如果设为了 true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不映射 PARTIAL:部分 FULL:全部 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session --> <setting name="localCacheScope" value="SESSION"/> <!-- 设置 JDBC 类型为空时,某些驱动程序 要指定值, default:OTHER,插入空值时不需要指定类型 --> <setting name="jdbcTypeForNull" value="NULL"/> </settings> </configuration>

整合 Druid

resources 目录下创建 spring-context-druid.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ssm.properties"/> <!-- 数据源配置, 使用 Druid 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --> <property name="driverClassName" value="${jdbc.driverClass}"/> <!-- 基本属性 url、user、password --> <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"/> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> </bean> </beans>

整合 MyBatis

resources 目录下创建 spring-context-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置 SqlSession --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 用于配置对应实体类所在的包,多个 package 之间可以用 ',' 号分割 --> <property name="typeAliasesPackage" value="com.antoniopeng.hello.ssm.entity"/> <!-- 用于配置对象关系映射配置文件所在目录 --> <property name="mapperLocations" value="classpath*:/mapper/**/*.xml"/> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> <!-- 扫描 Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.antoniopeng.hello.ssm.dao" /> </bean> </beans>

日志配置

创建 log4j.properties 日志配置文件

log4j.rootLogger=error, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.logger.com.ibatis = debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner = debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = debug log4j.logger.java.sql.Connection = debug log4j.logger.java.sql.Statement = debug log4j.logger.java.sql.PreparedStatement = debug log4j.logger.java.sql.ResultSet =debug log4j.logger.com.pro.mapper =debug

web.xml

完整的 web.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <welcome-file-list> <welcome-file>/login</welcome-file> </welcome-file-list> <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>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> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

创建访问视图

webapp > WEB-INF > views 目录下新建 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> </head> <body> <h1> Hello SSM </h1> </body> </html>

controller 目录下创建 IndexController

package com.antoniopeng.hello.ssm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping(value = "/") public String index() { return "index"; } }

部署到 Tomcat 服务器

点击 Intellij IDEA 右上方 Add Configuration..

添加 Tomcat 本地服务器配置

配置下载好的 Tomcat 服务器

Tomcat8 官网下载地址

设置访问端口号,默认 8080

将项目添加到 Tomcat 服务器

最后运行项目,访问 localhost:8080/ 即可。

源码地址:github.com/antoniopeng/ssm-example.git

到此这篇关于最优雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企业级应用(附源码)的文章就介绍到这了,更多相关 Spring Spring MVC MyBatis 搭建 Java 企业级应用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签: 框架 优雅 spring

本文共计3110个文字,预计阅读时间需要13分钟。

这里使用 Maven 项目管理工具构建项目,初始化项目,并在 Intellij IDEA 中创建 Java Web 项目。

如何最优雅地融合Spring框架?

1. 打开 Intellij IDEA,点击 Create New Project。

2.选择 Maven 构建工具,配置 JDK 版本。

3.选择 maven-archetype-webapp 模板(Java Web 项目)。

如何最优雅地融合Spring框架?

4.填写项目信息至 Maven 仓库。

这里使用 Maven 项目管理工具构建项目

初始化项目

  • 打开 Intellij IDEA,点击 Create New Project
  • 选择 Maven 构建项目
  • 选择 JDK 版本

选择 maven-archetype-webapp 模板(Java Web 项目)

填写项目在 Maven 仓库中的坐标(在 Maven 仓库中根据这个坐标才能找到该项目)

  • 选择 Maven 路径
  • 选择 Maven 配置文件路径
  • 选择 Maven 本地仓库路径
  • 填写项目名
  • 选择工作目录

创建目录

src > main 目录下分别新建 java 源码目录 和 resource 配置文件目录

java 目录下创建基本的源码目录结构

webapp 目录下创建 static 目录,用于存放静态资源文件(css, js, img 等)

webapp > WEB-INF 目录下创建 views 目录,用于存放视图页面(jsp, html 等)

pom.xml

完整的 pom.xml 配置如下

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.antoniopeng</groupId> <artifactId>ssm-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>hello-ssm Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>antoniopeng.com</url> <properties> <!-- 环境配置 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 统一的依赖管理 --> <alibaba-druid.version>1.1.6</alibaba-druid.version> <apache-localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=123456 # JDBC Pool jdbc.pool.init=1 jdbc.pool.minIdle=3 jdbc.pool.maxActive=20 # Web View Location web.view.prefix=/WEB-INF/views/ web.view.suffix=.jsp # Upload Size web.maxUploadSize=10485760

Spring 核心配置

resources 目录下创建 spring-context.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启 Spring 相关注解 --> <context:annotation-config /> <!-- 使用 Annotation 自动注册 Bean, 并且只扫描 @Controller --> <context:component-scan base-package="com.antoniopeng.hello.ssm"> <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"/> </beans>

整合 Spring MVC

创建 spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ssm.properties"/> <!-- 使用 Annotation 自动注册 Bean,只扫描 @Controller --> <context:component-scan base-package="com.antoniopeng.hello.ssm.controller" 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"/> <!-- 拦截器配置 --> <mvc:interceptors> <!-- 拦截登录 --> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/login" /> <mvc:exclude-mapping path="/static/**" /> <bean class="com.antoniopeng.hello.ssm.interceptor.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!-- 上传文件拦截,设置最大上传文件大小 10M = 10*1024*1024(B) = 10485760 bytes --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="${web.maxUploadSize}"/> </bean> </beans>

MyBatis 配置

创建 mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局参数 --> <settings> <!-- 打印 SQL 语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="false"/> <!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/> <!-- 是否允许单条 SQL 返回多个数据集 (取决于驱动的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/> <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/> <!-- 允许 JDBC 生成主键。需要驱动器支持。如果设为了 true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false --> <setting name="useGeneratedKeys" value="false"/> <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不映射 PARTIAL:部分 FULL:全部 --> <setting name="autoMappingBehavior" value="PARTIAL"/> <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/> <!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session --> <setting name="localCacheScope" value="SESSION"/> <!-- 设置 JDBC 类型为空时,某些驱动程序 要指定值, default:OTHER,插入空值时不需要指定类型 --> <setting name="jdbcTypeForNull" value="NULL"/> </settings> </configuration>

整合 Druid

resources 目录下创建 spring-context-druid.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:context="www.springframework.org/schema/context" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:ssm.properties"/> <!-- 数据源配置, 使用 Druid 数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass --> <property name="driverClassName" value="${jdbc.driverClass}"/> <!-- 基本属性 url、user、password --> <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"/> <!-- 配置监控统计拦截的filters --> <property name="filters" value="stat"/> </bean> </beans>

整合 MyBatis

resources 目录下创建 spring-context-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置 SqlSession --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 用于配置对应实体类所在的包,多个 package 之间可以用 ',' 号分割 --> <property name="typeAliasesPackage" value="com.antoniopeng.hello.ssm.entity"/> <!-- 用于配置对象关系映射配置文件所在目录 --> <property name="mapperLocations" value="classpath*:/mapper/**/*.xml"/> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> <!-- 扫描 Mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.antoniopeng.hello.ssm.dao" /> </bean> </beans>

日志配置

创建 log4j.properties 日志配置文件

log4j.rootLogger=error, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[service] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.logger.com.ibatis = debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner = debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = debug log4j.logger.java.sql.Connection = debug log4j.logger.java.sql.Statement = debug log4j.logger.java.sql.PreparedStatement = debug log4j.logger.java.sql.ResultSet =debug log4j.logger.com.pro.mapper =debug

web.xml

完整的 web.xml 配置如下:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <welcome-file-list> <welcome-file>/login</welcome-file> </welcome-file-list> <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>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> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

创建访问视图

webapp > WEB-INF > views 目录下新建 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index</title> </head> <body> <h1> Hello SSM </h1> </body> </html>

controller 目录下创建 IndexController

package com.antoniopeng.hello.ssm.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping(value = "/") public String index() { return "index"; } }

部署到 Tomcat 服务器

点击 Intellij IDEA 右上方 Add Configuration..

添加 Tomcat 本地服务器配置

配置下载好的 Tomcat 服务器

Tomcat8 官网下载地址

设置访问端口号,默认 8080

将项目添加到 Tomcat 服务器

最后运行项目,访问 localhost:8080/ 即可。

源码地址:github.com/antoniopeng/ssm-example.git

到此这篇关于最优雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企业级应用(附源码)的文章就介绍到这了,更多相关 Spring Spring MVC MyBatis 搭建 Java 企业级应用内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签: 框架 优雅 spring