Maven创建web项目:SpringMVC+Mybatis

jopen 9年前

项目构建步骤

1、File->New->Project

勾选Create from archetype

Maven创建web项目:SpringMVC+Mybatis

点击Next

2、输入GroupId、ArtifactId

Maven创建web项目:SpringMVC+Mybatis

点击Next

Maven创建web项目:SpringMVC+Mybatis

3、继续点击Next,输入Project name

Maven创建web项目:SpringMVC+Mybatis

点击Finish,完成基本项目创建

Maven创建web项目:SpringMVC+Mybatis

4、在src/main下添加java目录作为源文件目录

在main上右键new Directory并命名为java;

同时在Project Structure中,将java目录设置为Sources,然后Apply,点击OK。

Maven创建web项目:SpringMVC+Mybatis

5、配置pom.xml

可以在maven仓库进行相关依赖搜索:http://www.mvnrepository.com/

利用<dependency>元素进行项目所依赖的jar包配置,maven通过对pom.xml的配置使得不再需要导入jar包那么麻烦

Maven创建web项目:SpringMVC+Mybatis

6、配置tomcat

Maven创建web项目:SpringMVC+Mybatis

Maven创建web项目:SpringMVC+Mybatis

Maven创建web项目:SpringMVC+Mybatis


MavenDemo简单实例

1、项目结构

Maven创建web项目:SpringMVC+Mybatis


注:  在WEB-INFO/lib下存放有jstl.jar和standard.jar,需要将其导入项目的Libraries中

如图:

Maven创建web项目:SpringMVC+Mybatis


2、pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">      <modelVersion>4.0.0</modelVersion>    <packaging>war</packaging>      <name>MavenDemo</name>    <groupId>MavenDemo</groupId>    <artifactId>MavenDemo</artifactId>    <version>1.0-SNAPSHOT</version>      <dependencies>        <!-- spring start-->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>          <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>4.1.6.RELEASE</version>        </dependency>        <!-- spring end-->          <!-- mybatis start-->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.2.8</version>        </dependency>          <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.2.2</version>        </dependency>        <!-- mybatis end-->          <!-- mysql connector start-->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.34</version>        </dependency>        <!-- mysql connector end-->          <!-- junit start-->        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>        </dependency>        <!-- junit end-->          <!-- 阿里巴巴数据源包 -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>druid</artifactId>            <version>1.0.2</version>        </dependency>    </dependencies>    </project>

3、web.xml

<?xml version="1.0" encoding="UTF-8"?>  <web-app version="2.4"           xmlns="http://java.sun.com/xml/ns/j2ee"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>        <listener>          <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>      </listener>        <servlet>          <servlet-name>dispatcher</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <load-on-startup>1</load-on-startup>      </servlet>            <servlet-mapping>          <servlet-name>dispatcher</servlet-name>          <url-pattern>/</url-pattern>      </servlet-mapping>        <welcome-file-list>          <welcome-file>/index.jsp</welcome-file>      </welcome-file-list>  </web-app>

4、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd                             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd                             ">      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">          <property name="driverClassName" value="com.mysql.jdbc.Driver" />          <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />          <property name="username" value="root" />          <property name="password" value="root" />      </bean>        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">          <property name="dataSource" ref="dataSource" />      </bean>        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">          <property name="basePackage" value="mapper" />      </bean>  </beans>

5、dispathcer-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xmlns:context="http://www.springframework.org/schema/context"         xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">        <mvc:default-servlet-handler />        <mvc:annotation-driven />        <context:component-scan base-package="controller" />      <context:component-scan base-package="mapper" />      <context:component-scan base-package="service" />        <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/jsp/" />          <property name="suffix" value=".jsp" />      </bean>  </beans>

6、model/User.java

package model;    /**   * Created by Roger on 2015/9/17.   */  public class User {      private int id;      private String name;        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }        public String getName() {          return name;      }        public void setName(String name) {          this.name = name;      }  }

7、mapper/UserMapper.java

package mapper;    import model.User;  import org.apache.ibatis.annotations.Result;  import org.apache.ibatis.annotations.Results;  import org.apache.ibatis.annotations.Select;  import org.springframework.stereotype.Repository;    import java.util.List;    /**   * Created by Roger on 2015/9/17.   */  @Repository("userMapper")  public interface UserMapper {      @Select("select * from user")      @Results(value = {@Result(id = true, property = "id", column = "id"),                  @Result(property = "name", column = "name")})      public List<User> selectAll();  }

8、service/impl/UserServiceImpl.java

package service.impl;    import mapper.UserMapper;  import model.User;  import org.springframework.stereotype.Service;  import service.UserService;    import javax.annotation.Resource;  import java.util.List;    /**   * Created by Roger on 2015/9/17.   */  @Service("userService")  public class UserServiceImpl implements UserService {      @Resource(name = "userMapper")      private UserMapper userMapper;        public List<User> selectAll() {          return userMapper.selectAll();      }  }

9、controller/UserController.java

package controller;    import org.springframework.stereotype.Controller;  import org.springframework.ui.Model;  import org.springframework.web.bind.annotation.RequestMapping;  import service.UserService;    import javax.annotation.Resource;    /**   * Created by Roger on 2015/9/17.   */  @Controller  public class UserController {      @Resource(name = "userService")      private UserService userService;        @RequestMapping("/getUser")      public String getUser(Model model) {          System.out.println(userService.selectAll().size());          model.addAttribute("user", userService.selectAll());          return "listUser";      }  }

10、WEB-INFO/jsp/listUser.jsp

<%--    Created by IntelliJ IDEA.    User: Roger    Date: 2015/9/17    Time: 11:01    To change this template use File | Settings | File Templates.  --%>  <%@ page contentType="text/html;charset=UTF-8" language="java" %>  <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>  <html>  <head>      <title>list user</title>  </head>  <body>      <c:forEach items="${user}" var="item">          ${item.id}--${item.name}          <br>      </c:forEach>  </body>  </html>

11、运行结果

通过getUser来访问数据库中user表格的数据

Maven创建web项目:SpringMVC+Mybatis

完成代码下载链接:http://pan.baidu.com/s/1qW01FmW

来自:http://my.oschina.net/u/1020238/blog/507521