Spring+SpringMVC+Mybatis框架搭建

待我称王封你为后i 2022-09-26 00:24 288阅读 0赞

项目建设完成之后的结构:

A100756555-124775.png_small.png

数据库的表结构如下:

A100758758-124775.png_small.png

环境建设:搭建Maven环境、Tomcat环境、需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备);

开始创建工程:

1.创建一个Maven工程:

A100800992-124775.png_small.png

选择webapp

A100803258-124775.png_small.png

随便填写了:

A100805445-124775.png_small.png

创建完成之后工程会报错:

接下来怎么解决的?

选中项目点击右键,选择“Properties” 进入下面界面:

选中project facets ,然后将java版本改成 你的jdk版本,或者1.6以上版本。

A100807820-124775.png_small.png

之后选择 java build path

A100810258-124775.png_small.png

修改lib里面的java版本:

选中上面的jre System Library 然后选择 edit A100812633-124775.png_small.png

保存之后再看看工程。上面的操作主要是让目录结构显示正常。(上面的不一定可以解决问题,工程还是会报错的)

在pom.xml文件中添加依赖:

  1. <dependency>
  2. <groupId>javax</groupId>
  3. <artifactId>javaee-api</artifactId>
  4. <version>7.0</version>
  5. </dependency>

这个立竿见影;不报错了。

之后再pom文件中添加依赖包,最终pom.xml文件如下:

  1. <pre style="font-size: 13px; padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><span style="color: rgb(232, 191, 106);"><project </span><span style="color: rgb(186, 186, 186);">xmlns</span><span style="color: rgb(106, 135, 89);">="http://maven.apache.org/POM/4.0.0" </span><span style="color: rgb(186, 186, 186);">xmlns:</span><span style="color: rgb(152, 118, 170);">xsi</span><span style="color: rgb(106, 135, 89);">="http://www.w3.org/2001/XMLSchema-instance"
  2. </span><span style="color: rgb(106, 135, 89);"> </span><span style="color: rgb(152, 118, 170);">xsi</span><span style="color: rgb(186, 186, 186);">:schemaLocation</span><span style="color: rgb(106, 135, 89);">="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span><span style="color: rgb(232, 191, 106);">>
  3. </span><span style="color: rgb(232, 191, 106);"> <modelVersion></span>4.0.0<span style="color: rgb(232, 191, 106);"></modelVersion>
  4. </span><span style="color: rgb(232, 191, 106);"> <groupId></span>com.eliteams<span style="color: rgb(232, 191, 106);"></groupId>
  5. </span><span style="color: rgb(232, 191, 106);"> <artifactId></span>quick4j<span style="color: rgb(232, 191, 106);"></artifactId>
  6. </span><span style="color: rgb(232, 191, 106);"> <packaging></span>war<span style="color: rgb(232, 191, 106);"></packaging>
  7. </span><span style="color: rgb(232, 191, 106);"> <version></span>1.0.0<span style="color: rgb(232, 191, 106);"></version>
  8. </span><span style="color: rgb(232, 191, 106);"> <name></span>quick4j App<span style="color: rgb(232, 191, 106);"></name>
  9. </span><span style="color: rgb(232, 191, 106);"> <url></span>https://github.com/starzou/quick4j<span style="color: rgb(232, 191, 106);"></url>
  10. </span>
  11. <span style="color: rgb(232, 191, 106);"> <build>
  12. </span><span style="color: rgb(232, 191, 106);"> <finalName></span>quick4j<span style="color: rgb(232, 191, 106);"></finalName>
  13. </span><span style="color: rgb(232, 191, 106);"> <plugins>
  14. </span><span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- Mybatis generator代码生成插件 配置 -->
  15. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin>
  16. </span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.mybatis.generator<span style="color: rgb(232, 191, 106);"></groupId>
  17. </span><span style="color: rgb(232, 191, 106);"> <artifactId></span>mybatis-generator-maven-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
  18. </span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.mybatis.generator}<span style="color: rgb(232, 191, 106);"></version>
  19. </span><span style="color: rgb(232, 191, 106);"> <configuration>
  20. </span><span style="color: rgb(232, 191, 106);"> <configurationFile></span>${mybatis.generator.generatorConfig.xml}<span style="color: rgb(232, 191, 106);"></configurationFile>
  21. </span><span style="color: rgb(232, 191, 106);"> <overwrite></span>true<span style="color: rgb(232, 191, 106);"></overwrite>
  22. </span><span style="color: rgb(232, 191, 106);"> <verbose></span>true<span style="color: rgb(232, 191, 106);"></verbose>
  23. </span><span style="color: rgb(232, 191, 106);"> </configuration>
  24. </span><span style="color: rgb(232, 191, 106);"> </plugin>
  25. </span>
  26. <span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- Maven插件 配置 -->
  27. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin>
  28. </span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.apache.maven.plugins<span style="color: rgb(232, 191, 106);"></groupId>
  29. </span><span style="color: rgb(232, 191, 106);"> <artifactId></span>maven-compiler-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
  30. </span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.maven-compiler}<span style="color: rgb(232, 191, 106);"></version>
  31. </span><span style="color: rgb(232, 191, 106);"> <configuration>
  32. </span><span style="color: rgb(232, 191, 106);"> <source></span>${project.build.jdk}<span style="color: rgb(232, 191, 106);"></source>
  33. </span><span style="color: rgb(232, 191, 106);"> <target></span>${project.build.jdk}<span style="color: rgb(232, 191, 106);"></target>
  34. </span><span style="color: rgb(232, 191, 106);"> <encoding></span>${project.build.sourceEncoding}<span style="color: rgb(232, 191, 106);"></encoding>
  35. </span><span style="color: rgb(232, 191, 106);"> </configuration>
  36. </span><span style="color: rgb(232, 191, 106);"> </plugin>
  37. </span>
  38. <span style="color: rgb(232, 191, 106);"> <plugin>
  39. </span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.apache.maven.plugins<span style="color: rgb(232, 191, 106);"></groupId>
  40. </span><span style="color: rgb(232, 191, 106);"> <artifactId></span>maven-surefire-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
  41. </span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.maven-surefire}<span style="color: rgb(232, 191, 106);"></version>
  42. </span><span style="color: rgb(232, 191, 106);"> <configuration>
  43. </span><span style="color: rgb(232, 191, 106);"> <skipTests></span>${skipTests}<span style="color: rgb(232, 191, 106);"></skipTests>
  44. </span><span style="color: rgb(232, 191, 106);"> </configuration>
  45. </span><span style="color: rgb(232, 191, 106);"> </plugin>
  46. </span><span style="color: rgb(232, 191, 106);"> </plugins>
  47. </span>
  48. <span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!--配置Maven 对resource文件 过滤 -->
  49. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><resources>
  50. </span><span style="color: rgb(232, 191, 106);"> <resource>
  51. </span><span style="color: rgb(232, 191, 106);"> <directory></span>src/main/resources<span style="color: rgb(232, 191, 106);"></directory>
  52. </span><span style="color: rgb(232, 191, 106);"> <includes>
  53. </span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.properties<span style="color: rgb(232, 191, 106);"></include>
  54. </span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.xml<span style="color: rgb(232, 191, 106);"></include>
  55. </span><span style="color: rgb(232, 191, 106);"> </includes>
  56. </span><span style="color: rgb(232, 191, 106);"> <</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">></span>true<span style="color: rgb(232, 191, 106);"></</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">>
  57. </span><span style="color: rgb(232, 191, 106);"> </resource>
  58. </span><span style="color: rgb(232, 191, 106);"> <resource>
  59. </span><span style="color: rgb(232, 191, 106);"> <directory></span>src/main/java<span style="color: rgb(232, 191, 106);"></directory>
  60. </span><span style="color: rgb(232, 191, 106);"> <includes>
  61. </span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.properties<span style="color: rgb(232, 191, 106);"></include>
  62. </span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.xml<span style="color: rgb(232, 191, 106);"></include>
  63. </span><span style="color: rgb(232, 191, 106);"> </includes>
  64. </span><span style="color: rgb(232, 191, 106);"> <</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">></span>true<span style="color: rgb(232, 191, 106);"></</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">>
  65. </span><span style="color: rgb(232, 191, 106);"> </resource>
  66. </span><span style="color: rgb(232, 191, 106);"> </resources>
  67. </span><span style="color: rgb(232, 191, 106);"> </build>
  68. </span>
  69. <span style="color: rgb(232, 191, 106);"> <properties>
  70. </span><span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- base setting -->
  71. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><project.build.sourceEncoding></span>UTF-8<span style="color: rgb(232, 191, 106);"></project.build.sourceEncoding>
  72. </span><span style="color: rgb(232, 191, 106);"> <project.build.locales></span>zh_CN<span style="color: rgb(232, 191, 106);"></project.build.locales>
  73. </span><span style="color: rgb(232, 191, 106);"> <project.build.jdk></span>1.7<span style="color: rgb(232, 191, 106);"></project.build.jdk>
  74. </span>
  75. <span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- plugin setting -->
  76. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><mybatis.generator.generatorConfig.xml></span>${basedir}/src/test/resources/generatorConfig.xml<span style="color: rgb(232, 191, 106);"></mybatis.generator.generatorConfig.xml>
  77. </span><span style="color: rgb(232, 191, 106);"> <mybatis.generator.generatorConfig.properties></span>file:///${basedir}/src/test/resources/generatorConfig.properties<span style="color: rgb(232, 191, 106);"></mybatis.generator.generatorConfig.properties>
  78. </span>
  79. <span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- plugin versions -->
  80. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin.mybatis.generator></span>1.3.1<span style="color: rgb(232, 191, 106);"></plugin.mybatis.generator>
  81. </span><span style="color: rgb(232, 191, 106);"> <plugin.maven-compiler></span>3.1<span style="color: rgb(232, 191, 106);"></plugin.maven-compiler>
  82. </span><span style="color: rgb(232, 191, 106);"> <plugin.maven-surefire></span>2.18.1<span style="color: rgb(232, 191, 106);"></plugin.maven-surefire>
  83. </span><span style="color: rgb(232, 191, 106);"> <skipTests></span>true<span style="color: rgb(232, 191, 106);"></skipTests>
  84. </span>
  85. <span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- lib versions -->
  86. </span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><junit.version></span>4.11<span style="color: rgb(232, 191, 106);"></junit.version>
  87. </span><span style="color: rgb(232, 191, 106);"> <spring.version></span>4.0.2.RELEASE<span style="color: rgb(232, 191, 106);"></spring.version>
  88. </span><span style="color: rgb(232, 191, 106);"> <mybatis.version></span>3.2.2<span style="color: rgb(232, 191, 106);"></mybatis.version>
  89. </span><span style="color: rgb(232, 191, 106);"> <mybatis.spring.version></span>1.2.2<span style="color: rgb(232, 191, 106);"></mybatis.spring.version>
  90. </span><span style="color: rgb(232, 191, 106);"> <mysql.connector.version></span>5.1.30<span style="color: rgb(232, 191, 106);"></mysql.connector.version>
  91. </span><span style="color: rgb(232, 191, 106);"> <postgresql.version></span>9.1-901.jdbc4<span style="color: rgb(232, 191, 106);"></postgresql.version>
  92. </span><span style="color: rgb(232, 191, 106);"> <slf4j.version></span>1.6.6<span style="color: rgb(232, 191, 106);"></slf4j.version>
  93. </span><span style="color: rgb(232, 191, 106);"> <log4j.version></span>1.2.12<span style="color: rgb(232, 191, 106);"></log4j.version>
  94. </span><span style="color: rgb(232, 191, 106);"> <httpclient.version></span>4.1.2<span style="color: rgb(232, 191, 106);"></httpclient.version>
  95. </span><span style="color: rgb(232, 191, 106);"> <jackson.version></span>1.9.13<span style="color: rgb(232, 191, 106);"></jackson.version>
  96. </span><span style="color: rgb(232, 191, 106);"> <c3p0.version></span>0.9.1.2<span style="color: rgb(232, 191, 106);"></c3p0.version>
  97. </span><span style="color: rgb(232, 191, 106);"> <druid.version></span>1.0.5<span style="color: rgb(232, 191, 106);"></druid.version>
  98. </span><span style="color: rgb(232, 191, 106);"> <tomcat.jdbc.version></span>7.0.53<span style="color: rgb(232, 191, 106);"></tomcat.jdbc.version>
  99. </span><span style="color: rgb(232, 191, 106);"> <jstl.version></span>1.2<span style="color: rgb(232, 191, 106);"></jstl.version>
  100. </span><span style="color: rgb(232, 191, 106);"> <google.collections.version></span>1.0<span style="color: rgb(232, 191, 106);"></google.collections.version>
  101. </span><span style="color: rgb(232, 191, 106);"> <cglib.version></span>3.1<span style="color: rgb(232, 191, 106);"></cglib.version>
  102. </span><span style="color: rgb(232, 191, 106);"> <shiro.version></span>1.2.3<span style="color: rgb(232, 191, 106);"></shiro.version>
  103. </span><span style="color: rgb(232, 191, 106);"> <commons.fileupload.version></span>1.3.1<span style="color: rgb(232, 191, 106);"></commons.fileupload.version>
  104. </span><span style="color: rgb(232, 191, 106);"> <commons.codec.version></span>1.9<span style="color: rgb(232, 191, 106);"></commons.codec.version>
  105. </span><span style="color: rgb(232, 191, 106);"> <commons.net.version></span>3.3<span style="color: rgb(232, 191, 106);"></commons.net.version>
  106. </span><span style="color: rgb(232, 191, 106);"> <aspectj.version></span>1.6.12<span style="color: rgb(232, 191, 106);"></aspectj.version>
  107. </span><span style="color: rgb(232, 191, 106);"> <netty.version></span>4.0.18.Final<span style="color: rgb(232, 191, 106);"></netty.version>
  108. </span><span style="color: rgb(232, 191, 106);"> <hibernate.validator.version></span>5.1.1.Final<span style="color: rgb(232, 191, 106);"></hibernate.validator.version>
  109. </span><span style="color: rgb(232, 191, 106);"> </properties></span>
  1. <!-- junit -->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>${junit.version}</version>
  6. </dependency>
  7. <!-- springframe start -->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-core</artifactId>
  11. <version>${spring.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-web</artifactId>
  16. <version>${spring.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-oxm</artifactId>
  21. <version>${spring.version}</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-tx</artifactId>
  26. <version>${spring.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-jdbc</artifactId>
  31. <version>${spring.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-webmvc</artifactId>
  36. <version>${spring.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-aop</artifactId>
  41. <version>${spring.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework</groupId>
  45. <artifactId>spring-context-support</artifactId>
  46. <version>${spring.version}</version>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-test</artifactId>
  51. <version>${spring.version}</version>
  52. </dependency>
  53. <!-- springframe end -->
  54. <!-- mybatis start-->
  55. <dependency>
  56. <groupId>org.mybatis</groupId>
  57. <artifactId>mybatis</artifactId>
  58. <version>${mybatis.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>org.mybatis</groupId>
  62. <artifactId>mybatis-spring</artifactId>
  63. <version>${mybatis.spring.version}</version>
  64. </dependency>
  65. <!--mybatis end-->
  66. <!-- mysql-connector -->
  67. <dependency>
  68. <groupId>mysql</groupId>
  69. <artifactId>mysql-connector-java</artifactId>
  70. <version>${mysql.connector.version}</version>
  71. </dependency>
  72. <!-- DruidDataSource -->
  73. <dependency>
  74. <groupId>com.alibaba</groupId>
  75. <artifactId>druid</artifactId>
  76. <version>${druid.version}</version>
  77. </dependency>
  78. <!-- jackson -->
  79. <dependency>
  80. <groupId>org.codehaus.jackson</groupId>
  81. <artifactId>jackson-mapper-asl</artifactId>
  82. <version>${jackson.version}</version>
  83. </dependency>
  84. <!-- log start -->
  85. <dependency>
  86. <groupId>log4j</groupId>
  87. <artifactId>log4j</artifactId>
  88. <version>${log4j.version}</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>org.slf4j</groupId>
  92. <artifactId>slf4j-api</artifactId>
  93. <version>${slf4j.version}</version>
  94. </dependency>
  95. <dependency>
  96. <groupId>org.slf4j</groupId>
  97. <artifactId>slf4j-log4j12</artifactId>
  98. <version>${slf4j.version}</version>
  99. </dependency>
  100. <!-- log end -->
  101. <!-- servlet api -->
  102. <dependency>
  103. <groupId>javax.servlet</groupId>
  104. <artifactId>javax.servlet-api</artifactId>
  105. <version>3.0.1</version>
  106. <scope>provided</scope>
  107. </dependency>
  108. <!-- jstl -->
  109. <dependency>
  110. <groupId>javax.servlet</groupId>
  111. <artifactId>jstl</artifactId>
  112. <version>${jstl.version}</version>
  113. </dependency>
  114. <!-- start apache -->
  115. <dependency>
  116. <groupId>commons-fileupload</groupId>
  117. <artifactId>commons-fileupload</artifactId>
  118. <version>${commons.fileupload.version}</version>
  119. </dependency>
  120. <dependency>
  121. <groupId>org.apache.httpcomponents</groupId>
  122. <artifactId>httpclient</artifactId>
  123. <version>${httpclient.version}</version>
  124. </dependency>
  125. <dependency>
  126. <groupId>commons-codec</groupId>
  127. <artifactId>commons-codec</artifactId>
  128. <version>${commons.codec.version}</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>commons-net</groupId>
  132. <artifactId>commons-net</artifactId>
  133. <version>${commons.net.version}</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>commons-logging</groupId>
  137. <artifactId>commons-logging</artifactId>
  138. <version>1.1.3</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>commons-collections</groupId>
  142. <artifactId>commons-collections</artifactId>
  143. <version>3.2.1</version>
  144. </dependency>
  145. <!-- end apache -->
  146. <!-- google -->
  147. <dependency>
  148. <groupId>com.google.collections</groupId>
  149. <artifactId>google-collections</artifactId>
  150. <version>${google.collections.version}</version>
  151. </dependency>
  152. <!-- cglib -->
  153. <dependency>
  154. <groupId>cglib</groupId>
  155. <artifactId>cglib-nodep</artifactId>
  156. <version>${cglib.version}</version>
  157. </dependency>
  158. <!-- shiro -->
  159. <dependency>
  160. <groupId>org.apache.shiro</groupId>
  161. <artifactId>shiro-spring</artifactId>
  162. <version>${shiro.version}</version>
  163. </dependency>
  164. <dependency>
  165. <groupId>org.apache.shiro</groupId>
  166. <artifactId>shiro-ehcache</artifactId>
  167. <version>${shiro.version}</version>
  168. </dependency>
  169. <dependency>
  170. <groupId>org.apache.shiro</groupId>
  171. <artifactId>shiro-core</artifactId>
  172. <version>${shiro.version}</version>
  173. </dependency>
  174. <dependency>
  175. <groupId>org.apache.shiro</groupId>
  176. <artifactId>shiro-web</artifactId>
  177. <version>${shiro.version}</version>
  178. </dependency>
  179. <dependency>
  180. <groupId>org.apache.shiro</groupId>
  181. <artifactId>shiro-quartz</artifactId>
  182. <version>${shiro.version}</version>
  183. </dependency>
  184. <!-- aspectjweaver -->
  185. <dependency>
  186. <groupId>org.aspectj</groupId>
  187. <artifactId>aspectjweaver</artifactId>
  188. <version>${aspectj.version}</version>
  189. </dependency>
  190. <dependency>
  191. <groupId>org.aspectj</groupId>
  192. <artifactId>aspectjrt</artifactId>
  193. <version>${aspectj.version}</version>
  194. </dependency>
  195. <!-- hibernate-validator -->
  196. <dependency>
  197. <groupId>org.hibernate</groupId>
  198. <artifactId>hibernate-validator</artifactId>
  199. <version>${hibernate.validator.version}</version>
  200. </dependency>
  201. <!-- netty -->
  202. <dependency>
  203. <groupId>io.netty</groupId>
  204. <artifactId>netty-all</artifactId>
  205. <version>${netty.version}</version>
  206. </dependency>
  207. <dependency>
  208. <groupId>org.mybatis.generator</groupId>
  209. <artifactId>mybatis-generator-core</artifactId>
  210. <version>1.3.2</version>
  211. <type>jar</type>
  212. <scope>test</scope>
  213. </dependency>
  214. <dependency>
  215. <groupId>redis.clients</groupId>
  216. <artifactId>jedis</artifactId>
  217. <version>2.6.2</version>
  218. <type>jar</type>
  219. <scope>compile</scope>
  220. </dependency>
org.bouncycastlebcprov-ext-jdk15on1.50org.apache.directory.studioorg.apache.commons.codec1.8com.sun.mailjavax.mail1.5.0commons-dbcpcommons-dbcp1.2.2



C1


之后再看lib里面是否都导入了jar包:


接着需要配置resource下面的四个配置文件,目录结构如下:


jdbc.properties 、log4j.properties、 spring-mvc.xml 、 spring-mybatis.xml 这些文件可能都需要自己创建。

需要自己创建一个数据库表testu,包括四个字段 id username password age


jdbc.properties 配置如下:

  1. jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/testu?useUnicode=true&characterEncoding=UTF-8
    jdbc.username=root
    jdbc.password=root
    #\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
    jdbc.initialSize=0
    #\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
    jdbc.maxActive=20
    #\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
    jdbc.maxIdle=20
    #\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
    jdbc.minIdle=1
    #\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
    jdbc.maxWait=60000


    log4j.properties配置如下:


    #定义LOG输出级别
    log4j.rootLogger=INFO,Console,File
    #定义日志输出目的地为控制台
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.Target=System.out
    #可以灵活地指定日志输出格式,下面一行是指定具体的格式
    log4j.appender.Console.layout = org.apache.log4j.PatternLayout
    log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

    #文件大小到达指定尺寸的时候产生一个新的文件
    log4j.appender.File = org.apache.log4j.RollingFileAppender
    #指定输出目录
    log4j.appender.File.File = logs/ssm.log
    #定义文件最大大小
    log4j.appender.File.MaxFileSize = 10MB
    # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
    log4j.appender.File.Threshold = ALL
    log4j.appender.File.layout = org.apache.log4j.PatternLayout
    log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n


    spring-mvc.xml 配置如下:


    <?xml version=”1.0 encoding=”UTF-8”?>

  2.     <context:component-scan base-package="com.cn.uuu.controller" />
  3.     <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
  4.     <bean id="mappingJacksonHttpMessageConverter"
  5.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  6.         <property name="supportedMediaTypes">
  7.             <list>
  8.                 <value>text/html;charset=UTF-8</value>
  9.             </list>
  10.         </property>
  11.     </bean>
  12.     <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
  13.     <bean
  14.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  15.         <property name="messageConverters">
  16.             <list>
  17.                 <ref bean="mappingJacksonHttpMessageConverter" />    <!-- JSON转换器 -->
  18.             </list>
  19.         </property>
  20.     </bean>
  21.     <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  23.         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
  24.         <property name="prefix" value="/WEB-INF/jsp/" />
  25.         <property name="suffix" value=".jsp" />
  26.     </bean>
  27.     <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
  28.     <bean id="multipartResolver"  
  29.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  30.         <!-- 默认编码 -->
  31.         <property name="defaultEncoding" value="utf-8" />  
  32.         <!-- 文件大小最大值 -->
  33.         <property name="maxUploadSize" value="10485760000" />  
  34.         <!-- 内存中的最大值 -->
  35.         <property name="maxInMemorySize" value="40960" />  
  36.     </bean> 
  37. </beans>
  38. spring-mybatis.xml配置文件:

  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <beans xmlns="http://www.springframework.org/schema/beans"
  41.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  42.     xmlns:context="http://www.springframework.org/schema/context"
  43.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  44.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  45.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  46.                         http://www.springframework.org/schema/context  
  47.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  48.                         http://www.springframework.org/schema/mvc  
  49.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  50.     <!-- 自动扫描 -->
  51.     <context:component-scan base-package="com.cn.uuu" />
  52.     <!-- 引入配置文件 -->
  53.     <bean id="propertyConfigurer"
  54.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  55.         <property name="location" value="classpath:jdbc.properties" />
  56.     </bean>
  57.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  58.         destroy-method="close">
  59.         <property name="driverClassName" value="${jdbc.driver}" />
  60.         <property name="url" value="${jdbc.url}" />
  61.         <property name="username" value="${jdbc.username}" />
  62.         <property name="password" value="${jdbc.password}" />
  63.         <!-- 初始化连接大小 -->
  64.         <property name="initialSize" value="${jdbc.initialSize}"></property>
  65.         <!-- 连接池最大数量 -->
  66.         <property name="maxActive" value="${jdbc.maxActive}"></property>
  67.         <!-- 连接池最大空闲 -->
  68.         <property name="maxIdle" value="${jdbc.maxIdle}"></property>
  69.         <!-- 连接池最小空闲 -->
  70.         <property name="minIdle" value="${jdbc.minIdle}"></property>
  71.         <!-- 获取连接最大等待时间 -->
  72.         <property name="maxWait" value="${jdbc.maxWait}"></property>
  73.     </bean>
  74.     <!-- springMyBatis完美整合,不需要mybatis的配置映射文件 -->
  75.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  76.         <property name="dataSource" ref="dataSource" />
  77.         <!-- 自动扫描mapping.xml文件 -->
  78.         <property name="mapperLocations" value="classpath:com/cn/uuu/mapping/*.xml"></property>
  79.     </bean>
  80.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  81.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  82.         <property name="basePackage" value="com.cn.uuu.IDao" />
  83.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  84.     </bean>
  85.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  86.     <bean id="transactionManager"
  87.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  88.         <property name="dataSource" ref="dataSource" />
  89.     </bean>
  90. </beans>
  91. 到这里配置文件基本配置完毕

  92. 接下来怎么创建一个简单的实例运行:

  93. 可以参考这篇文章 http://blog.csdn.net/cool\_easy/article/details/42778239 使用代码生成。生成

  94. 这三个包,还有几个文件需要自己创建。

  95. 代码结构如下

  96. 最后的代码详见:http://download.csdn.net/detail/naploen8/8381755

  97. 原文链接: http://blog.csdn.net/cool_easy/article/details/42963573

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SSM框架

     自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供!(后续读写分离:[https://bl