【SSM】Eclipse使用Maven创建Web项目+整合SSM框架

自己接触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之后,就可以在本地设置好的地方看到对应的Maven文件了,还有包含resp文件夹

 

三、新建Maven项目:

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

 

 

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

 

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

 

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

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

pox.xml配置依赖的内容:

 

[html]  view plain copy
 print?
  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.0modelVersion>    
  4.     <groupId>com.ssmgroupId>    
  5.     <artifactId>Maven_ProjectartifactId>    
  6.     <packaging>warpackaging>    
  7.     <version>0.0.1-SNAPSHOTversion>    
  8.     <name>Maven_Project Maven Webappname>    
  9.     <url>http://maven.apache.orgurl>    
  10.       
  11.         
  12.     <properties>    
  13.         <srping.version>4.0.2.RELEASEsrping.version>    
  14.         <mybatis.version>3.2.8mybatis.version>    
  15.         <slf4j.version>1.7.12slf4j.version>    
  16.         <log4j.version>1.2.17log4j.version>    
  17.     properties>    
  18.         
  19.     <dependencies>    
  20.             
  21.         <dependency>    
  22.             <groupId>junitgroupId>    
  23.             <artifactId>junitartifactId>    
  24.             <version>4.11version>    
  25.                   
  26.             <scope>testscope>    
  27.         dependency>    
  28.             
  29.         <dependency>    
  30.             <groupId>javaxgroupId>    
  31.             <artifactId>javaee-apiartifactId>    
  32.             <version>7.0version>    
  33.         dependency>    
  34.             
  35.         <dependency>    
  36.             <groupId>org.springframeworkgroupId>    
  37.             <artifactId>spring-testartifactId>    
  38.             <version>${srping.version}version>    
  39.         dependency>    
  40.         <dependency>    
  41.             <groupId>org.springframeworkgroupId>    
  42.             <artifactId>spring-coreartifactId>    
  43.             <version>${srping.version}version>    
  44.         dependency>    
  45.         <dependency>    
  46.             <groupId>org.springframeworkgroupId>    
  47.             <artifactId>spring-oxmartifactId>    
  48.             <version>${srping.version}version>    
  49.         dependency>    
  50.         <dependency>    
  51.             <groupId>org.springframeworkgroupId>    
  52.             <artifactId>spring-txartifactId>    
  53.             <version>${srping.version}version>    
  54.         dependency>    
  55.         <dependency>    
  56.             <groupId>org.springframeworkgroupId>    
  57.             <artifactId>spring-jdbcartifactId>    
  58.             <version>${srping.version}version>    
  59.         dependency>    
  60.         <dependency>    
  61.             <groupId>org.springframeworkgroupId>    
  62.             <artifactId>spring-aopartifactId>    
  63.             <version>${srping.version}version>    
  64.         dependency>    
  65.         <dependency>    
  66.             <groupId>org.springframeworkgroupId>    
  67.             <artifactId>spring-contextartifactId>    
  68.             <version>${srping.version}version>    
  69.         dependency>    
  70.         <dependency>    
  71.             <groupId>org.springframeworkgroupId>    
  72.             <artifactId>spring-context-supportartifactId>    
  73.             <version>${srping.version}version>    
  74.         dependency>    
  75.         <dependency>    
  76.             <groupId>org.springframeworkgroupId>    
  77.             <artifactId>spring-expressionartifactId>    
  78.             <version>${srping.version}version>    
  79.         dependency>    
  80.         <dependency>    
  81.             <groupId>org.springframeworkgroupId>    
  82.             <artifactId>spring-ormartifactId>    
  83.             <version>${srping.version}version>    
  84.         dependency>    
  85.         <dependency>    
  86.             <groupId>org.springframeworkgroupId>    
  87.             <artifactId>spring-webartifactId>    
  88.             <version>${srping.version}version>    
  89.         dependency>    
  90.         <dependency>    
  91.             <groupId>org.springframeworkgroupId>    
  92.             <artifactId>spring-webmvcartifactId>    
  93.             <version>${srping.version}version>    
  94.         dependency>    
  95.             
  96.             
  97.         <dependency>    
  98.             <groupId>org.mybatisgroupId>    
  99.             <artifactId>mybatisartifactId>    
  100.             <version>${mybatis.version}version>    
  101.         dependency>    
  102.         <dependency>    
  103.             <groupId>org.mybatisgroupId>    
  104.             <artifactId>mybatis-springartifactId>    
  105.             <version>1.2.2version>    
  106.         dependency>    
  107.             
  108.             
  109.         <dependency>    
  110.             <groupId>mysqlgroupId>    
  111.             <artifactId>mysql-connector-javaartifactId>    
  112.             <version>5.1.35version>    
  113.         dependency>    
  114.             
  115.         <dependency>    
  116.             <groupId>commons-dbcpgroupId>    
  117.             <artifactId>commons-dbcpartifactId>    
  118.             <version>1.4version>    
  119.         dependency>    
  120.             
  121.         <dependency>    
  122.             <groupId>jstlgroupId>    
  123.             <artifactId>jstlartifactId>    
  124.             <version>1.2version>    
  125.         dependency>    
  126.             
  127.         <dependency>    
  128.             <groupId>log4jgroupId>    
  129.             <artifactId>log4jartifactId>    
  130.             <version>${log4j.version}version>    
  131.         dependency>    
  132.         <dependency>    
  133.             <groupId>org.slf4jgroupId>    
  134.             <artifactId>slf4j-apiartifactId>    
  135.             <version>${slf4j.version}version>    
  136.         dependency>    
  137.         <dependency>    
  138.             <groupId>org.slf4jgroupId>    
  139.             <artifactId>slf4j-log4j12artifactId>    
  140.             <version>${slf4j.version}version>    
  141.         dependency>    
  142.             
  143.             
  144.             
  145.         <dependency>    
  146.             <groupId>com.alibabagroupId>    
  147.             <artifactId>fastjsonartifactId>    
  148.             <version>1.2.6version>    
  149.         dependency>    
  150.         <dependency>    
  151.             <groupId>org.codehaus.jacksongroupId>    
  152.             <artifactId>jackson-mapper-aslartifactId>    
  153.             <version>1.9.13version>    
  154.         dependency>    
  155.             
  156.         <dependency>    
  157.             <groupId>commons-fileuploadgroupId>    
  158.             <artifactId>commons-fileuploadartifactId>    
  159.             <version>1.3.1version>    
  160.         dependency>    
  161.         <dependency>    
  162.             <groupId>commons-iogroupId>    
  163.             <artifactId>commons-ioartifactId>    
  164.             <version>2.4version>    
  165.         dependency>    
  166.         <dependency>    
  167.             <groupId>commons-codecgroupId>    
  168.             <artifactId>commons-codecartifactId>    
  169.             <version>1.10version>    
  170.         dependency>    
  171.             
  172.     dependencies>    
  173.       
  174.     <build>    
  175.       <finalName>Maven_ProjectfinalName>    
  176.     build>    
  177. project>    

 

 

2、在src/main/resources下添加配置文件:applicationContext.xml:

 

[html]  view plain copy
 print?
  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.         
  14.     <context:annotation-config />    
  15.         
  16.         
  17.     <context:component-scan base-package="com.ssm" />    
  18.         
  19.         
  20.     <import resource="spring-dao.xml"/>    
  21.         
  22.         
  23.     <import resource="spring-db.xml"/>    
  24.         
  25.         
  26.     <import resource="spring-tx.xml"/>    
  27.         
  28. beans>    

 

 

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

 

 

[html]  view plain copy
 print?
  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

 

[html]  view plain copy
 print?
  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. #文件大小到达指定尺寸的时候产生一个新的文件    
  11. log4j.appender.File = org.apache.log4j.RollingFileAppender    
  12. #指定输出目录    
  13. log4j.appender.File.File = logs/ssm.log    
  14. #定义文件最大大小    
  15. log4j.appender.File.MaxFileSize = 10MB    
  16. # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志    
  17. log4j.appender.File.Threshold = ALL    
  18. log4j.appender.File.layout = org.apache.log4j.PatternLayout    
  19. log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n    


配置我们的ioc注入:spring-dao.xml

 

 

[html]  view plain copy
 print?
  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.                             
  14.         
  15.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
  16.             
  17.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  18.         <property name="location" value="classpath:jdbc.properties" />    
  19.     bean>    
  20.         
  21.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
  22.         <property name="driverClassName" value="${driver}" />    
  23.         <property name="url" value="${url}" />    
  24.         <property name="username" value="${username}" />    
  25.         <property name="password" value="${password}" />    
  26.             
  27.         <property name="initialSize" value="${initialSize}">property>    
  28.             
  29.         <property name="maxActive" value="${maxActive}">property>    
  30.             
  31.         <property name="maxIdle" value="${maxIdle}">property>    
  32.             
  33.         <property name="minIdle" value="${minIdle}">property>    
  34.             
  35.         <property name="maxWait" value="${maxWait}">property>    
  36.     bean>    
  37.     
  38.         
  39.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
  40.         <property name="dataSource" ref="dataSource" />    
  41.             
  42.         <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml">property>    
  43.     bean>    
  44.         
  45. beans>    


配置spring的事务管理器:spring-tx.xml

 

 

[html]  view plain copy
 print?
  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.         
  14.     <bean id="transactionManager"    
  15.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
  16.         <property name="dataSource" ref="dataSource" />    
  17.     bean>    
  18.         
  19. beans>    


接下来,我们来整合spring mvc:spring-mvc.xml:

 

[html]  view plain copy
 print?
  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.         
  14.     <bean id="mappingJacksonHttpMessageConverter"    
  15.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
  16.         <property name="supportedMediaTypes">    
  17.             <list>    
  18.                 <value>text/html;charset=UTF-8value>    
  19.             list>    
  20.         property>    
  21.     bean>    
  22.         
  23.            
  24.     <mvc:annotation-driven />    
  25.     <mvc:default-servlet-handler/>    
  26.         
  27.         
  28.     <context:component-scan base-package="com.ssm.controller" />    
  29.         
  30.         
  31.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
  32.         <property name="messageConverters">    
  33.             <list>    
  34.                 <ref bean="mappingJacksonHttpMessageConverter" />     
  35.             list>    
  36.         property>    
  37.     bean>    
  38.         
  39.         
  40.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  41.             
  42.         <property name="prefix" value="/WEB-INF/jsp/" />    
  43.         <property name="suffix" value=".jsp" />    
  44.     bean>    
  45.         
  46.         
  47.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
  48.             
  49.         <property name="defaultEncoding" value="utf-8" />      
  50.             
  51.         <property name="maxUploadSize" value="10485760000" />      
  52.             
  53.         <property name="maxInMemorySize" value="40960" />      
  54.     bean>     
  55.     
  56. beans>    


修改web.xml

 

 

[html]  view plain copy
 print?
  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 Applicationdisplay-name>    
  7.         
  8.     <welcome-file-list>    
  9.         <welcome-file>/index.jspwelcome-file>    
  10.     welcome-file-list>    
  11.         
  12.         
  13.     <context-param>    
  14.          <param-name>contextConfigLocationparam-name>    
  15.         <param-value>classpath:applicationContext.xmlparam-value>    
  16.     context-param>    
  17.     <listener>    
  18.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>    
  19.     listener>    
  20.         
  21.         
  22.     <filter>    
  23.         <filter-name>encodingFilterfilter-name>    
  24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>    
  25.         <async-supported>trueasync-supported>    
  26.         <init-param>    
  27.             <param-name>encodingparam-name>    
  28.             <param-value>UTF-8param-value>    
  29.         init-param>    
  30.     filter>    
  31.     <filter-mapping>    
  32.         <filter-name>encodingFilterfilter-name>    
  33.         <url-pattern>/*url-pattern>    
  34.     filter-mapping>    
  35.         
  36.         
  37.     <servlet>    
  38.         <servlet-name>SpringMVCservlet-name>    
  39.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>    
  40.         <init-param>    
  41.             <param-name>contextConfigLocationparam-name>    
  42.             <param-value>/WEB-INF/spring-mvc.xmlparam-value>    
  43.         init-param>    
  44.         <load-on-startup>1load-on-startup>    
  45.         <async-supported>trueasync-supported>    
  46.     servlet>    
  47.     <servlet-mapping>    
  48.         <servlet-name>SpringMVCservlet-name>    
  49.         <url-pattern>/url-pattern>    
  50.     servlet-mapping>    
  51.         
  52. web-app>    

 

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

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

 

 

UserController.java:

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

 

 

IUserDao.java:

 

[java]  view plain copy
 print?
  1. package com.ssm.dao;    
  2.     
  3. import java.util.List;    
  4. import java.util.Map;    
  5.     
  6. import com.ssm.dto.User;    
  7.     
  8. public interface IUserDao {    
  9.         
  10.     public User queryByPrimaryKey(Integer id);    
  11.         
  12.     public List queryUserByBatch(Map params);    
  13.         
  14.     public void insertUser(User user);    
  15.         
  16.     public void insertUserByBatch(List list);    
  17.         
  18.     public void deleteByPrimaryKey(Integer id);    
  19.         
  20.     public void delteUserByBatch(Map params);    
  21.         
  22.     public void updateByPrimaryKey(Integer id);    
  23.     
  24.     public List getAllUser();    
  25.         
  26. }    

 

 

User.java:

 

[java]  view plain copy
 print?
  1. package com.ssm.dto;    
  2.     
  3. public class User {    
  4.     private Integer id;    
  5.     
  6.     private String name;    
  7.     
  8.     private String password;    
  9.     
  10.     public Integer getId() {    
  11.         return id;    
  12.     }    
  13.     
  14.     public void setId(Integer id) {    
  15.         this.id = id;    
  16.     }    
  17.     
  18.     public String getName() {    
  19.         return name;    
  20.     }    
  21.     
  22.     public void setName(String name) {    
  23.         this.name = name;    
  24.     }    
  25.     
  26.     public String getPassword() {    
  27.         return password;    
  28.     }    
  29.     
  30.     public void setPassword(String password) {    
  31.         this.password = password == null ? null : password.trim();    
  32.     }    
  33. }    


UserMapper.xml:

 

 

[html]  view plain copy
 print?
  1. xml version="1.0" encoding="UTF-8" ?>    
  2. >    
  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.     
  11.     <sql id="Base_Column_List">    
  12.         id, name, password, age    
  13.     sql>    
  14.     
  15.         
  16.     <select id="queryByPrimaryKey" resultMap="BaseResultMap"    
  17.         parameterType="java.lang.Integer">    
  18.         select    
  19.         <include refid="Base_Column_List" />    
  20.         from user    
  21.         where id = #{id,jdbcType=INTEGER}    
  22.     select>    
  23.         
  24.         
  25.     <select id="getAllUser" resultMap="BaseResultMap">    
  26.         select    
  27.         <include refid="Base_Column_List" />    
  28.         from user    
  29.     select>    
  30.         
  31.         
  32.     <select id="queryUserByBatch" resultMap="BaseResultMap"    
  33.         parameterType="java.util.Map">    
  34.         select    
  35.         <include refid="Base_Column_List" />    
  36.         from user    
  37.         where id in    
  38.         <foreach collection="idList" item="userId" index="index" open="(" separator="," close=")">    
  39.              #{userId,jdbcType=DECIMAL}    
  40.         foreach>    
  41.     select>    
  42.         
  43.         
  44.     <insert id="insertUser" parameterType="com.ssm.dto.User">    
  45.         insert into user      
  46.         <trim prefix="(" suffix=")" suffixOverrides="," >      
  47.           <if test="id != null" >      
  48.             id,      
  49.           if>      
  50.           <if test="name != null" >      
  51.             name,      
  52.           if>      
  53.           <if test="password != null" >      
  54.             password,      
  55.           if>      
  56.           <if test="age != null" >      
  57.             age,      
  58.           if>      
  59.         trim>      
  60.         <trim prefix="values (" suffix=")" suffixOverrides="," >      
  61.           <if test="id != null" >      
  62.             #{id,jdbcType=INTEGER},      
  63.           if>      
  64.           <if test="name != null" >      
  65.             #{name,jdbcType=VARCHAR},      
  66.           if>      
  67.           <if test="password != null" >      
  68.             #{password,jdbcType=VARCHAR},      
  69.           if>      
  70.           <if test="age != null" >      
  71.             #{age,jdbcType=INTEGER},      
  72.           if>      
  73.         trim>       
  74.     insert>    
  75.         
  76.         
  77.     <insert id="insertUserByBatch" parameterType="java.util.List" >    
  78.     insert into USER    
  79.        (ID,    
  80.         NAME,    
  81.         PASSWORD,    
  82.         AGE)    
  83.     select A.* from    
  84.          (    
  85.          <foreach collection="list" item="user" index="index" separator="union">    
  86.              select     
  87.              #{user.id,jdbcType=INTEGER},    
  88.              #{user.name,jdbcType=VARCHAR},    
  89.              #{user.password,jdbcType=VARCHAR},    
  90.              #{user.age,jdbcType=INTEGER}    
  91.               from dual    
  92.          foreach>    
  93.          ) A    
  94.     insert>    
  95.     
  96.         
  97.     <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">    
  98.         delete from user    
  99.         where id = #{id,jdbcType=INTEGER}      
  100.     delete>    
  101.         
  102.         
  103.     <delete id="deleteUserByBatch" parameterType="java.util.Map" >    
  104.      delete from user    
  105.      where id IN     
  106.          <foreach item="ids" collection="iList" open="(" separator="," close=")">    
  107.              #{ids,jdbcType=DECIMAL}    
  108.          foreach>    
  109.     delete>    
  110.     
  111.         
  112.     <update id="updateByPrimaryKey" parameterType="com.ssm.dto.User" >      
  113.         update user      
  114.         <set >      
  115.           <if test="name != null" >      
  116.             name = #{name,jdbcType=VARCHAR},      
  117.           if>      
  118.           <if test="password != null" >      
  119.             password = #{password,jdbcType=VARCHAR},      
  120.           if>      
  121.           <if test="age != null" >      
  122.             age = #{age,jdbcType=INTEGER},      
  123.           if>      
  124.         set>      
  125.         where id = #{id,jdbcType=INTEGER}     
  126.     update>    
  127.       
  128. mapper>    


IUserService.java:

 

 

[java]  view plain copy
 print?
  1. package com.ssm.service;    
  2.     
  3. import java.util.List;    
  4.     
  5. import com.ssm.dto.User;    
  6.     
  7. public interface IUserService {    
  8.         
  9.     public User getUserById(int userId);    
  10.     
  11.     public void insertUser(User user);    
  12.     
  13.     public void addUser(User user);    
  14.     
  15.     public List getAllUser();    
  16. }    


UserServiceImpl.java:

[java]  view plain copy
 print?
  1. package com.ssm.service.Impl;    
  2.     
  3. import java.util.List;    
  4.     
  5. import javax.annotation.Resource;    
  6.     
  7. import org.springframework.stereotype.Service;    
  8.     
  9. import com.ssm.dao.IUserDao;    
  10. import com.ssm.dto.User;    
  11. import com.ssm.service.IUserService;    
  12.     
  13. @Service("userService")    
  14. public class UserServiceImpl implements IUserService {    
  15.     @Resource    
  16.     private IUserDao userDao;    
  17.         
  18.     public User getUserById(int userId) {    
  19.         return userDao.queryByPrimaryKey(userId);    
  20.     }    
  21.     
  22.     public void insertUser(User user) {    
  23.         userDao.insertUser(user);    
  24.     }    
  25.     
  26.     public void addUser(User user) {    
  27.         userDao.insertUser(user);    
  28.     }    
  29.     
  30.     @Override    
  31.     public List getAllUser() {    
  32.         return userDao.getAllUser();    
  33.     }    
  34.     
  35. }    


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

 

userinfo.jsp:

 

[html]  view plain copy
 print?
  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. >    
  7. <html>    
  8.   <head>    
  9.     <base href="<%=basePath%>">    
  10.         
  11.     <title>My JSP 'userinfo.jsp' starting pagetitle>    
  12.         
  13.     <meta http-equiv="pragma" content="no-cache">    
  14.     <meta http-equiv="cache-control" content="no-cache">    
  15.     <meta http-equiv="expires" content="0">        
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    
  17.     <meta http-equiv="description" content="This is my page">    
  18.         
  19.     
  20.   head>    
  21.       
  22.   <body>    
  23.     <form id="addUser" action="user/addUser" method="post">     
  24.         userName: <input id="name" name="name" /><br/>     
  25.         password: <input id="password" name="password" /><br/>    
  26.         <input type="submit" value="添加新用户"/>     
  27.     form>    
  28.   body>    
  29. html>     

 

 

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

到此,框架成

 

总结:    真心知道用maven之后,我们大框架真的方便多了,最起码不用个顶个的去找jar包,然后逐一的 导入,只需要在pom文件里面配置一下依赖,就好了。虽然说比较简单,可是可能是自己没有动手弄过 吧,期间除了各种各样的错,或者说是失误,但是,总之,耗时一天半,总算是通了,这也是收获, 加油吧,少(qing)年。

转载于:https://www.cnblogs.com/DoubleEggs/p/6243216.html

你可能感兴趣的:(【SSM】Eclipse使用Maven创建Web项目+整合SSM框架)