Maven创建Web项目、、、整合SSM框架

时间:2023-01-26 14:11:05

自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的《淘淘商城》一个ssm框架的电商项目。用过,但是还真的没有自己搭建过,一直都是用别人搭建好的。所以,从网上找了一些材料,结合自己来解决过程中的一些问题,使自己对ssm框架理解更加深了,不为分享,只是总结一下,下次有机会再搭建的时候,直接看自己的博客,省得找了!

一、准备环境:

maven:apache-maven-3.2.3

jdk:jdk1.8.0_25

tomcat:tomcat-6.0.37

二、配置Maven、jdk

1、Window——>Preferences——>Maven——>设置自己的Settings

2、Window——>Preferences——>Java——>Installed JREs——>Add

Maven创建Web项目、、、整合SSM框架

配置好maven之后,就可以在本地设置好的地方看到对应的Maven文件了,还有包含resp文件夹

三、新建Maven项目:

1、右击——>New(或者是File——>New)——>other——>Maven——>Maven Project——>Next

Maven创建Web项目、、、整合SSM框架

Maven创建Web项目、、、整合SSM框架

2、这时候,我们就成功创建出一个Maven项目了,项目结构如下图:

Maven创建Web项目、、、整合SSM框架

3、右击项目,选择Properties进行一些配置:

Maven创建Web项目、、、整合SSM框架

到这里,我们的Maven项目就建好了,接下来,我们来整合搭建SSM(spring MVC + Spring + Mybatis)

首先我们来配置一些整合框架所必须要的jar包,也就是来配置我们的pom文件,然后直接alt+f5,保存后,就会自动下载,存放在之前配置好的本地maven仓库中。

pox.xml配置依赖的内容:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.ssm</groupId>
  5. <artifactId>Maven_Project</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>Maven_Project Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <!-- 用来设置版本号 -->
  11. <properties>
  12. <srping.version>4.0.2.RELEASE</srping.version>
  13. <mybatis.version>3.2.8</mybatis.version>
  14. <slf4j.version>1.7.12</slf4j.version>
  15. <log4j.version>1.2.17</log4j.version>
  16. </properties>
  17. <!-- 用到的jar包 -->
  18. <dependencies>
  19. <!-- 单元测试 -->
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.11</version>
  24. <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- java ee包 -->
  28. <dependency>
  29. <groupId>javax</groupId>
  30. <artifactId>javaee-api</artifactId>
  31. <version>7.0</version>
  32. </dependency>
  33. <!-- spring框架包 start -->
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-test</artifactId>
  37. <version>${srping.version}</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework</groupId>
  41. <artifactId>spring-core</artifactId>
  42. <version>${srping.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-oxm</artifactId>
  47. <version>${srping.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-tx</artifactId>
  52. <version>${srping.version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-jdbc</artifactId>
  57. <version>${srping.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-aop</artifactId>
  62. <version>${srping.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-context</artifactId>
  67. <version>${srping.version}</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-context-support</artifactId>
  72. <version>${srping.version}</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-expression</artifactId>
  77. <version>${srping.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-orm</artifactId>
  82. <version>${srping.version}</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.springframework</groupId>
  86. <artifactId>spring-web</artifactId>
  87. <version>${srping.version}</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>org.springframework</groupId>
  91. <artifactId>spring-webmvc</artifactId>
  92. <version>${srping.version}</version>
  93. </dependency>
  94. <!-- spring框架包 end -->
  95. <!-- mybatis框架包 start -->
  96. <dependency>
  97. <groupId>org.mybatis</groupId>
  98. <artifactId>mybatis</artifactId>
  99. <version>${mybatis.version}</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.mybatis</groupId>
  103. <artifactId>mybatis-spring</artifactId>
  104. <version>1.2.2</version>
  105. </dependency>
  106. <!-- mybatis框架包 end -->
  107. <!-- 数据库驱动 -->
  108. <dependency>
  109. <groupId>mysql</groupId>
  110. <artifactId>mysql-connector-java</artifactId>
  111. <version>5.1.35</version>
  112. </dependency>
  113. <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
  114. <dependency>
  115. <groupId>commons-dbcp</groupId>
  116. <artifactId>commons-dbcp</artifactId>
  117. <version>1.4</version>
  118. </dependency>
  119. <!-- jstl标签类 -->
  120. <dependency>
  121. <groupId>jstl</groupId>
  122. <artifactId>jstl</artifactId>
  123. <version>1.2</version>
  124. </dependency>
  125. <!-- log start -->
  126. <dependency>
  127. <groupId>log4j</groupId>
  128. <artifactId>log4j</artifactId>
  129. <version>${log4j.version}</version>
  130. </dependency>
  131. <dependency>
  132. <groupId>org.slf4j</groupId>
  133. <artifactId>slf4j-api</artifactId>
  134. <version>${slf4j.version}</version>
  135. </dependency>
  136. <dependency>
  137. <groupId>org.slf4j</groupId>
  138. <artifactId>slf4j-log4j12</artifactId>
  139. <version>${slf4j.version}</version>
  140. </dependency>
  141. <!-- log END -->
  142. <!-- Json  -->
  143. <!-- 格式化对象,方便输出日志 -->
  144. <dependency>
  145. <groupId>com.alibaba</groupId>
  146. <artifactId>fastjson</artifactId>
  147. <version>1.2.6</version>
  148. </dependency>
  149. <dependency>
  150. <groupId>org.codehaus.jackson</groupId>
  151. <artifactId>jackson-mapper-asl</artifactId>
  152. <version>1.9.13</version>
  153. </dependency>
  154. <!-- 上传组件包 start -->
  155. <dependency>
  156. <groupId>commons-fileupload</groupId>
  157. <artifactId>commons-fileupload</artifactId>
  158. <version>1.3.1</version>
  159. </dependency>
  160. <dependency>
  161. <groupId>commons-io</groupId>
  162. <artifactId>commons-io</artifactId>
  163. <version>2.4</version>
  164. </dependency>
  165. <dependency>
  166. <groupId>commons-codec</groupId>
  167. <artifactId>commons-codec</artifactId>
  168. <version>1.10</version>
  169. </dependency>
  170. <!-- 上传组件包 end -->
  171. </dependencies>
  172. <build>
  173. <finalName>Maven_Project</finalName>
  174. </build>
  175. </project>

2、在src/main/resources下添加配置文件: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" xmlns:p="http://www.springframework.org/schema/p"
  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-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!-- 使用注解式注入 -->
  13. <context:annotation-config />
  14. <!-- 自动扫描 -->
  15. <context:component-scan base-package="com.ssm" />
  16. <!-- 导入DAO配置 -->
  17. <import resource="spring-dao.xml"/>
  18. <!-- 导入数据库配置 -->
  19. <import resource="spring-db.xml"/>
  20. <!-- 导入数据库配置 -->
  21. <import resource="spring-tx.xml"/>
  22. </beans>

配置数据库连接池:jdbc.properties

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/maven_ssm
  3. username=root
  4. password=root
  5. #定义初始连接数
  6. initialSize=0
  7. #定义最大连接数
  8. maxActive=20
  9. #定义最大空闲
  10. maxIdle=20
  11. #定义最小空闲
  12. minIdle=1
  13. #定义最长等待时间
  14. maxWait=60000

配置日志:log4j.properties

  1. #定义LOG输出级别
  2. log4j.rootLogger=INFO,Console,File
  3. #定义日志输出目的地为控制台
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Console.Target=System.out
  6. #可以灵活地指定日志输出格式,下面一行是指定具体的格式
  7. log4j.appender.Console.layout = org.apache.log4j.PatternLayout
  8. log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
  9. #文件大小到达指定尺寸的时候产生一个新的文件
  10. log4j.appender.File = org.apache.log4j.RollingFileAppender
  11. #指定输出目录
  12. log4j.appender.File.File = logs/ssm.log
  13. #定义文件最大大小
  14. log4j.appender.File.MaxFileSize = 10MB
  15. # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
  16. log4j.appender.File.Threshold = ALL
  17. log4j.appender.File.layout = org.apache.log4j.PatternLayout
  18. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

配置我们的ioc注入:spring-dao.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" xmlns:p="http://www.springframework.org/schema/p"
  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-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  13. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  14. <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。
  15. 可指定多个包,包与包之间用逗号或分号分隔-->
  16. <property name="basePackage" value="com.ssm.dao" />
  17. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  18. </bean>
  19. </beans>

spring-db.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" xmlns:p="http://www.springframework.org/schema/p"
  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-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!-- 引入配置文件 -->
  13. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  14. <property name="location" value="classpath:jdbc.properties" />
  15. </bean>
  16. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  17. <property name="driverClassName" value="${driver}" />
  18. <property name="url" value="${url}" />
  19. <property name="username" value="${username}" />
  20. <property name="password" value="${password}" />
  21. <!-- 初始化连接大小 -->
  22. <property name="initialSize" value="${initialSize}"></property>
  23. <!-- 连接池最大数量 -->
  24. <property name="maxActive" value="${maxActive}"></property>
  25. <!-- 连接池最大空闲 -->
  26. <property name="maxIdle" value="${maxIdle}"></property>
  27. <!-- 连接池最小空闲 -->
  28. <property name="minIdle" value="${minIdle}"></property>
  29. <!-- 获取连接最大等待时间 -->
  30. <property name="maxWait" value="${maxWait}"></property>
  31. </bean>
  32. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  33. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  34. <property name="dataSource" ref="dataSource" />
  35. <!-- 自动扫描mapping.xml文件 -->
  36. <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
  37. </bean>
  38. </beans>

配置spring的事务管理器:spring-tx.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" xmlns:p="http://www.springframework.org/schema/p"
  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-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  13. <bean id="transactionManager"
  14. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  15. <property name="dataSource" ref="dataSource" />
  16. </bean>
  17. </beans>

接下来,我们来整合spring mvc: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" xmlns:p="http://www.springframework.org/schema/p"
  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-3.1.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  12. <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
  13. <bean id="mappingJacksonHttpMessageConverter"
  14. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  15. <property name="supportedMediaTypes">
  16. <list>
  17. <value>text/html;charset=UTF-8</value>
  18. </list>
  19. </property>
  20. </bean>
  21. <!-- 添加注解驱动 -->
  22. <mvc:annotation-driven />
  23. <mvc:default-servlet-handler/>
  24. <!-- 设置使用注解的类所在的包 -->
  25. <context:component-scan base-package="com.ssm.controller" />
  26. <!-- 完成请求和注解POJO的映射 -->
  27. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  28. <property name="messageConverters">
  29. <list>
  30. <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
  31. </list>
  32. </property>
  33. </bean>
  34. <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
  35. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  36. <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
  37. <property name="prefix" value="/WEB-INF/jsp/" />
  38. <property name="suffix" value=".jsp" />
  39. </bean>
  40. <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->
  41. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  42. <!-- 默认编码 -->
  43. <property name="defaultEncoding" value="utf-8" />
  44. <!-- 文件大小最大值 -->
  45. <property name="maxUploadSize" value="10485760000" />
  46. <!-- 内存中的最大值 -->
  47. <property name="maxInMemorySize" value="40960" />
  48. </bean>
  49. </beans>

修改web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. version="3.0">
  6. <display-name>Archetype Created Web Application</display-name>
  7. <welcome-file-list>
  8. <welcome-file>/index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- 加载spring bean -->
  11. <context-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </context-param>
  15. <listener>
  16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  17. </listener>
  18. <!-- 编码过滤器 -->
  19. <filter>
  20. <filter-name>encodingFilter</filter-name>
  21. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  22. <async-supported>true</async-supported>
  23. <init-param>
  24. <param-name>encoding</param-name>
  25. <param-value>UTF-8</param-value>
  26. </init-param>
  27. </filter>
  28. <filter-mapping>
  29. <filter-name>encodingFilter</filter-name>
  30. <url-pattern>/*</url-pattern>
  31. </filter-mapping>
  32. <!-- Spring MVC servlet -->
  33. <servlet>
  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>/WEB-INF/spring-mvc.xml</param-value>
  39. </init-param>
  40. <load-on-startup>1</load-on-startup>
  41. <async-supported>true</async-supported>
  42. </servlet>
  43. <servlet-mapping>
  44. <servlet-name>SpringMVC</servlet-name>
  45. <url-pattern>/</url-pattern>
  46. </servlet-mapping>
  47. </web-app>

到这里,我们的ssm框架就完成了,下面,我们来个实现:

在src/main/java下分别添加我们的dao包,service包,还有controller包.包里面添加相应的类,效果图如下:

Maven创建Web项目、、、整合SSM框架

UserController.java:

  1. package com.ssm.controller;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import javax.servlet.http.HttpServletRequest;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import com.ssm.dto.User;
  9. import com.ssm.service.IUserService;
  10. @Controller
  11. @RequestMapping("/user")
  12. public class UserController {
  13. @Resource
  14. private IUserService userService;
  15. @RequestMapping("/userList")
  16. public String userList(HttpServletRequest request,Model model){
  17. List<User> uList = userService.getAllUser();
  18. model.addAttribute("uList", uList);
  19. return "userList";
  20. }
  21. @RequestMapping("/showUser")
  22. public String showUser(HttpServletRequest request,Model model){
  23. int userId = Integer.parseInt(request.getParameter("id"));
  24. User user = userService.getUserById(userId);
  25. model.addAttribute("user", user);
  26. return "showUser";
  27. }
  28. @RequestMapping("/addUserUI")
  29. public String addUserUI(){
  30. return "addUser";
  31. }
  32. @RequestMapping("/addUser")
  33. public String addUser(HttpServletRequest request,Model model){
  34. User user = new User();
  35. user.setName(String.valueOf(request.getParameter("name")));
  36. user.setPassword(String.valueOf(request.getParameter("password")));
  37. user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));
  38. userService.addUser(user);
  39. return "redirect:/user/userList";
  40. }
  41. }

IUserDao.java:

  1. package com.ssm.dao;
  2. import java.util.List;
  3. import java.util.Map;
  4. import com.ssm.dto.User;
  5. public interface IUserDao {
  6. public User queryByPrimaryKey(Integer id);
  7. public List<User> queryUserByBatch(Map<String,Object> params);
  8. public void insertUser(User user);
  9. public void insertUserByBatch(List<User> list);
  10. public void deleteByPrimaryKey(Integer id);
  11. public void delteUserByBatch(Map<String,Object> params);
  12. public void updateByPrimaryKey(Integer id);
  13. public List<User> getAllUser();
  14. }

User.java:

  1. package com.ssm.dto;
  2. public class User {
  3. private Integer id;
  4. private String name;
  5. private String password;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password == null ? null : password.trim();
  23. }
  24. }

UserMapper.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.ssm.dao.IUserDao" >
  4. <resultMap id="BaseResultMap" type="com.ssm.dto.User">
  5. <result column="id" property="id" jdbcType="INTEGER" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="password" property="password" jdbcType="VARCHAR" />
  8. <result column="age" property="age" jdbcType="INTEGER" />
  9. </resultMap>
  10. <sql id="Base_Column_List">
  11. id, name, password, age
  12. </sql>
  13. <!-- 查询用户-->
  14. <select id="queryByPrimaryKey" resultMap="BaseResultMap"
  15. parameterType="java.lang.Integer">
  16. select
  17. <include refid="Base_Column_List" />
  18. from user
  19. where id = #{id,jdbcType=INTEGER}
  20. </select>
  21. <!-- 查询用户-->
  22. <select id="getAllUser" resultMap="BaseResultMap">
  23. select
  24. <include refid="Base_Column_List" />
  25. from user
  26. </select>
  27. <!-- 批量查询用户-->
  28. <select id="queryUserByBatch" resultMap="BaseResultMap"
  29. parameterType="java.util.Map">
  30. select
  31. <include refid="Base_Column_List" />
  32. from user
  33. where id in
  34. <foreach collection="idList" item="userId" index="index" open="(" separator="," close=")">
  35. #{userId,jdbcType=DECIMAL}
  36. </foreach>
  37. </select>
  38. <!-- 插入用户 -->
  39. <insert id="insertUser" parameterType="com.ssm.dto.User">
  40. insert into user
  41. <trim prefix="(" suffix=")" suffixOverrides="," >
  42. <if test="id != null" >
  43. id,
  44. </if>
  45. <if test="name != null" >
  46. name,
  47. </if>
  48. <if test="password != null" >
  49. password,
  50. </if>
  51. <if test="age != null" >
  52. age,
  53. </if>
  54. </trim>
  55. <trim prefix="values (" suffix=")" suffixOverrides="," >
  56. <if test="id != null" >
  57. #{id,jdbcType=INTEGER},
  58. </if>
  59. <if test="name != null" >
  60. #{name,jdbcType=VARCHAR},
  61. </if>
  62. <if test="password != null" >
  63. #{password,jdbcType=VARCHAR},
  64. </if>
  65. <if test="age != null" >
  66. #{age,jdbcType=INTEGER},
  67. </if>
  68. </trim>
  69. </insert>
  70. <!-- 批量插入用户 -->
  71. <insert id="insertUserByBatch" parameterType="java.util.List" >
  72. insert into USER
  73. (ID,
  74. NAME,
  75. PASSWORD,
  76. AGE)
  77. select A.* from
  78. (
  79. <foreach collection="list" item="user" index="index" separator="union">
  80. select
  81. #{user.id,jdbcType=INTEGER},
  82. #{user.name,jdbcType=VARCHAR},
  83. #{user.password,jdbcType=VARCHAR},
  84. #{user.age,jdbcType=INTEGER}
  85. from dual
  86. </foreach>
  87. ) A
  88. </insert>
  89. <!-- 删除用户 -->
  90. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  91. delete from user
  92. where id = #{id,jdbcType=INTEGER}
  93. </delete>
  94. <!-- 批量删除用户 -->
  95. <delete id="deleteUserByBatch" parameterType="java.util.Map" >
  96. delete from user
  97. where id IN
  98. <foreach item="ids" collection="iList" open="(" separator="," close=")">
  99. #{ids,jdbcType=DECIMAL}
  100. </foreach>
  101. </delete>
  102. <!-- 更新用户 -->
  103. <update id="updateByPrimaryKey" parameterType="com.ssm.dto.User" >
  104. update user
  105. <set >
  106. <if test="name != null" >
  107. name = #{name,jdbcType=VARCHAR},
  108. </if>
  109. <if test="password != null" >
  110. password = #{password,jdbcType=VARCHAR},
  111. </if>
  112. <if test="age != null" >
  113. age = #{age,jdbcType=INTEGER},
  114. </if>
  115. </set>
  116. where id = #{id,jdbcType=INTEGER}
  117. </update>
  118. </mapper>

IUserService.java:

  1. package com.ssm.service;
  2. import java.util.List;
  3. import com.ssm.dto.User;
  4. public interface IUserService {
  5. public User getUserById(int userId);
  6. public void insertUser(User user);
  7. public void addUser(User user);
  8. public List<User> getAllUser();
  9. }

UserServiceImpl.java:

  1. package com.ssm.service.Impl;
  2. import java.util.List;
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Service;
  5. import com.ssm.dao.IUserDao;
  6. import com.ssm.dto.User;
  7. import com.ssm.service.IUserService;
  8. @Service("userService")
  9. public class UserServiceImpl implements IUserService {
  10. @Resource
  11. private IUserDao userDao;
  12. public User getUserById(int userId) {
  13. return userDao.queryByPrimaryKey(userId);
  14. }
  15. public void insertUser(User user) {
  16. userDao.insertUser(user);
  17. }
  18. public void addUser(User user) {
  19. userDao.insertUser(user);
  20. }
  21. @Override
  22. public List<User> getAllUser() {
  23. return userDao.getAllUser();
  24. }
  25. }

接下来就是我们的静态页面了jsp文件了,在WEB-INF文件夹下新建jsp文件夹,添加userinfo.jsp

userinfo.jsp:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath=request.getSSScheme()+"://request.getServerName()"+":"+request.getServerPort+Path+
  5. "/"%>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'userinfo.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <form id="addUser" action="user/addUser" method="post">
  22. userName: <input id="name" name="name" /><br/>
  23. password: <input id="password" name="password" /><br/>
  24. <input type="submit" value="添加新用户"/>
  25. </form>
  26. </body>
  27. </html>

哦,忘记配置tomcat,配置一下tomcat,然后部署发布项目,进行测试

Maven创建Web项目、、、整合SSM框架

到此,框架成