MyEclipse使用Maven创建web项目+搭建SSM框架

目录(?)[-]

  1. 安装maven和配置
  2. MyEclipse配置Maven
  3. MyEclipse使用maven创建web项目
  4. 搭建SpringSpringMVCMybatis框架
  5. 常见问题

使用maven已经有一段时间了,但项目是别人搭建好的,因此一直想着自己要学习搭建一下。网上找了些资料后,结合自己实验,花了点时间就搞好,老样子,写在博客上,免得日后忘记。


博文中的代码:链接:http://pan.baidu.com/s/1o8tqIMI 密码:m3si


1.安装maven和配置


1、下载maven.apache.org/download.html,下载apache-maven-3.1.1-bin.zip,**-src.zip为Maven的源码

2、配置环境变量,打开系统环境变量新建一个M2_HOME将解压后的Maven存放路径放在此,然后在path路径下加

入史%M2_HOME%\bin路径

3、检测是否配置成功,打开命令行输入echo %M2_HOME%查看M2_HOME指向的Maven存放安装目录是否正

确,输入mvn -v查看是否能找到正确的mvn执行脚本

4、以后升级的话将M2_HOME的路径换为最新的安装目录就行

5、Maven实践:初次安装完后输入mvn help:system的命令,此时会打印出所有的Java系统属性和环境变量,然后

Maven会执行一个真正的任务将相应的所需的构件下载到本地仓库中包括pom和jar文件,然后用户在

C:\Users\Administrator\.m2下就能看到本地仓库了

6、用户需要复制M2_HOME/conf/setting.xml文件到C:/Users/Administrator/.m2/setting.xml,这样用户每次升

级后就不要在次修改setting.xml文件了


2.MyEclipse配置Maven


1、在本地创建一个文件夹MavenRepository,并在MavenRepository文件夹下创建文件夹repo。

2、进入Maven解压后的文件夹,进入conf文件夹,将settings.xml文件复制到上一步创建的MavenRepository文件

夹下

3、打开MavenRepository文件夹下的settings.xml文件,找到localRepository标签,此时是被注释掉的,我们解除

注释,然后配置步骤1中的repo路径,如E:\MavenRepository\repo

4、在MyEclipse中的Perferences进行如下配置,添加自己的Maven



5、User Settings设置为之前修改过的setting.xml


6、File->New->others,搜索maven,如果看到有Maven Project则代表配置成功。



3.MyEclipse使用maven创建web项目


1、创建一个maven项目







2、按照上面教程,即可创建出一个Maven项目,项目结构如下图



3、将JRE版本设置为1.7,可使用默认的或者自己导入



4、此时index.jsp会报以下错误



在pom.xml文件的dependencies节点下添加如下代码即可,添加完后,会下载对应的jar包



5、右键项目,选择Properties,进行如下配置



4.搭建Spring+SpringMVC+Mybatis框架


1、Maven引入项目用到的jar包,修改pom.xml后保存会自动下载,存放在之前配置的本地仓库中,即:

E:\MavenRepository\repo文件夹

pom.xml
[html]  view plain  copy
 
  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.chillaxgroupId>  
  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
 
  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.chillax" />  
  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,有多人反映直接复制时,第一行的driver最后面多了个空格,请注意
[html]  view plain  copy
 
  1. driver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://127.0.0.1:3306/maventest  
  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
 
  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  
spring-dao.xml
[html]  view plain  copy
 
  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/chillax/mapper/*.xml">property>  
  43.     bean>  
  44.       
  45. beans>  
spring-tx.xml
[html]  view plain  copy
 
  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>  

3、在WEB-INF文件夹下添加/修改以下配置文件

spring-mvc.xml
[html]  view plain  copy
 
  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.chillax.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
 
  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>  

4、在src/main/java下添加如下包和类


UserController.java
[java]  view plain  copy
 
  1. package com.chillax.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.chillax.dto.User;  
  13. import com.chillax.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
 
  1. package com.chillax.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import com.chillax.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
 
  1. package com.chillax.dto;  
  2.   
  3. public class User {  
  4.     private Integer id;  
  5.   
  6.     private String name;  
  7.   
  8.     private String password;  
  9.   
  10.     private Integer age;  
  11.   
  12.     public Integer getId() {  
  13.         return id;  
  14.     }  
  15.   
  16.     public void setId(Integer id) {  
  17.         this.id = id;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return name;  
  22.     }  
  23.   
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.   
  28.     public String getPassword() {  
  29.         return password;  
  30.     }  
  31.   
  32.     public void setPassword(String password) {  
  33.         this.password = password == null ? null : password.trim();  
  34.     }  
  35.   
  36.     public Integer getAge() {  
  37.         return age;  
  38.     }  
  39.   
  40.     public void setAge(Integer age) {  
  41.         this.age = age;  
  42.     }  
  43. }  
UserMapper.xml
[html]  view plain  copy
 
  1. xml version="1.0" encoding="UTF-8" ?>  
  2. >  
  3. <mapper namespace="com.chillax.dao.IUserDao" >    
  4.     <resultMap id="BaseResultMap" type="com.chillax.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.chillax.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.chillax.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
 
  1. package com.chillax.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.chillax.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
 
  1. package com.chillax.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.chillax.dao.IUserDao;  
  10. import com.chillax.dto.User;  
  11. import com.chillax.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. }  


5、在WEB-INF文件夹下创建jsp文件夹,并添加userList.jsp,showUser.jsp、addUser.jsp

userList.jsp
[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. >  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'showUser.jsp' starting pagetitle>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.       
  21.   
  22.   head>  
  23.     
  24.   <body>  
  25.     <c:forEach items="${uList }" var="u">  
  26.         用户名称:${u.name}  
  27.         用户年龄:${u.age }  
  28.         <br/>  
  29.     c:forEach>  
  30.   body>  
  31. html>  

addUser.jsp
[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. >  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'addUser.jsp' starting pagetitle>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.       
  20.   
  21.   head>  
  22.     
  23.   <body>  
  24.     <form id="addUser" action="user/addUser" method="post">   
  25.         userName: <input id="name" name="name" /><br/>   
  26.         password: <input id="password" name="password" /><br/>   
  27.         age: <input id="age" name="age"/><br/>   
  28.         <input type="submit" value="添加新用户"/>   
  29.     form>  
  30.   body>  
  31. html>  

showUser.jsp
[html]  view plain  copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. >  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'showUser.jsp' starting pagetitle>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.       
  20.   
  21.   head>  
  22.     
  23.   <body>  
  24.     ${user.name }<br>  
  25.   body>  
  26. html>  



6、创建数据库,并创建表

[sql]  view plain  copy
 
  1. DROP TABLE IF EXISTS `user`;  
  2.   
  3. CREATE TABLE `user` (  
  4.   `id` int(12) NOT NULL AUTO_INCREMENT,  
  5.   `namevarchar(50) NOT NULL,  
  6.   `passwordvarchar(50) NOT NULL,  
  7.   `age` int(9) NOT NULL,  
  8.   PRIMARY KEY (`id`)  
  9. ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;  
  10.   
  11.   
  12. insert  into `user`(`id`,`name`,`password`,`age`) values (1,'admin','admin',22);  

7、整个项目文件结构图



8、部署发布项目,进行测试



至此,框架搭建完成

5.常见问题


问题1:pom.xml文件里的代码到哪里找?

解答:http://www.mvnrepository.com/


问题2:新建MAVEN项目过程中,出现下图错误?



解决:在MyEclipse的安装目录找到myeclipse.ini,将分配的内存加大



问题3:新建完工程后出现以下错误?



解答:在pom.xml添加以下代码即可,添加完代码就会自动下载好jar包




问题4:修改pom.xml时出现以下错误?




解答:这是由于jar包没有正确下载导致的,找到错误的jar包位置,将文件夹里的文件删掉,将pom.xml里该jar包的配置代码删除后再重新写上,让MAVEN重新下载jar包即可。



问题5:出现以下错误?



解答:



问题6:出现以下错误?



解答:将项目的jre版本换成1.7或者以上的版本即可

你可能感兴趣的:(Java-API,框架-Spring)