经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Spring整合SpringMVC + Mybatis基础框架的配置文件详解
来源:jb51  时间:2021/2/1 13:48:28  对本文有异议

前言

新建一个普通的Maven项目

基本目录结构

  1. ├── src #
  2. ├── main #
  3. └── java # java代码目录
  4. └── resources # 配置文件目录, 存放下面Spring配置文件
  5. ├── test # 单元测试目录
  6. ├── web # web目录
  7. └── WEB-INF # web.xml 配置文件目录

1. Mybatis层编写

1、在 resources 目录下新建数据库配置文件 database.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. # 如果是使用 MySQL8.0+ 那么还需要增加一个时区的配置; serverTimezone=Asia/Shanghai
  3. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
  4. jdbc.username=root
  5. jdbc.password=123456

2、在 resources 目录下创建Mybatis配置文件 mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--配置数据源, 交给Spring-->
  7. <!--配置log-->
  8. <settings>
  9. <!--STDOUT_LOGGING: 标准的日志工厂实现-->
  10. <setting name="logImpl" value="STDOUT_LOGGING"/>
  11. </settings>
  12. <!--配置别名-->
  13. <typeAliases>
  14. <package name="com.pro.pojo"/>
  15. </typeAliases>
  16.  
  17. <!--绑定Mapper-->
  18. <mappers>
  19. <mapper class="com.pro.dao.BooksMapper"/>
  20. </mappers>
  21.  
  22. </configuration>

2. Spring层编写

1. Spring整合Mybatis

配置Spring整合MyBatis,这里数据源使用c3p0连接池;

编写Spring整合Mybatis的相关的配置文件;在 resources 目录下创建 spring-dao.xml

注意:这里要引入上面Mybatis层的两个配置文件,配置文件的名称不要写错

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <!--1. 关联数据库配置文件-->
  11. <context:property-placeholder location="classpath:database.properties"/>
  12.  
  13. <!--2. 连接池
  14. dbcp: 半自动化操作, 不能自动连接
  15. c3p0: 自动化操作 (自动加载配置文件并设置到对象中)
  16. druid, hikari
  17. -->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="driverClass" value="${jdbc.driver}"/>
  20. <property name="jdbcUrl" value="${jdbc.url}"/>
  21. <property name="user" value="${jdbc.username}"/>
  22. <property name="password" value="${jdbc.password}"/>
  23.  
  24. <!--c3p0连接池的私有属性, 最大最小连接池大小-->
  25. <property name="maxPoolSize" value="30"/>
  26. <property name="minPoolSize" value="10"/>
  27. <!--关闭连接后不自动commit-->
  28. <property name="autoCommitOnClose" value="false"/>
  29. <!--连接超时-->
  30. <property name="checkoutTimeout" value="10000"/>
  31. <!--获取连接失败重试次数-->
  32. <property name="acquireRetryAttempts" value="2"/>
  33. </bean>
  34.  
  35. <!--3. sqlSessionFactory-->
  36. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  37. <property name="dataSource" ref="dataSource"/>
  38. <!--绑定Mybatis配置文件-->
  39. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  40. </bean>
  41.  
  42. <!--4. 配置Dao扫描包, 动态实现Dao接口注入到Spring容器中-->
  43. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  44. <!--注入 sqlSessionFactory-->
  45. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  46. <!--配置要扫描的dao包-->
  47. <property name="basePackage" value="com.pro.dao"/>
  48. </bean>
  49. </beans>

2. Spring整合service

将业务层的类注入到Spring中,在 resources 目录下创建 spring-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd">
  15.  
  16. <!--1. 扫描service下的包-->
  17. <context:component-scan base-package="com.pro.service"/>
  18.  
  19. <!--2. 将业务层的类注入到Spring中-->
  20. <bean id="BooksServiceImpl" class="com.pro.service.BooksServiceImpl">
  21. <property name="booksMapper" ref="booksMapper"/>
  22. </bean>
  23.  
  24. <!--3. 配置声明式事务-->
  25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  26. <!--注入数据源-->
  27. <property name="dataSource" ref="dataSource"/>
  28. </bean>
  29.  
  30. <!--4. 配置aop实现事务织入-->
  31. <!--配置事务通知-->
  32. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  33. <!--1. 给那些方法配置事务-->
  34. <!--2. 配置事务的传播特性: propagation-->
  35. <tx:attributes>
  36. <tx:method name="*" propagation="REQUIRED"/>
  37. </tx:attributes>
  38. </tx:advice>
  39.  
  40. <!--配置事务切入-->
  41. <aop:config>
  42. <!--mapper包下的所有类的所有方法-->
  43. <aop:pointcut id="txPointCut" expression="execution(* com.pro.dao.*.*(..))"/>
  44. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
  45. </aop:config>
  46. </beans>

3. SpringMVC层编写

1. 编写web.xml

修改 WEB-INF 下的 web.xml 文件

这里引入Spring整合的配置文件 applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
  5. version="5.0">
  6.  
  7. <!--DispatchServlet-->
  8. <servlet>
  9. <servlet-name>springmvc</servlet-name>
  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11. <!--加载Spring配置文件-->
  12. <init-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </init-param>
  16. <!--启动级别-->
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>springmvc</servlet-name>
  21. <url-pattern>/</url-pattern>
  22. </servlet-mapping>
  23.  
  24. <!--乱码过滤-->
  25. <filter>
  26. <filter-name>encodingFilter</filter-name>
  27. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  28. <init-param>
  29. <param-name>encoding</param-name>
  30. <param-value>utf-8</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>encodingFilter</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37.  
  38. <!--Session过期时间-->
  39. <session-config>
  40. <session-timeout>15</session-timeout>
  41. </session-config>
  42. </web-app>

2. 编写spring-mvc.xml

resources 目录下创建 spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  10. http://www.springframework.org/schema/context
  11. https://www.springframework.org/schema/context/spring-context.xsd">
  12.  
  13.  
  14. <!--1. 注解驱动-->
  15. <mvc:annotation-driven/>
  16. <!--2. 静态资源过滤-->
  17. <mvc:default-servlet-handler/>
  18. <!--3. 扫描包: controller-->
  19. <context:component-scan base-package="com.pro.controller"/>
  20. <!--4. 视图解析器-->
  21. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22. <property name="prefix" value="/WEB-INF/jsp/"/>
  23. <property name="suffix" value=".jsp"/>
  24. </bean>
  25.  
  26. </beans>

4. Spring配置整合文件,applicationContext.xml

resources 目录下创建 applicationContext.xml

这里引入上面三个配置文件 spring-dao.xmlspring-service.xmlspring-mvc.xml 整合成一个总的配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <import resource="classpath:spring-dao.xml"/>
  8. <import resource="classpath:spring-service.xml"/>
  9. <import resource="classpath:spring-mvc.xml"/>
  10.  
  11. </beans>

依赖

  1. <!--依赖-->
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.projectlombok</groupId>
  5. <artifactId>lombok</artifactId>
  6. <version>1.18.10</version>
  7. </dependency>
  8. <!--Junit-->
  9. <dependency>
  10. <groupId>junit</groupId>
  11. <artifactId>junit</artifactId>
  12. <version>4.13</version>
  13. </dependency>
  14. <!--数据库驱动-->
  15. <dependency>
  16. <groupId>mysql</groupId>
  17. <artifactId>mysql-connector-java</artifactId>
  18. <version>5.1.47</version>
  19. </dependency>
  20. <!--数据库连接池-->
  21. <dependency>
  22. <groupId>com.mchange</groupId>
  23. <artifactId>c3p0</artifactId>
  24. <version>0.9.5.2</version>
  25. </dependency>
  26.  
  27. <!--Servlet - JSP -->
  28. <dependency>
  29. <groupId>javax.servlet</groupId>
  30. <artifactId>servlet-api</artifactId>
  31. <version>2.5</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>javax.servlet.jsp</groupId>
  35. <artifactId>jsp-api</artifactId>
  36. <version>2.2</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>javax.servlet</groupId>
  40. <artifactId>jstl</artifactId>
  41. <version>1.2</version>
  42. </dependency>
  43.  
  44. <!--Mybatis-->
  45. <dependency>
  46. <groupId>org.mybatis</groupId>
  47. <artifactId>mybatis</artifactId>
  48. <version>3.5.2</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.mybatis</groupId>
  52. <artifactId>mybatis-spring</artifactId>
  53. <version>2.0.2</version>
  54. </dependency>
  55.  
  56. <!--Spring-->
  57. <dependency>
  58. <groupId>org.springframework</groupId>
  59. <artifactId>spring-webmvc</artifactId>
  60. <version>5.1.9.RELEASE</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.springframework</groupId>
  64. <artifactId>spring-jdbc</artifactId>
  65. <version>5.1.9.RELEASE</version>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.aspectj</groupId>
  69. <artifactId>aspectjweaver</artifactId>
  70. <version>1.9.4</version>
  71. </dependency>
  72. </dependencies>
  73.  
  74. <!--静态资源导出问题-->
  75. <build>
  76. <resources>
  77. <resource>
  78. <directory>src/main/java</directory>
  79. <includes>
  80. <include>**/*.properties</include>
  81. <include>**/*.xml</include>
  82. </includes>
  83. <filtering>false</filtering>
  84. </resource>
  85. <resource>
  86. <directory>src/main/resources</directory>
  87. <includes>
  88. <include>**/*.properties</include>
  89. <include>**/*.xml</include>
  90. </includes>
  91. <filtering>false</filtering>
  92. </resource>
  93. </resources>
  94. </build>

到此这篇关于Spring整合SpringMVC + Mybatis基础框架的配置文件的文章就介绍到这了,更多相关Spring整合SpringMVC Mybatis内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号