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

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


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


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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第1张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第2张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第3张图片

3.MyEclipse使用maven创建web项目


1、创建一个maven项目

MyEclipse使用Maven创建web项目+搭建SSM框架教_第4张图片
MyEclipse使用Maven创建web项目+搭建SSM框架教_第5张图片

MyEclipse使用Maven创建web项目+搭建SSM框架教_第6张图片
MyEclipse使用Maven创建web项目+搭建SSM框架教_第7张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第8张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第9张图片

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



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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第10张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第11张图片

4.搭建Spring+SpringMVC+Mybatis框架


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

E:\MavenRepository\repo文件夹

pom.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.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.         <dependency>  
  96.             <groupId>org.springframeworkgroupId>  
  97.             <artifactId>spring-aspectsartifactId>  
  98.             <version>${srping.version}version>  
  99.         dependency>  
  100.           
  101.           
  102.         <dependency>  
  103.             <groupId>org.mybatisgroupId>  
  104.             <artifactId>mybatisartifactId>  
  105.             <version>${mybatis.version}version>  
  106.         dependency>  
  107.         <dependency>  
  108.             <groupId>org.mybatisgroupId>  
  109.             <artifactId>mybatis-springartifactId>  
  110.             <version>1.2.2version>  
  111.         dependency>  
  112.           
  113.           
  114.         <dependency>  
  115.             <groupId>mysqlgroupId>  
  116.             <artifactId>mysql-connector-javaartifactId>  
  117.             <version>5.1.35version>  
  118.         dependency>  
  119.           
  120.         <dependency>  
  121.             <groupId>commons-dbcpgroupId>  
  122.             <artifactId>commons-dbcpartifactId>  
  123.             <version>1.4version>  
  124.         dependency>  
  125.           
  126.         <dependency>  
  127.             <groupId>jstlgroupId>  
  128.             <artifactId>jstlartifactId>  
  129.             <version>1.2version>  
  130.         dependency>  
  131.         <dependency>  
  132.             <groupId>taglibsgroupId>  
  133.             <artifactId>standardartifactId>  
  134.             <version>1.1.2version>  
  135.         dependency>  
  136.           
  137.         <dependency>  
  138.             <groupId>log4jgroupId>  
  139.             <artifactId>log4jartifactId>  
  140.             <version>${log4j.version}version>  
  141.         dependency>  
  142.         <dependency>  
  143.             <groupId>org.slf4jgroupId>  
  144.             <artifactId>slf4j-apiartifactId>  
  145.             <version>${slf4j.version}version>  
  146.         dependency>  
  147.         <dependency>  
  148.             <groupId>org.slf4jgroupId>  
  149.             <artifactId>slf4j-log4j12artifactId>  
  150.             <version>${slf4j.version}version>  
  151.         dependency>  
  152.           
  153.           
  154.           
  155.         <dependency>  
  156.             <groupId>com.alibabagroupId>  
  157.             <artifactId>fastjsonartifactId>  
  158.             <version>1.2.6version>  
  159.         dependency>  
  160.         <dependency>  
  161.             <groupId>org.codehaus.jacksongroupId>  
  162.             <artifactId>jackson-mapper-aslartifactId>  
  163.             <version>1.9.13version>  
  164.         dependency>  
  165.           
  166.         <dependency>  
  167.             <groupId>commons-fileuploadgroupId>  
  168.             <artifactId>commons-fileuploadartifactId>  
  169.             <version>1.3.1version>  
  170.         dependency>  
  171.         <dependency>  
  172.             <groupId>commons-iogroupId>  
  173.             <artifactId>commons-ioartifactId>  
  174.             <version>2.4version>  
  175.         dependency>  
  176.         <dependency>  
  177.             <groupId>commons-codecgroupId>  
  178.             <artifactId>commons-codecartifactId>  
  179.             <version>1.10version>  
  180.         dependency>  
  181.           
  182.   
  183.           
  184.         <dependency>  
  185.             <groupId>net.sourceforge.jexcelapigroupId>  
  186.             <artifactId>jxlartifactId>  
  187.             <version>2.6version>  
  188.         dependency>  
  189.         <dependency>  
  190.             <groupId>org.apache.poigroupId>  
  191.             <artifactId>poiartifactId>  
  192.             <version>3.8version>  
  193.         dependency>  
  194.         <dependency>  
  195.             <groupId>org.apache.poigroupId>  
  196.             <artifactId>poi-ooxmlartifactId>  
  197.             <version>3.9version>  
  198.         dependency>  
  199.           
  200.     dependencies>  
  201.   
  202.     <build>  
  203.         <finalName>Maven_ProjectfinalName>  
  204.         <plugins>  
  205.             <plugin>  
  206.                 <groupId>org.apache.maven.pluginsgroupId>  
  207.                 <artifactId>maven-compiler-pluginartifactId>  
  208.                 <version>2.3.2version>  
  209.                 <configuration>  
  210.                     <source>1.7source>  
  211.                     <target>1.7target>  
  212.                 configuration>  
  213.             plugin>  
  214.         plugins>  
  215.     build>  
  216. project>  

	4.0.0
	com.chillax
	Maven_Project
	war
	0.0.1-SNAPSHOT
	Maven_Project Maven Webapp
	http://maven.apache.org

	
	
		4.0.2.RELEASE
		3.2.8
		1.7.12
		1.2.17
	
	
	
		
		
			junit
			junit
			4.11
			
			test
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			org.springframework
			spring-test
			${srping.version}
		
		
			org.springframework
			spring-core
			${srping.version}
		
		
			org.springframework
			spring-oxm
			${srping.version}
		
		
			org.springframework
			spring-tx
			${srping.version}
		
		
			org.springframework
			spring-jdbc
			${srping.version}
		
		
			org.springframework
			spring-aop
			${srping.version}
		
		
			org.springframework
			spring-context
			${srping.version}
		
		
			org.springframework
			spring-context-support
			${srping.version}
		
		
			org.springframework
			spring-expression
			${srping.version}
		
		
			org.springframework
			spring-orm
			${srping.version}
		
		
			org.springframework
			spring-web
			${srping.version}
		
		
			org.springframework
			spring-webmvc
			${srping.version}
		
		
			org.springframework
			spring-aspects
			${srping.version}
		
		
		
		
			org.mybatis
			mybatis
			${mybatis.version}
		
		
			org.mybatis
			mybatis-spring
			1.2.2
		
		
		
		
			mysql
			mysql-connector-java
			5.1.35
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			jstl
			jstl
			1.2
		
		
			taglibs
			standard
			1.1.2
		
		
		
			log4j
			log4j
			${log4j.version}
		
		
			org.slf4j
			slf4j-api
			${slf4j.version}
		
		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		
		
		
		
		
			com.alibaba
			fastjson
			1.2.6
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
		
			commons-fileupload
			commons-fileupload
			1.3.1
		
		
			commons-io
			commons-io
			2.4
		
		
			commons-codec
			commons-codec
			1.10
		
		

		
		
			net.sourceforge.jexcelapi
			jxl
			2.6
		
		
			org.apache.poi
			poi
			3.8
		
		
			org.apache.poi
			poi-ooxml
			3.9
		
		
	

	
		Maven_Project
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.3.2
				
					1.7
					1.7
				
			
		
	



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.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
print ?
  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  
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/maventest
username=root
password=root
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
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  
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
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.          spring-db.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="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.       
  36.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  37.         <property name="dataSource" ref="dataSource" />  
  38.           
  39.         <property name="mapperLocations" value="classpath:com/chillax/mapper/*.xml">property>  
  40.     bean>  
  41.       
  42. beans>  



	
	
		
	
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

	
	
		
		
		
	
	
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:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  9.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  10.                         http://www.springframework.org/schema/context    
  11.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  12.                         http://www.springframework.org/schema/aop   
  13.                         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  14.                         http://www.springframework.org/schema/tx  
  15.                         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
  16.   
  17.       
  18.     <bean id="transactionManager"  
  19.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  20.         <property name="dataSource" ref="dataSource" />  
  21.     bean>  
  22.       
  23.        
  24.      <aop:config>  
  25.          <aop:pointcut id="allServiceMethod" expression="execution(* com.chillax.service.*.*(..))"/>  
  26.          <aop:advisor pointcut-ref="allServiceMethod" advice-ref="TxAdvice" />  
  27.      aop:config>  
  28.       
  29.       
  30.     <tx:advice id="TxAdvice" transaction-manager="transactionManager">  
  31.          <tx:attributes>  
  32.                <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>  
  33.          tx:attributes>  
  34.     tx:advice>  
  35.       
  36. beans>  



	
	
		
	
	
	 
	 
		 
		 
	 
	
	
	
	     
		       
	     
 	
 	


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

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


                        
	
	
		
			
				text/html;charset=UTF-8
			
		
	
	
	   
    
    
    
    
	
	
	
	
		
			
					
			
		
	
	
	
	
		
		
		
	
	
	
	  
        
          
        
          
        
          
     

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>  


	Archetype Created Web Application
	
	
		/index.jsp
	
	
	
    
         contextConfigLocation
        classpath:applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		true
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	
	
	
	
		SpringMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/spring-mvc.xml
		
		1
		true
	
	
		SpringMVC
		/
	
	


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


UserController.java
[java] view plain copy
print ?
  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. }  
package com.chillax.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chillax.dto.User;
import com.chillax.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	
	@RequestMapping("/userList")
	public String userList(HttpServletRequest request,Model model){
		List uList = userService.getAllUser();
		model.addAttribute("uList", uList);
		return "userList";
	}
	
	@RequestMapping("/showUser")
	public String showUser(HttpServletRequest request,Model model){
		int userId = Integer.parseInt(request.getParameter("id"));
		User user = userService.getUserById(userId);
		model.addAttribute("user", user);
		return "showUser";
	}
	
	@RequestMapping("/addUserUI")
	public String addUserUI(){
		return "addUser";
	}
	
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request,Model model){
		User user = new User();
		user.setName(String.valueOf(request.getParameter("name")));
		user.setPassword(String.valueOf(request.getParameter("password")));
		user.setAge(Integer.parseInt(String.valueOf(request.getParameter("age"))));
		userService.addUser(user);
		return "redirect:/user/userList";
	}
}

IUserDao.java
[java] view plain copy
print ?
  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. }  
package com.chillax.dao;

import java.util.List;
import java.util.Map;

import com.chillax.dto.User;

public interface IUserDao {
	
	public User queryByPrimaryKey(Integer id);
	
	public List queryUserByBatch(Map params);
	
	public void insertUser(User user);
	
	public void insertUserByBatch(List list);
	
	public void deleteByPrimaryKey(Integer id);
	
	public void delteUserByBatch(Map params);
	
	public void updateByPrimaryKey(Integer id);

	public List getAllUser();
	
}

User.java
[java] view plain copy
print ?
  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. }  
package com.chillax.dto;

public class User {
    private Integer id;

    private String name;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
UserMapper.xml
[html] view plain copy
print ?
  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>  


  
    
		
		
		
		
	

	
		id, name, password, age
	

	
	
	
	
	
	
	
	
	
	
	
		insert into user  
		  
		    
		    id,  
		    
		    
		    name,  
		    
		    
		    password,  
		    
		    
		    age,  
		    
		  
		  
		    
		    #{id,jdbcType=INTEGER},  
		    
		    
		    #{name,jdbcType=VARCHAR},  
		    
		    
		    #{password,jdbcType=VARCHAR},  
		    
		    
		    #{age,jdbcType=INTEGER},  
		    
		   
	
	
	
	
    insert into USER
       (ID,
        NAME,
        PASSWORD,
        AGE)
    select A.* from
         (
         
             select 
             #{user.id,jdbcType=INTEGER},
             #{user.name,jdbcType=VARCHAR},
             #{user.password,jdbcType=VARCHAR},
             #{user.age,jdbcType=INTEGER}
              from dual
         
         ) A
  	

	
	
		delete from user
		where id = #{id,jdbcType=INTEGER}  
  	
  	
  	
	
	 delete from user
	 where id IN 
	     
	         #{ids,jdbcType=DECIMAL}
	     
	

	
	  
	    update user  
		  
		    
		    name = #{name,jdbcType=VARCHAR},  
		    
		    
		    password = #{password,jdbcType=VARCHAR},  
		    
		    
		    age = #{age,jdbcType=INTEGER},  
		    
		  
		where id = #{id,jdbcType=INTEGER} 
  	
  

IUserService.java
[java] view plain copy
print ?
  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. }  
package com.chillax.service;

import java.util.List;

import com.chillax.dto.User;

public interface IUserService {
	
	public User getUserById(int userId);

	public void insertUser(User user);

	public void addUser(User user);

	public List getAllUser();
}

UserServiceImpl.java
[java] view plain copy
print ?
  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. }  
package com.chillax.service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chillax.dao.IUserDao;
import com.chillax.dto.User;
import com.chillax.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService {
	@Resource
	private IUserDao userDao;
	
	public User getUserById(int userId) {
		return userDao.queryByPrimaryKey(userId);
	}

	public void insertUser(User user) {
		userDao.insertUser(user);
	}

	public void addUser(User user) {
		userDao.insertUser(user);
	}

	@Override
	public List getAllUser() {
		return userDao.getAllUser();
	}

}


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

userList.jsp
[html] view plain copy
print ?
  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>  
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'showUser.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    
    	用户名称:${u.name}
    	用户年龄:${u.age }
    	

addUser.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.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>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'addUser.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    
userName:
password:
age:

showUser.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.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>  
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'showUser.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    ${user.name }



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

[sql] view plain copy
print ?
  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);  
DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `age` int(9) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;


insert  into `user`(`id`,`name`,`password`,`age`) values (1,'admin','admin',22);

7、整个项目文件结构图

MyEclipse使用Maven创建web项目+搭建SSM框架教_第12张图片MyEclipse使用Maven创建web项目+搭建SSM框架教_第13张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第14张图片MyEclipse使用Maven创建web项目+搭建SSM框架教_第15张图片MyEclipse使用Maven创建web项目+搭建SSM框架教_第16张图片

至此,框架搭建完成

5.常见问题


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

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


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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第17张图片

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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第18张图片

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



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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第19张图片


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




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

MyEclipse使用Maven创建web项目+搭建SSM框架教_第20张图片

问题5:出现以下错误?



解答:

MyEclipse使用Maven创建web项目+搭建SSM框架教_第21张图片

问题6:出现以下错误?



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

问题7:出现以下错误,并且已经确定jre是1.7版本或者以上版本。
MyEclipse使用Maven创建web项目+搭建SSM框架教_第22张图片

解答: 在pom.xml文件的的标签中加入以下代码,然后Update Project(参照问题5
[html] view plain copy
print ?
  1. <plugins>  
  2.     <plugin>  
  3.         <groupId>org.apache.maven.pluginsgroupId>  
  4.         <artifactId>maven-compiler-pluginartifactId>  
  5.         <version>2.3.2version>  
  6.         <configuration>  
  7.             <source>1.7source>  
  8.             <target>1.7target>  
  9.         configuration>  
  10.     plugin>  
  11. plugins>  

	
		org.apache.maven.plugins
		maven-compiler-plugin
		2.3.2
		
			1.7
			1.7
		
	
转载地址:http://blog.csdn.net/v123411739/article/details/50742885/

你可能感兴趣的:(Maven,SSM)