Maven+SpringMVC+myBatis项目搭建(MyEclipse)


Maven+SpringMVC+myBatis项目搭建(MyEclipse)

  • Maven项目搭建
  • SpringMVC+MyBatis项目搭建

Maven项目搭建

在Myeclipse中,新建maven projectàweb的项目要选webapp如图à填写信息后fin

(注意:此时maven仓库连全球仓库,点击next后要下载webapp包,箭头处会出现进度条,如没有下载后面创建maven项目会报错)

 

即maven-web项目新建完成!

新建完成后项目只有src/main/resources目录,如想添加目录如图添加新目录

 

在pom.xml中Maven导入SpringMVC+MyBatis依赖包清单

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

   4.0.0

   com.sunyard.bigdata

   SunPMS

   war

   0.0.1-SNAPSHOT

   SunPMS Maven Webapp

   http://maven.apache.org

  

     

          junit

          junit

          3.8.1

          test

     

     

     

          org.springframework

          spring-web

          4.3.0.RELEASE

          compile

     

     

          org.springframework

          spring-aop

          4.3.0.RELEASE

          compile

     

     

          org.springframework

          spring-beans

          4.3.0.RELEASE

          compile

     

     

          org.springframework

          spring-context

          4.3.0.RELEASE

          compile

     

     

          org.springframework

          spring-core

          4.3.0.RELEASE

          compile

     

     

          org.springframework

          spring-expression

          4.3.0.RELEASE

          compile

     

     

          commons-logging

          commons-logging

          1.1.3

          compile

     

     

     

          org.springframework

          spring-tx

          4.3.0.RELEASE

     

     

     

          org.springframework

          spring-jdbc

          4.3.0.RELEASE

     

     

     

          aopalliance

          aopalliance

          1.0

     

     

     

          org.aspectj

          aspectjweaver

          1.8.5

     

  

     

          org.springframework

          spring-webmvc

          4.3.0.RELEASE

          compile

     

     

     

          mysql

          mysql-connector-java

          5.1.40

     

     

     

          org.mybatis

          mybatis

          3.4.1

     

     

     

          org.mybatis

          mybatis-spring

          1.3.0

     

     

     

          com.alibaba

          fastjson

          1.2.44

     

     

     

          javax.servlet

          javax.servlet-api

         3.1.0

          provided

     

  

 

  

      SunPMS

  

后续步骤如“Spring+MyBatis项目搭建”第3步以后

*如果不构建maven项目则直接执行“Spring+MyBatis项目搭建”步骤即可

 

SpringMVC+MyBatis项目搭建

1. 新建一个Web项目

2. 在lib下,添加Spring和myBatis的依赖包

3. 新建applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

"http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"

   xmlns:aop="http://www.springframework.org/schema/aop"

   xsi:schemaLocation=

        http://www.springframework.org/schema/aop  

        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 

        http://www.springframework.org/schema/mvc  

        http://www.springframework.org/schema/mvc/spring-mvc-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/tx  

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

        http://www.springframework.org/schema/context  

       http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

  

  

      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

      "location" value="classpath:database.properties">

  

  

  

   "dataSource"

      class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      "driverClassName" value="${supp.driverClassName}">

      "url" value="${supp.url}">

      "username" value="${supp.username}">

      "password" value="${supp.password}">

  

  

  

   "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      "dataSource" ref="dataSource">

      "configLocation" value="">

      "mapperLocations" value="classpath:com/interfacedesigner/mapping/*.xml">

  

  

  

   "org.mybatis.spring.mapper.MapperScannerConfigurer">

      "sqlSessionFactoryBeanName" value="sqlSessionFactory">

      "basePackage" value="com.interfacedesigner">

  

  

  

   "txManage"

      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

      "dataSource" ref="dataSource">

  

  

  

   "txAdvice" transaction-manager="txManage">

     

          "select" propagation="REQUIRED" />

          "update" propagation="REQUIRED" />

          "delete" propagation="REQUIRED" />

          "insert" propagation="REQUIRED" />

          "*" propagation="REQUIRED" />

     

  

  

      "execution(* com.interfacedesigner.*.*(..))"

          id="pointcut" />

      "txAdvice" pointcut-ref="pointcut" />

  

  

   "com.interfacedesigner">

4. 新建springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> 

"http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xmlns:context="http://www.springframework.org/schema/context" 

    xmlns:tx="http://www.springframework.org/schema/tx" 

    xmlns:mvc="http://www.springframework.org/schema/mvc" 

    xsi:schemaLocation="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-2.5.xsd 

           http://www.springframework.org/schema/tx  

           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 

           http://www.springframework.org/schema/mvc 

           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

            

        "com.interfacedesigner"> 

 

5. 新建database.properties文件

# my DB

supp.driverClassName=com.mysql.jdbc.Driver

supp.url=jdbc\:mysql\://127.0.0.1\:3306/test?allowMultiQueries\=true&useUnicode=true&characterEncoding=UTF-8

supp.username=root

supp.password=123456

 6. 修改web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>

"http://www.w3.org/2001/XMLSchema-instance"

   xmlns="http://java.sun.com/xml/ns/javaee"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

   id="WebApp_ID" version="3.0">

   ssmTest

 

  

  

      contextConfigLocation

      classpath:applicationContext.xml

  

  

      org.springframework.web.context.ContextLoaderListener

  

  

  

      springDispatcherServlet

      org.springframework.web.servlet.DispatcherServlet

     

          contextConfigLocation

          classpath:springmvc.xml

     

      1

  

 

  

  

      springDispatcherServlet

      /

  

 

  

      contextFilter

      com.interfacedesigner.filter.HttpFilter

  

  

      contextFilter

      /*

  

 

7. 完成项目中java类的搭建

*注意标签的使用