eclipse maven 構建 spring mvc+mybatis

//首先最重要的是引入spring mvc+mybatis所須要的依賴jar才能夠配置項目,因此先粘出pom.xml 直接粘貼便可
//pom.xml
<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>
  <groupId>com.liyi</groupId>
  <artifactId>system-spring</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>system-spring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
   <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <springversion>3.0.5.RELEASE</springversion>  
        <junitversion>3.8.1</junitversion>  
    </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junitversion}</version>
      <scope>test</scope>
    </dependency>
    <!-- spring mvc 開始 -->
    <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-asm</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aspects</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-expression</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jms</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-oxm</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>${springversion}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
  
        <dependency>  
            <groupId>commons-collections</groupId>  
            <artifactId>commons-collections</artifactId>  
            <version>3.1</version>  
        </dependency>  
  
        <dependency>  
            <groupId>commons-logging</groupId>  
            <artifactId>commons-logging</artifactId>  
            <version>1.1</version>  
        </dependency>  
        
         <!-- spring mvc 結束-->
         <!-- mybatis 開始-->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>3.2.1</version>  
        </dependency>  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.2.0</version>  
        </dependency>  
         <!-- mybatis 結束-->  
         
         <!-- 數據庫鏈接 開始-->
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.26</version>  
        </dependency>  
         <!-- 數據庫鏈接 結束-->
         
         <!-- Servlet start -->  
        <!-- <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>servlet-api</artifactId>  
            <version>2.5</version>  
            <scope>provided</scope>  
        </dependency> -->  
        <dependency>  
            <groupId>javax.servlet.jsp</groupId>  
            <artifactId>jsp-api</artifactId>  
            <version>2.1</version>  
            <scope>provided</scope>  
        </dependency>  
        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency> 
        <!-- Servlet end -->  
        
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>20030825.184428</version>
        </dependency>
        
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>20030825.183949</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>system-spring</finalName>
           <plugins>    
             <plugin>    
                <artifactId>maven-compiler-plugin</artifactId>    
                <version>2.0.2</version>    
                <configuration>    
                    <source>1.6</source>    
                    <target>1.6</target>    
                </configuration>    
           </plugin>  
         </plugins>  
  </build>
</project>
//其次最重要的是項目的入口 web.xml 配置spring mvc的核心 DispatcherServlet,由於沒有指定配置文件的路徑,他
//先會去webapps-->web-inf下面找 ,他會默認去web-inf 的jsp文件夾下找相應的jsp
<?xml version="1.0" encoding="UTF-8"?>  
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        id="WebApp_ID" version="2.3"  
        xsi:schemaLocation="   http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_3.xsd">  
        <display-name>Archetype Created Web Application</display-name>  
      
        <servlet>  
            <servlet-name>spring</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
            <load-on-startup>1</load-on-startup>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>spring</servlet-name>  
            <url-pattern>*.do</url-pattern>  
        </servlet-mapping>  
    </web-app>  
//下面 就要貼出spring 的核心配置文件 即spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
       xmlns:aop="http://www.springframework.org/schema/aop"   
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd   
        http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  
    <mvc:annotation-driven />  
    <context:component-scan base-package="com.liyi.*" />  
    <!-- 引入jdbc配置文件 -->  
    <context:property-placeholder location="/WEB-INF/jdbc.properties" />  
     <!--建立jdbc數據源 -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
    </bean>  
  
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <!-- 建立SqlSessionFactory,同時指定數據源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
     <!-- Mapper接口所在包名,Spring會自動查找其下的Mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.liyi.dao" />  
    </bean>  
  
     <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
  
</beans>  
//jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456

//在src/main/java下面建立一個包命名爲com.liyi,由於掃描包配置的是此路徑 因此配這個包便可,建立一個類
//用代碼生成器生成mybatis的代碼模板 dao、dao.xml、service、impl、contorller、po、vo等
package com.liyi;

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

@Controller
@RequestMapping("/hello")
public class HelloController {
    @Resource
    private UserService userService;
    
    @RequestMapping(value="/test.do")
    public ModelAndView test(){
        ModelMap model = new ModelMap();
        List<UserPO> list = userService.findAll();
        model.addAttribute("users",list);
        ModelAndView modelAndView = new ModelAndView("hello",model);
        return modelAndView;
    }
}
//訪問http://localhost:8080/system-spring/hello/test.do就會跳到hello.jsp
相關文章
相關標籤/搜索