当前位置:   article > 正文

在IntelliJ IDEA中集成SSM项目_idea ssm

idea ssm

SSM项目:springMVC为控制器、spring 为事务层、 MyBatis 负责持久

首先看下集成后项目结构:

1、打开IntelliJ IDEA,点击 "File" -> "New" -> "Project"。

点击Finish,此时我们就已经创建了一个maven-web项目;

2、在src/main 下,创建 java和resources文件夹;

在java文件夹下:创建controller,service,dao,entity等文件夹。

在resources文件夹下创建mappers(存放实体类映射.xml文件)文件夹和配置文件。

applicationContext.xml --spring 容器相关配置;

dispatcherServlet.xml --springMvc 相关配置;

mybatis-config.xml --mybatis 相关配置;

generatorConfig.xml --自动生成Dao、Model层相关代码;

jdbc.properties --jdbc常量配置;

log4j.properties --集成log4j

3、在webapp/WEB-INF 下,创建views文件夹,存放静态资源;

web.xml --web-servlet相关配置;

4、pom文件相关依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>org.example</groupId>
  6. <artifactId>web-ssm</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>web-ssm Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. <!-- spring版本号 -->
  17. <spring.version>4.2.5.RELEASE</spring.version>
  18. <!-- mybatis版本号 -->
  19. <mybatis.version>3.2.4</mybatis.version>
  20. <!-- log4j日志文件管理包版本 -->
  21. <slf4j.version>1.6.6</slf4j.version>
  22. <log4j.version>1.2.12</log4j.version>
  23. <!-- log4j日志文件管理包版本 -->
  24. <slf4j.version>1.6.6</slf4j.version>
  25. <log4j.version>1.2.12</log4j.version>
  26. </properties>
  27. <dependencies>
  28. <dependency>
  29. <groupId>junit</groupId>
  30. <artifactId>junit</artifactId>
  31. <version>4.11</version>
  32. <scope>test</scope>
  33. </dependency>
  34. <!-- spring核心包 -->
  35. <!-- springframe start -->
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-core</artifactId>
  39. <version>${spring.version}</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>
  43. <artifactId>spring-web</artifactId>
  44. <version>${spring.version}</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-oxm</artifactId>
  49. <version>${spring.version}</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-tx</artifactId>
  54. <version>${spring.version}</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-jdbc</artifactId>
  59. <version>${spring.version}</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-webmvc</artifactId>
  64. <version>${spring.version}</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-aop</artifactId>
  69. <version>${spring.version}</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-context-support</artifactId>
  74. <version>${spring.version}</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-test</artifactId>
  79. <version>${spring.version}</version>
  80. </dependency>
  81. <!-- springframe end -->
  82. <!-- mybatis核心包 -->
  83. <dependency>
  84. <groupId>org.mybatis</groupId>
  85. <artifactId>mybatis</artifactId>
  86. <version>${mybatis.version}</version>
  87. </dependency>
  88. <!-- mybatis/spring包 -->
  89. <dependency>
  90. <groupId>org.mybatis</groupId>
  91. <artifactId>mybatis-spring</artifactId>
  92. <version>1.2.2</version>
  93. </dependency>
  94. <!-- mysql驱动包 -->
  95. <dependency>
  96. <groupId>mysql</groupId>
  97. <artifactId>mysql-connector-java</artifactId>
  98. <version>8.0.19</version>
  99. </dependency>
  100. <!-- c3p0 -->
  101. <dependency>
  102. <groupId>com.mchange</groupId>
  103. <artifactId>c3p0</artifactId>
  104. <version>0.9.5.5</version>
  105. </dependency>
  106. <!-- junit测试包 -->
  107. <dependency>
  108. <groupId>junit</groupId>
  109. <artifactId>junit</artifactId>
  110. <version>4.11</version>
  111. <scope>test</scope>
  112. </dependency>
  113. <!-- jstl -->
  114. <dependency>
  115. <groupId>javax.servlet</groupId>
  116. <artifactId>jstl</artifactId>
  117. <version>1.2</version>
  118. </dependency>
  119. <dependency>
  120. <groupId>javax.servlet.jsp</groupId>
  121. <artifactId>jsp-api</artifactId>
  122. <version>2.2.1-b03</version>
  123. <scope>provided</scope>
  124. </dependency>
  125. <!-- servlet -->
  126. <dependency>
  127. <groupId>javax.servlet</groupId>
  128. <artifactId>servlet-api</artifactId>
  129. <version>2.5</version>
  130. <scope>provided</scope>
  131. </dependency>
  132. <!-- json数据 -->
  133. <dependency>
  134. <groupId>com.fasterxml.jackson.core</groupId>
  135. <artifactId>jackson-core</artifactId>
  136. <version>2.7.1</version>
  137. </dependency>
  138. <dependency>
  139. <groupId>com.fasterxml.jackson.core</groupId>
  140. <artifactId>jackson-annotations</artifactId>
  141. <version>2.7.1</version>
  142. </dependency>
  143. <dependency>
  144. <groupId>com.fasterxml.jackson.core</groupId>
  145. <artifactId>jackson-databind</artifactId>
  146. <version>2.7.1</version>
  147. </dependency>
  148. <dependency>
  149. <groupId>net.sf.json-lib</groupId>
  150. <artifactId>json-lib</artifactId>
  151. <version>2.4</version>
  152. <classifier>jdk15</classifier>
  153. </dependency>
  154. <!-- commons -->
  155. <dependency>
  156. <groupId>commons-lang</groupId>
  157. <artifactId>commons-lang</artifactId>
  158. <version>2.4</version>
  159. </dependency>
  160. <dependency>
  161. <groupId>commons-logging</groupId>
  162. <artifactId>commons-logging</artifactId>
  163. <version>1.1</version>
  164. </dependency>
  165. <dependency>
  166. <groupId>commons-pool</groupId>
  167. <artifactId>commons-pool</artifactId>
  168. <version>1.5.6</version>
  169. </dependency>
  170. <dependency>
  171. <groupId>commons-dbcp</groupId>
  172. <artifactId>commons-dbcp</artifactId>
  173. <version>1.4</version>
  174. </dependency>
  175. <dependency>
  176. <groupId>commons-beanutils</groupId>
  177. <artifactId>commons-beanutils</artifactId>
  178. <version>1.8.3</version>
  179. </dependency>
  180. <dependency>
  181. <groupId>commons-httpclient</groupId>
  182. <artifactId>commons-httpclient</artifactId>
  183. <version>3.1</version>
  184. </dependency>
  185. <dependency>
  186. <groupId>commons-collections</groupId>
  187. <artifactId>commons-collections</artifactId>
  188. <version>3.1</version>
  189. </dependency>
  190. <dependency>
  191. <groupId>commons-codec</groupId>
  192. <artifactId>commons-codec</artifactId>
  193. <version>1.9</version>
  194. </dependency>
  195. <dependency>
  196. <groupId>javax.annotation</groupId>
  197. <artifactId>jsr250-api</artifactId>
  198. <version>1.0</version>
  199. </dependency>
  200. <dependency>
  201. <groupId>net.sf.ezmorph</groupId>
  202. <artifactId>ezmorph</artifactId>
  203. <version>1.0.6</version>
  204. </dependency>
  205. <dependency>
  206. <groupId>javax.activation</groupId>
  207. <artifactId>activation</artifactId>
  208. <version>1.1</version>
  209. </dependency>
  210. <!--<dependency>
  211. <groupId>taglibs</groupId>
  212. <artifactId>standard</artifactId>
  213. <version>1.1.2</version>
  214. </dependency>-->
  215. <!-- 日志文件管理包 -->
  216. <!-- log start -->
  217. <dependency>
  218. <groupId>log4j</groupId>
  219. <artifactId>log4j</artifactId>
  220. <version>${log4j.version}</version>
  221. </dependency>
  222. <dependency>
  223. <groupId>org.slf4j</groupId>
  224. <artifactId>slf4j-api</artifactId>
  225. <version>${slf4j.version}</version>
  226. </dependency>
  227. <dependency>
  228. <groupId>org.slf4j</groupId>
  229. <artifactId>slf4j-log4j12</artifactId>
  230. <version>${slf4j.version}</version>
  231. </dependency>
  232. <!--上传-->
  233. <dependency>
  234. <groupId>commons-fileupload</groupId>
  235. <artifactId>commons-fileupload</artifactId>
  236. <version>1.3.1</version>
  237. </dependency>
  238. <dependency>
  239. <groupId>commons-io</groupId>
  240. <artifactId>commons-io</artifactId>
  241. <version>2.4</version>
  242. </dependency>
  243. <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
  244. <dependency>
  245. <groupId>org.mybatis.generator</groupId>
  246. <artifactId>mybatis-generator-core</artifactId>
  247. <version>1.3.5</version>
  248. </dependency>
  249. <dependency>
  250. <groupId>cglib</groupId>
  251. <artifactId>cglib</artifactId>
  252. <version>3.2.5</version>
  253. </dependency>
  254. <!-- log end -->
  255. </dependencies>
  256. <build>
  257. <finalName>web-ssm</finalName>
  258. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  259. <plugins>
  260. <plugin>
  261. <artifactId>maven-clean-plugin</artifactId>
  262. <version>3.1.0</version>
  263. </plugin>
  264. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  265. <plugin>
  266. <artifactId>maven-resources-plugin</artifactId>
  267. <version>3.0.2</version>
  268. </plugin>
  269. <plugin>
  270. <artifactId>maven-compiler-plugin</artifactId>
  271. <version>3.8.0</version>
  272. </plugin>
  273. <plugin>
  274. <artifactId>maven-surefire-plugin</artifactId>
  275. <version>2.22.1</version>
  276. </plugin>
  277. <plugin>
  278. <artifactId>maven-war-plugin</artifactId>
  279. <version>3.2.2</version>
  280. </plugin>
  281. <plugin>
  282. <artifactId>maven-install-plugin</artifactId>
  283. <version>2.5.2</version>
  284. </plugin>
  285. <plugin>
  286. <artifactId>maven-deploy-plugin</artifactId>
  287. <version>2.8.2</version>
  288. </plugin>
  289. </plugins>
  290. </pluginManagement>
  291. <plugins>
  292. <plugin>
  293. <groupId>org.mybatis.generator</groupId>
  294. <artifactId>mybatis-generator-maven-plugin</artifactId>
  295. <version>1.3.2</version>
  296. <configuration>
  297. <!--允许移动生成的文件-->
  298. <verbose>true</verbose>
  299. <!--允许覆盖生成的文件-->
  300. <overwrite>true</overwrite>
  301. </configuration>
  302. </plugin>
  303. </plugins>
  304. </build>
  305. </project>

注*:

1*、本例MySQL Server 8.0

2*、使用 mybatis-generator-maven-plugin,在pom.xml中<pluginManagement>和<plugins>要同级,如下:

  1. <build>
  2. <finalName>web-ssm</finalName>
  3. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  4. </pluginManagement>
  5. <plugins>
  6. <plugin>
  7. <groupId>org.mybatis.generator</groupId>
  8. <artifactId>mybatis-generator-maven-plugin</artifactId>
  9. <version>1.3.2</version>
  10. <configuration>
  11. <!--允许移动生成的文件-->
  12. <verbose>true</verbose>
  13. <!--允许覆盖生成的文件-->
  14. <overwrite>true</overwrite>
  15. </configuration>
  16. </plugin>
  17. </plugins>
  18. </build>

5、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. 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. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/springtx.xsd">
  11. <!--配合MyBatis-->
  12. <!--配置数据库参数-->
  13. <context:property-placeholder location="classpath:jdbc.properties"/>
  14. <!--数据库连接池-->
  15. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  16. <!--配置连接池属性-->
  17. <property name="driverClass" value="${jdbc.driver}"/>
  18. <property name="jdbcUrl" value="${jdbc.url}"/>
  19. <property name="user" value="${jdbc.username}"/>
  20. <property name="password" value="${jdbc.password}"/>
  21. <!--c3p0连接池私有属性-->
  22. <property name="maxPoolSize" value="${maxPoolSize}"/>
  23. <property name="minPoolSize" value="${minPoolSize}"/>
  24. <property name="autoCommitOnClose" value="false"/>
  25. <property name="checkoutTimeout" value="${checkoutTimeout}"/>
  26. <property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
  27. <property name="initialPoolSize" value="${initialPoolSize}"/>
  28. </bean>
  29. <!-- 配置 SqlSessionFactory 对象,spring和MyBatis完美整合,不需要mybatis的配置映射文件-->
  30. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  31. <!--注入数据库连接池-->
  32. <property name="dataSource" ref="dataSource"/>
  33. <!-- 配置 MyBaties 全局配置⽂件:mybatis-config.xml -->
  34. <property name="configLocation" value="classpath:mybatis-config.xml"/>
  35. <!-- 扫描 model 包 使⽤别名 -->
  36. <property name="typeAliasesPackage" value="org.example.entity"/>
  37. <!-- 扫描 sql 配置⽂件:mapper 需要的 xml ⽂件 -->
  38. <property name="mapperLocations" value="classpath:mappers/*.xml"/>
  39. </bean>
  40. <!-- 配置扫描 dao 接⼝包, 动态实现 dao 接⼝, 注⼊到 Spring 容器中 -->
  41. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  42. <!-- 注⼊ sqlSessionFactory -->
  43. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  44. <!-- 给出需要扫描 dao 接⼝包 -->
  45. <property name="basePackage" value="org.example.dao"/>
  46. </bean>
  47. <!--spring-service-->
  48. <!-- 扫描 service 包下所有使⽤注解的类型 -->
  49. <context:component-scan base-package="org.example.service"/>
  50. <!-- 配置事务管理器 -->
  51. <bean id="transactionManager"
  52. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  53. <!-- 注⼊数据库连接池 -->
  54. <property name="dataSource" ref="dataSource"/>
  55. </bean>
  56. </beans>

6、dispatcherServlet.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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  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/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd">
  13. <!-- controller扫描器 -->
  14. <context:component-scan base-package="org.example.controller"/>
  15. <!-- 配置注解驱动 -->
  16. <mvc:annotation-driven/>
  17. <!-- 视图解析器 -->
  18. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  19. <!--前缀-->
  20. <property name="prefix" value="/WEB-INF/views/"/>
  21. <!--后缀-->
  22. <property name="suffix" value=".jsp"/>
  23. </bean>
  24. <!--静态资源默认servlet配置
  25. (1)加⼊对静态资源的处理: js,gif,png
  26. (2)允许使⽤"/"做整体映射
  27. -->
  28. <mvc:default-servlet-handler/>
  29. </beans>

7、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. <settings>
  7. <setting name="mapUnderscoreToCamelCase" value="true"/>
  8. <setting name="jdbcTypeForNull" value="NULL"/>
  9. <!--显式的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
  10. <setting name="cacheEnabled" value="true"/>
  11. <setting name="lazyLoadingEnabled" value="true"/>
  12. <setting name="aggressiveLazyLoading" value="false"/>
  13. <!-- 使⽤jdbc的getGeneratedKeys获取数据库⾃增主键值 -->
  14. <setting name="useGeneratedKeys" value="true" />
  15. <!-- 使⽤列别名替换列名 默认:true -->
  16. <setting name="useColumnLabel" value="true" />
  17. <setting name="cacheEnabled" value="true"/>
  18. </settings>
  19. </configuration>

8、generatorConfig.xml 相关配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!--导入属性配置 jdbc配置文件-->
  7. <properties resource="jdbc.properties"></properties>
  8. <!--指定特定数据库的jdbc驱动jar包的位置 此处需要指明mysql-connector-java-5.1.30.jar的绝对路径,否则会报错!-->
  9. <!-- <classPathEntry location="D:\database\mysql\jar\mysql-connector-java-5.1.37-bin.jar"/>-->
  10. <classPathEntry location="E:\MyRepository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar"/>
  11. <context id="default" targetRuntime="MyBatis3">
  12. <!-- optional,旨在创建class时,对注释进行控制 -->
  13. <commentGenerator>
  14. <property name="suppressDate" value="true"/>
  15. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  16. <property name="suppressAllComments" value="true"/>
  17. </commentGenerator>
  18. <!--jdbc的数据库连接 -->
  19. <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}"
  20. userId="${jdbc.username}" password="${jdbc.password}">
  21. </jdbcConnection>
  22. <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
  23. <javaTypeResolver>
  24. <property name="forceBigDecimals" value="false"/>
  25. </javaTypeResolver>
  26. <!-- 生成模型model的包名和位置 此处需要根据自己项目做调整-->
  27. <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
  28. targetPackage 指定生成的model生成所在的包名
  29. targetProject 指定在该项目下所在的路径
  30. -->
  31. <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
  32. <javaModelGenerator targetPackage="org.example.entity" targetProject="./src/main/java">
  33. <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
  34. <property name="enableSubPackages" value="false"/>
  35. <!-- 是否对model添加 构造函数 -->
  36. <property name="constructorBased" value="true"/>
  37. <!-- 是否对类CHAR类型的列的数据进行trim操作 (去空)-->
  38. <property name="trimStrings" value="true"/>
  39. <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
  40. <property name="immutable" value="false"/>
  41. </javaModelGenerator>
  42. <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
  43. <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
  44. <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
  45. <property name="enableSubPackages" value="false"/>
  46. </sqlMapGenerator>
  47. <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
  48. type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
  49. type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
  50. type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
  51. -->
  52. <!-- targetPackage:mapper接口dao生成的位置 -->
  53. <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
  54. <javaClientGenerator type="XMLMAPPER" targetPackage="org.example.dao" targetProject="./src/main/java">
  55. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  56. <property name="enableSubPackages" value="false"/>
  57. </javaClientGenerator>
  58. <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
  59. <table tableName="student" domainObjectName="Student" enableCountByExample="false"
  60. enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
  61. selectByExampleQueryId="false">
  62. </table>
  63. <!-- 数据库中该字段的类型是 txt ,不同版本生成对应字体类的属性类型可能不同,因此指定转换类型 -->
  64. <!-- mybatis插件的搭建 -->
  65. </context>
  66. </generatorConfiguration>

9、jdbc.properties 相关配置:

  1. #jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.driver=com.mysql.cj.jdbc.Driver
  3. jdbc.url=jdbc:mysql://localhost:3306/testdata?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
  4. jdbc.username=root
  5. jdbc.password=yhuan
  6. #最大连接数
  7. maxPoolSize=20
  8. #最小连接数
  9. minPoolSize=10
  10. #连接超时时间
  11. checkoutTimeout=60000
  12. #失败重连次数
  13. acquireRetryAttempts=3
  14. #初始化连接数量
  15. initialPoolSize=10

10、log4j.properties 相关配置:

  1. ### set log levels ###
  2. log4j.rootLogger = INFO,D
  3. ###输出到控制台###
  4. log4j.logger.toConsole=debug,stdout
  5. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  6. log4j.appender.stdout.Target = System.out
  7. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  8. log4j.appender.stdout.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n
  9. ### 输出到日志文件 ###
  10. ## 其中File 可以采用绝对路径 亦可以采用相对路径 catalina.home 是tomcat目录 如果没有logs文件夹 将会报错 ##
  11. ## 更加倾向于将日志文件放在 D:/database/log4j/log4jTest/logtest.log ##
  12. ## 绝对路径如:e:/logtest.log ##
  13. log4j.logger.daily=INFO,D
  14. log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
  15. log4j.appender.D.File = E:/log4j/log4jTest/logtest.log
  16. log4j.appender.D.Append = true
  17. log4j.appender.D.Threshold = INFO
  18. log4j.appender.D.layout = org.apache.log4j.PatternLayout
  19. log4j.appender.D.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n
  20. ##log4j.logger.org.apache.ibatis=debug,stdout
  21. ##log4j.logger.java.sql=debug,stdout

11、web.xml 相关配置:

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <!--Spring MVC 配置 并添加监听-->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:applicationContext.xml</param-value>
  10. </context-param>
  11. <!-- 字符过滤器 传值乱码-->
  12. <filter>
  13. <filter-name>encodingFilter</filter-name>
  14. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  15. <init-param>
  16. <param-name>encoding</param-name>
  17. <param-value>UTF-8</param-value>
  18. </init-param>
  19. <init-param>
  20. <param-name>forceEncoding</param-name>
  21. <param-value>true</param-value>
  22. </init-param>
  23. </filter>
  24. <filter-mapping>
  25. <filter-name>encodingFilter</filter-name>
  26. <url-pattern>/*</url-pattern>
  27. </filter-mapping>
  28. <listener>
  29. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  30. </listener>
  31. <!--配置前端控制器 进行请求分发 DispatcherServlet本质也是一个Servlet -->
  32. <servlet>
  33. <!--名字可以自定义-->
  34. <servlet-name>SpringMVC</servlet-name>
  35. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  36. <init-param>
  37. <param-name>contextConfigLocation</param-name>
  38. <param-value>classpath:dispatcherServlet.xml</param-value>
  39. </init-param>
  40. <!--标记容器启动的时候就启动这个servlet-->
  41. <load-on-startup>1</load-on-startup>
  42. </servlet>
  43. <servlet-mapping>
  44. <servlet-name>SpringMVC</servlet-name>
  45. <!--拦截所有-->
  46. <url-pattern>/</url-pattern>
  47. </servlet-mapping>
  48. <!-- <absolute-ordering />-->
  49. </web-app>

12、使用mybatis-generator-maven-plugin:

右键→Run Maven Build,即可在generatorConfig.xml配置的路径下生成Dao,Model层代码。

13、项目添加tomcat启动:

14、新建TestController.java,启动tomcat,测试---

  1. package org.example.controller;
  2. import org.example.entity.Student;
  3. import org.example.entity.User;
  4. import org.example.service.TestService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.Statement;
  14. import java.util.List;
  15. @Controller
  16. @RequestMapping(value = "/test")
  17. public class TestController {
  18. @Autowired
  19. private TestService testService;
  20. @RequestMapping(value = "/testQuery",produces = "text/html;charset=UTF-8")
  21. @ResponseBody
  22. public String testQuery(){
  23. System.out.println("TestController--------------------------------------------");
  24. List<User> userList = testService.query();
  25. return userList.get(0).getName();
  26. }
  27. @RequestMapping(value = "/student/{id}",produces = "text/html;charset=UTF-8")
  28. @ResponseBody
  29. public String student(@PathVariable("id") Integer id){
  30. System.out.println("student-id:"+ id + "------------------------");
  31. Student student = testService.student(id);
  32. return student.getName();
  33. }
  34. @RequestMapping(value = "/hello")
  35. public String hello(){
  36. return "hello";
  37. }
  38. public static void main(String[] args) {
  39. try{
  40. Class.forName("com.mysql.jdbc.Driver");
  41. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdata?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8","root","yhuan");
  42. Statement myStmt = conn.createStatement();
  43. ResultSet rs = myStmt.executeQuery("SELECT * FROM user");
  44. while(rs.next()) {
  45. System.out.println(rs.getString("name"));
  46. }
  47. }
  48. catch(Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }

注*:内附main方法,自测数据库连通性。

新建/webapp/WEB-INF/views/hello.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Administrator
  4. Date: 2024/1/16
  5. Time: 16:20
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>this is helloPage</title>
  12. </head>
  13. <body>
  14. <h3>这是SSM框架的 hello页面</h3>
  15. </body>
  16. </html>

http://localhost:8080/web-ssm/test/testQuery

http://localhost:8080/web-ssm/test/hello

参考文章:IDEA 详细SSM框架的搭建_ssm 文件夹结构-CSDN博客

end!!!

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

闽ICP备14008679号