zoukankan      html  css  js  c++  java
  • 【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配置依赖的内容:

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

    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. #文件大小到达指定尺寸的时候产生一个新的文件    
    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

    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.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->    
    15.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
    16.         <!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。    
    17.          可指定多个包,包与包之间用逗号或分号分隔-->    
    18.         <property name="basePackage" value="com.ssm.dao" />    
    19.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    
    20.     </bean>                           
    21.                             
    22. </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.     <!-- 引入配置文件 -->    
    14.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
    15.         <property name="location" value="classpath:jdbc.properties" />    
    16.     </bean>    
    17.         
    18.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    
    19.         <property name="driverClassName" value="${driver}" />    
    20.         <property name="url" value="${url}" />    
    21.         <property name="username" value="${username}" />    
    22.         <property name="password" value="${password}" />    
    23.         <!-- 初始化连接大小 -->    
    24.         <property name="initialSize" value="${initialSize}"></property>    
    25.         <!-- 连接池最大数量 -->    
    26.         <property name="maxActive" value="${maxActive}"></property>    
    27.         <!-- 连接池最大空闲 -->    
    28.         <property name="maxIdle" value="${maxIdle}"></property>    
    29.         <!-- 连接池最小空闲 -->    
    30.         <property name="minIdle" value="${minIdle}"></property>    
    31.         <!-- 获取连接最大等待时间 -->    
    32.         <property name="maxWait" value="${maxWait}"></property>    
    33.     </bean>    
    34.     
    35.     <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->    
    36.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
    37.         <property name="dataSource" ref="dataSource" />    
    38.         <!-- 自动扫描mapping.xml文件 -->    
    39.         <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>    
    40.     </bean>    
    41.         
    42. </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.     
    13.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->    
    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:

    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.     <!--避免IE执行AJAX时,返回JSON出现下载文件 -->    
    14.     <bean id="mappingJacksonHttpMessageConverter"    
    15.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
    16.         <property name="supportedMediaTypes">    
    17.             <list>    
    18.                 <value>text/html;charset=UTF-8</value>    
    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.     <!-- 完成请求和注解POJO的映射 -->    
    31.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">    
    32.         <property name="messageConverters">    
    33.             <list>    
    34.                 <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->    
    35.             </list>    
    36.         </property>    
    37.     </bean>    
    38.         
    39.     <!-- 定义跳转的文件的前后缀 ,视图模式配置-->    
    40.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
    41.         <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->    
    42.         <property name="prefix" value="/WEB-INF/jsp/" />    
    43.         <property name="suffix" value=".jsp" />    
    44.     </bean>    
    45.         
    46.     <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器-->    
    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

    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.         
    8.     <welcome-file-list>    
    9.         <welcome-file>/index.jsp</welcome-file>    
    10.     </welcome-file-list>    
    11.         
    12.     <!-- 加载spring bean -->    
    13.     <context-param>    
    14.          <param-name>contextConfigLocation</param-name>    
    15.         <param-value>classpath:applicationContext.xml</param-value>    
    16.     </context-param>    
    17.     <listener>    
    18.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
    19.     </listener>    
    20.         
    21.     <!-- 编码过滤器 -->    
    22.     <filter>    
    23.         <filter-name>encodingFilter</filter-name>    
    24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
    25.         <async-supported>true</async-supported>    
    26.         <init-param>    
    27.             <param-name>encoding</param-name>    
    28.             <param-value>UTF-8</param-value>    
    29.         </init-param>    
    30.     </filter>    
    31.     <filter-mapping>    
    32.         <filter-name>encodingFilter</filter-name>    
    33.         <url-pattern>/*</url-pattern>    
    34.     </filter-mapping>    
    35.         
    36.     <!-- Spring MVC servlet -->    
    37.     <servlet>    
    38.         <servlet-name>SpringMVC</servlet-name>    
    39.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
    40.         <init-param>    
    41.             <param-name>contextConfigLocation</param-name>    
    42.             <param-value>/WEB-INF/spring-mvc.xml</param-value>    
    43.         </init-param>    
    44.         <load-on-startup>1</load-on-startup>    
    45.         <async-supported>true</async-supported>    
    46.     </servlet>    
    47.     <servlet-mapping>    
    48.         <servlet-name>SpringMVC</servlet-name>    
    49.         <url-pattern>/</url-pattern>    
    50.     </servlet-mapping>    
    51.         
    52. </web-app>    

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

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

    UserController.java:

    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<User> 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:

    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<User> queryUserByBatch(Map<String,Object> params);    
    13.         
    14.     public void insertUser(User user);    
    15.         
    16.     public void insertUserByBatch(List<User> list);    
    17.         
    18.     public void deleteByPrimaryKey(Integer id);    
    19.         
    20.     public void delteUserByBatch(Map<String,Object> params);    
    21.         
    22.     public void updateByPrimaryKey(Integer id);    
    23.     
    24.     public List<User> getAllUser();    
    25.         
    26. }    

    User.java:

    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:

    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.     
    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:

    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<User> getAllUser();    
    16. }    


    UserServiceImpl.java:

    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<User> getAllUser() {    
    32.         return userDao.getAllUser();    
    33.     }    
    34.     
    35. }    


    接下来就是我们的静态页面了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.         
    11.     <title>My JSP 'userinfo.jsp' starting page</title>    
    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.     <link rel="stylesheet" type="text/css" href="styles.css">  
    20.     -->    
    21.     
    22.   </head>    
    23.       
    24.   <body>    
    25.     <form id="addUser" action="user/addUser" method="post">     
    26.         userName: <input id="name" name="name" /><br/>     
    27.         password: <input id="password" name="password" /><br/>    
    28.         <input type="submit" value="添加新用户"/>     
    29.     </form>    
    30.   </body>    
    31. </html>     

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

    到此,框架成

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

  • 相关阅读:
    编程题#2: 魔兽世界之二:装备
    程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
    【转】C++动态创建二维数组,二维数组指针
    HDU-2571命运
    HDU-1203 I NEED A OFFER!
    HDU-1003 Max Sum
    HDU2196-Computer
    HDU-1520 Anniversary party
    ChineseHelper(获取汉字字符串的首拼)
    车牌号正则表达式(新能源车牌)
  • 原文地址:https://www.cnblogs.com/DoubleEggs/p/6243216.html
Copyright © 2011-2022 走看看