当前位置:   article > 正文

讲解SSM的xml文件

讲解SSM的xml文件

概述:这些配置文件很烦,建议直接复制粘贴

springMVC.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:mvc="http://www.springframework.org/schema/mvc"
  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/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  12. <context:component-scan base-package="edu.gyc"/>
  13. <mvc:annotation-driven/>
  14. <!--解决静态资源js css访问的问题-->
  15. <mvc:default-servlet-handler/>
  16. <!-- 配置视图解析器 -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  19. <property name="prefix" value="/WEB-INF/jsp/"/>
  20. <property name="suffix" value=".jsp"/>
  21. </bean>
  22. </beans>

applicationContext.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:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  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/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd">
  15. <context:component-scan base-package="edu.gyc">
  16. </context:component-scan>
  17. <!-- 读取java配置文件,替换占位符数据 -->
  18. <context:property-placeholder location="classpath:jdbc.properties"/>
  19. <!-- 配置数据源 -->
  20. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  21. init-method="init" destroy-method="close">
  22. <!-- 数据库基本信息配置 -->
  23. <property name="url" value="${druid.url}" />
  24. <property name="username" value="${druid.username}" />
  25. <property name="password" value="${druid.password}" />
  26. <property name = "driverClassName" value = "com.mysql.cj.jdbc.Driver" />
  27. <!-- 初始化连接数量 -->
  28. <property name="initialSize" value="${druid.initialSize}" />
  29. <!-- 最小空闲连接数 -->
  30. <property name="minIdle" value="${druid.minIdle}" />
  31. <!-- 最大并发连接数 -->
  32. <property name="maxActive" value="${druid.maxActive}" />
  33. <!-- 配置获取连接等待超时的时间 -->
  34. <property name="maxWait" value="${druid.maxWait}" />
  35. </bean>
  36. <!-- 配置SqlSessionFactory -->
  37. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  38. <!-- 注入dataSource -->
  39. <property name="dataSource" ref="dataSource"/>
  40. <property name="mapperLocations" value="classpath:mapper/*.xml"/>
  41. </bean>
  42. <!-- 配置Mapper代理对象方式一:MapperFactoryBean(了解) -->
  43. <!-- 通过MapperFactoryBean生成的代理对象,一次只能针对一个接口进行生成 -->
  44. <!--<bean id="accountMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  45. <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  46. <property name="mapperInterface" value="top.alanshelby.mapper.AccountMapper"></property>
  47. </bean>-->
  48. <!-- 配置Mapper代理对象方式二:MapperScannerConfigurer -->
  49. <!-- 批量代理对象的生成 -->
  50. <!-- 注意事项:mapper接口类和mapper映射文件同包同名 -->
  51. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  52. <!-- 指定需要生成代理的接口所在的包名 -->
  53. <property name="basePackage" value="edu.gyc.dao"/>
  54. <!-- 注意事项:千万不要配置SqlSessionFactory,要配置sqlSessionFactoryBeanName -->
  55. <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
  56. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  57. </bean>
  58. <!-- 配置平台事务管理器 -->
  59. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  60. <property name="dataSource" ref="dataSource"></property>
  61. </bean>
  62. <!-- 事务通知 -->
  63. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  64. <tx:attributes>
  65. <tx:method name="transfer*" propagation="REQUIRED" isolation="DEFAULT"/>
  66. </tx:attributes>
  67. </tx:advice>
  68. <!-- 实现注解的声明式事务 -->
  69. <tx:annotation-driven transaction-manager="transactionManager" mode="proxy"/>
  70. </beans>

在这个文件中就已经配置了sqlSessionFactory、MapperScannerConfigurer 。

一个用来与数据库进行交互的,一个用来扫描包的。

项目结构:

然后配置一下jdbc.properties

  1. druid.driverClassName=com.mysql.cj.jdbc.Driver
  2. druid.url=jdbc:mysql://127.0.0.1:3306/cyl?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
  3. druid.username=root
  4. druid.password=123456
  5. druid.initialSize=10
  6. druid.minIdle=6
  7. druid.maxActive=50
  8. druid.maxWait=60000

pom依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  9. <dependency>
  10. <groupId>org.mybatis.generator</groupId>
  11. <artifactId>mybatis-generator-core</artifactId>
  12. <version>1.3.7</version>
  13. </dependency>
  14. <!-- mybatis依赖 -->
  15. <dependency>
  16. <groupId>org.mybatis</groupId>
  17. <artifactId>mybatis</artifactId>
  18. <version>3.4.6</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.slf4j</groupId>
  22. <artifactId>slf4j-log4j12</artifactId>
  23. <version>1.7.21</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. <version>8.0.27</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>4.12</version>
  34. </dependency>
  35. <!-- 持久层依赖 begin -->
  36. <!-- spring ioc组件需要的依赖包 -->
  37. <dependency>
  38. <groupId>org.springframework</groupId>
  39. <artifactId>spring-beans</artifactId>
  40. <version>4.3.13.RELEASE</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-core</artifactId>
  45. <version>4.3.13.RELEASE</version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-context</artifactId>
  50. <version>4.3.13.RELEASE</version>
  51. </dependency>
  52. <dependency>
  53. <groupId>org.springframework</groupId>
  54. <artifactId>spring-expression</artifactId>
  55. <version>4.3.13.RELEASE</version>
  56. </dependency>
  57. <!-- spring 事务管理和JDBC依赖包 -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-tx</artifactId>
  61. <version>4.3.13.RELEASE</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework</groupId>
  65. <artifactId>spring-jdbc</artifactId>
  66. <version>4.3.13.RELEASE</version>
  67. </dependency>
  68. <!-- mysql数据库驱动包 -->
  69. <dependency>
  70. <groupId>mysql</groupId>
  71. <artifactId>mysql-connector-java</artifactId>
  72. <version>8.0.27</version>
  73. </dependency>
  74. <!-- druid连接池的依赖包 -->
  75. <dependency>
  76. <groupId>com.alibaba</groupId>
  77. <artifactId>druid</artifactId>
  78. <version>1.0.29</version>
  79. </dependency>
  80. <!-- mybatis依赖 -->
  81. <dependency>
  82. <groupId>org.mybatis</groupId>
  83. <artifactId>mybatis</artifactId>
  84. <version>3.4.6</version>
  85. </dependency>
  86. <!-- mybatis和spring的整合依赖 -->
  87. <dependency>
  88. <groupId>org.mybatis</groupId>
  89. <artifactId>mybatis-spring</artifactId>
  90. <version>1.3.2</version>
  91. </dependency>
  92. <!-- 持久层依赖 end -->
  93. <!-- 业务层依赖 begin -->
  94. <!-- 基于AspectJ的aop依赖 -->
  95. <dependency>
  96. <groupId>org.springframework</groupId>
  97. <artifactId>spring-aspects</artifactId>
  98. <version>4.3.13.RELEASE</version>
  99. </dependency>
  100. <dependency>
  101. <groupId>aopalliance</groupId>
  102. <artifactId>aopalliance</artifactId>
  103. <version>1.0</version>
  104. </dependency>
  105. <!-- 业务层依赖 end -->
  106. <!-- 表现层依赖 begin -->
  107. <!-- SpringMVC 相关包 -->
  108. <dependency>
  109. <groupId>org.springframework</groupId>
  110. <artifactId>spring-webmvc</artifactId>
  111. <version>4.3.13.RELEASE</version>
  112. </dependency>
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-web</artifactId>
  116. <version>4.3.13.RELEASE</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>javax.servlet</groupId>
  120. <artifactId>javax.servlet-api</artifactId>
  121. <version>3.1.0</version>
  122. </dependency>
  123. <dependency>
  124. <groupId>javax.servlet.jsp</groupId>
  125. <artifactId>jsp-api</artifactId>
  126. <version>2.2</version>
  127. </dependency>
  128. <dependency>
  129. <groupId>javax.servlet</groupId>
  130. <artifactId>jstl</artifactId>
  131. <version>1.2</version>
  132. </dependency>
  133. <!-- 表现层依赖 end -->
  134. <!-- 单元测试依赖 begin -->
  135. <!-- spring 单元测试组件包 -->
  136. <dependency>
  137. <groupId>org.springframework</groupId>
  138. <artifactId>spring-test</artifactId>
  139. <version>4.3.13.RELEASE</version>
  140. </dependency>
  141. <!-- 单元测试Junit -->
  142. <dependency>
  143. <groupId>junit</groupId>
  144. <artifactId>junit</artifactId>
  145. <version>4.12</version>
  146. </dependency>
  147. <!-- 单元测试依赖 end -->
  148. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  149. <dependency>
  150. <groupId>org.mybatis.generator</groupId>
  151. <artifactId>mybatis-generator-core</artifactId>
  152. <version>1.3.7</version>
  153. </dependency>
  154. <dependency>
  155. <groupId>org.slf4j</groupId>
  156. <artifactId>slf4j-log4j12</artifactId>
  157. <version>1.7.21</version>
  158. </dependency>
  159. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  160. <dependency>
  161. <groupId>com.fasterxml.jackson.core</groupId>
  162. <artifactId>jackson-databind</artifactId>
  163. <version>2.8.9</version>
  164. </dependency>
  165. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  166. <dependency>
  167. <groupId>com.fasterxml.jackson.core</groupId>
  168. <artifactId>jackson-databind</artifactId>
  169. <version>2.8.9</version>
  170. </dependency>
  171. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
  172. <dependency>
  173. <groupId>com.fasterxml.jackson.core</groupId>
  174. <artifactId>jackson-core</artifactId>
  175. <version>2.8.9</version>
  176. </dependency>
  177. <!--fastJson是一个JSON的处理工具包,由阿里巴巴公司研发推出。
  178. 我们使用它将List或者Map转换成JSON对象-->
  179. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  180. <dependency>
  181. <groupId>com.alibaba</groupId>
  182. <artifactId>fastjson</artifactId>
  183. <version>1.2.34</version>
  184. </dependency>
  185. </dependencies>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/589709
推荐阅读
相关标签
  

闽ICP备14008679号