你的位置:首页 > Java教程

[Java教程]Maven下的springMvc mybatis


从头开始采用maven管理,spring 、ibatis、tomcat。

在配置过程中sqlserver 的jar老是加载不了,解决方案参考前一篇博文。

eclipse中已经自带了maven的插件所以不需要再另外下载maven,使用maven的好处就是方便jar的管理。配置上

groupId artifactId version 等属性省去自己下载jar 的烦恼。tomcat 也不需要自己去下载,在maven中配置好即可。

maven下载jar包,需要一段时间需耐心等待。 

pom.

<project ="http://maven.apache.org/POM/4.0.0" ="http://www.w3.org/2001/  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>test</groupId>  <artifactId>example</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>example Maven Webapp</name>  <url>http://maven.apache.org</url>  <properties>    <spring.version>4.2.0.RELEASE</spring.version>    <mybatis.version>3.2.8</mybatis.version>    <slf4j.version>1.6.6</slf4j.version>    <log4j.version>1.2.9</log4j.version>  </properties>  <dependencies>    <dependency>      <groupId>com.microsoft.sqlserver</groupId>      <artifactId>sqljdbc4</artifactId>      <version>4.0</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-core</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-web</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-oxm</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-tx</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-jdbc</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-aop</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context-support</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-aop</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-test</artifactId>      <version>${spring.version}</version>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis</artifactId>      <version>${mybatis.version}</version>    </dependency>    <dependency>      <groupId>org.mybatis</groupId>      <artifactId>mybatis-spring</artifactId>      <version>1.2.2</version>    </dependency>    <dependency>      <groupId>org.codehaus.jackson</groupId>      <artifactId>jackson-mapper-asl</artifactId>      <version>1.9.13</version>    </dependency>    <dependency>      <groupId>com.faster</groupId>      <artifactId>jackson-core</artifactId>      <version>2.1.0</version>    </dependency>    <dependency>      <groupId>com.faster</groupId>      <artifactId>jackson-databind</artifactId>      <version>2.1.0</version>    </dependency>    <dependency>      <groupId>com.faster</groupId>      <artifactId>jackson-annotations</artifactId>      <version>2.1.0</version>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>druid</artifactId>      <version>1.0.2</version>    </dependency>    <dependency>      <groupId>log4j</groupId>      <artifactId>log4j</artifactId>      <version>${log4j.version}</version>    </dependency>    <dependency>      <groupId>org.slf4j</groupId>      <artifactId>slf4j-api</artifactId>      <version>${slf4j.version}</version>    </dependency>    <dependency>      <groupId>org.slf4j</groupId>      <artifactId>slf4j-log4j12</artifactId>      <version>${slf4j.version}</version>    </dependency>    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>      <version>3.0.1</version>      <scope>provided</scope>    </dependency>  </dependencies>  <build>    <finalName>example</finalName>    <pluginManagement>      <plugins>        <!--This plugin's configuration is used to store Eclipse m2e settings           only. It has no influence on the Maven build itself. -->        <plugin>          <groupId>org.eclipse.m2e</groupId>          <artifactId>lifecycle-mapping</artifactId>          <version>1.0.0</version>          <configuration>            <lifecycleMappingMetadata>              <pluginExecutions>                <pluginExecution>                  <pluginExecutionFilter>                    <groupId>                      org.apache.maven.plugins                    </groupId>                    <artifactId>                      maven-compiler-plugin                    </artifactId>                    <versionRange>[3.1,)</versionRange>                    <goals>                      <goal>testCompile</goal>                    </goals>                  </pluginExecutionFilter>                  <action>                    <ignore></ignore>                  </action>                </pluginExecution>              </pluginExecutions>            </lifecycleMappingMetadata>          </configuration>        </plugin>        <plugin>          <groupId>org.apache.tomcat.maven</groupId>          <artifactId>tomcat7-maven-plugin</artifactId>          <version>2.1</version>        </plugin>      </plugins>    </pluginManagement>  </build></project>

 数据库的文件

Microsoft SQL Server 2008

USE [tonyDb]GO/****** Object: Table [dbo].[User]  Script Date: 08/22/2015 23:08:12 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[User](	[UserID] [int] IDENTITY(1,1) NOT NULL,	[UserName] [varchar](50) NULL,	[PassWord] [varchar](50) NULL, CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED (	[UserID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO

 首先看一个新增报错的配置文件中有UserID,不过未给该字段设值。

 

新增

@RequestMapping(value = "/insertUser", method = RequestMethod.POST)	public @ResponseBody String insertUser(HttpServletRequest request, HttpServletResponse response) {		try {			User u = new User();			u.setUserName((String) request.getAttribute("userName"));			u.setPassWord((String) request.getAttribute("passWord"));			userService.insertUser(u);			return "success";		} catch (Exception e) {			logger.error(e);		}		return "error|Exception ";	}

  

 

保存成功的示例

 

 

 

整个项目目录如下:

 

项目中到的url分享如下:

maven仓库

eclipse

spring 

附加eclipse中的git使用

工程下载路径