maven+springMVC+mybatis+junit详细搭建过程

摘要  在做javaweb的过程中,搭建框架式比较头疼事情了,每次重新去搭建太浪费时间了,现在把框架搭建完成,可以在以后的项目中运用,节约开发成本。
maven  springMVC  mybatis  log4j  javaweb项目框架搭建

目录[-]

  • springMVC+mybatis框架搭建
  • 1.  工程目录结构整理清楚
  • 2.  引入依赖包
  • 3. 配置数据库连接属性
  • 4.  配置spring配置文件
  • 5.  java代码编写(model,dao,service层代码)
  • 6.  mybatis配置
  • 7.  junit测试插入功能
  • 8.  springMVC模块搭建 
  • 9.  log4j日志记录搭建
  • 10.  测试运行
  • 整体包下载地址:
  • springMVC+mybatis框架搭建

    在上一遍博客中以及讲诉了新建maven项目的流程,现在紧跟上一遍文章,接着搭建spring项目

    首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什么样的效果,自己的功能有哪些?

    (假设效果:项目目录结构清晰,能够查询到本地数据库中的内容。。)

    1.  工程目录结构整理清楚

    在src/main/java文件夹中,新建包cn.springmvc.model(存放javabean),

                                                cn.springmvc.dao(存放spring与mybatis连接接口),

                                                cn.springmvc.service(service接口),

                                                cn.springmvc.service.impl(service接口的实现),

                                                cn.springmvc.controller(存放控制层controller)

    在src/main/resource文件夹中,新建包conf(存放配置文件),

                                                       mapper(mybatis的mapper文件)

    在src/test/java文件夹中,新建包cn.springmvc.test(存放测试文件)

    在WEB-INF文件夹下新建jsp文件夹(存放jsp文件)

    这样项目结构基本完成了

    2.  引入依赖包

    打开maven的pom文件,对本次开发所需使用的架包依次导入(maven项目管理的优势)

    查找依赖结构有个不错的网站,http://search.maven.org/   只要输入包名即可查找引来关系

    pom.xml(包依赖)


    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    < 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 >eyas.springmvc groupId >
         < artifactId >springmvc artifactId >
         < packaging >war packaging >
         < version >0.0.1-SNAPSHOT version >
         < name >springmvc Maven Webapp name >
         < url >http://maven.apache.org url >
         < properties >
            
             < spring.version >3.2.4.RELEASE spring.version >
            
             < mybatis.version >3.2.4 mybatis.version >
            
             < slf4j.version >1.6.6 slf4j.version >
             < log4j.version >1.2.9 log4j.version >
         properties >
         < dependencies >
            
            
             < 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 >mysql groupId >
                 < artifactId >mysql-connector-java artifactId >
                 < version >5.1.29 version >
             dependency >
            
             < dependency >
                 < groupId >junit groupId >
                 < artifactId >junit artifactId >
                 < version >4.11 version >
                 < scope >test scope >
             dependency >
            
             < dependency >
                 < groupId >com.alibaba groupId >
                 < artifactId >druid artifactId >
                 < version >1.0.2 version >
             dependency >
     
            
             < dependency >
                 < groupId >org.codehaus.jackson groupId >
                 < artifactId >jackson-mapper-asl artifactId >
                 < version >1.9.13 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 >
            
         dependencies >
         < build >
             < finalName >springmvc finalName >
         build >
    project >


    3. 配置数据库连接属性

    conf/ jdbc.properties(jdbc配置文件)

    ?
    1
    2
    3
    4
    jdbc_driverClassName=com.mysql.jdbc.Driver
    jdbc_url=jdbc:mysql: //localhost:3306/mydays?useUnicode=true&characterEncoding=utf-8
    jdbc_username=root
    jdbc_password=root

    4.  配置spring配置文件

        conf/spring.xml(spring配置文件的扫描)


    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    xml version = "1.0" encoding = "UTF-8" ?>
    < beans xmlns = "http://www.springframework.org/schema/beans"
      xmlns:context = "http://www.springframework.org/schema/context"
      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-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
     
        
         < context:property-placeholder location = "classpath:conf/jdbc.properties" />
         
        
         < context:component-scan base-package = "cn.springmvc.service" />
    beans >
      conf/spring-mybatis.xml(spring与mybatis连接属性)
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    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:p = "http://www.springframework.org/schema/p"
       xmlns:context = "http://www.springframework.org/schema/context"
       xmlns:aop = "http://www.springframework.org/schema/aop"
       xmlns:tx = "http://www.springframework.org/schema/tx"
       xmlns:util = "http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.2.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
         http://www.springframework.org/schema/util
         http://www.springframework.org/schema/util/spring-util-3.2.xsd">
     
         < bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init"
         destroy-method = "close" >
         < property name = "driverClassName" >
           < value >${jdbc_driverClassName} value >
         property >
         < property name = "url" >
           < value >${jdbc_url} value >
         property >
         < property name = "username" >
           < value >${jdbc_username} value >
         property >
         < property name = "password" >
           < value >${jdbc_password} value >
         property >
        
         < property name = "maxActive" >
           < value >20 value >
         property >
        
         < property name = "initialSize" >
           < value >1 value >
         property >
        
         < property name = "maxWait" >
           < value >60000 value >
         property >
        
         < property name = "maxIdle" >
           < value >20 value >
         property >
        
         < property name = "minIdle" >
           < value >3 value >
         property >
        
         < property name = "removeAbandoned" >
           < value >true value >
         property >
        
         < property name = "removeAbandonedTimeout" >
           < value >180 value >
         property >
        
         < property name = "connectionProperties" >
           < value >clientEncoding=UTF-8 value >
         property >
       bean >
         
        
           < bean id = "sqlSessionFactory"
               class = "org.mybatis.spring.SqlSessionFactoryBean"
               p:dataSource-ref = "dataSource"
               p:configLocation = "classpath:conf/mybatis-config.xml"
               p:mapperLocations = "classpath:mapper/*.xml" />
           
       

    你可能感兴趣的:(Spring,MVC,MyBatis,maven)