spring mvc + mybatis 框架搭建 ( idea + gradle)

spring mvc + mybatis 框架搭建

idea + gradle
刚刚入门,只是个人见解,如有错误或者问题欢迎指出指正。
邮箱: [ wgh0807@qq.com ]

文章引用:
[apache - log4j]
[mybatis 配置]

一、 build.gradle 加载相关包

在dependencies下配置
相关包的搜索请见[maven.org]
个人常用的包:
''// java EE api
'' compile 'javax:javaee-api:7.0'
''
'' //mysql connector
'' compile 'mysql:mysql-connector-java:5.1.47'
''
'' // spring 框架
'' compile 'org.springframework:spring-webmvc:5.1.3.RELEASE'
'' compile 'org.springframework:spring-jdbc:5.1.3.RELEASE'
'' compile 'org.springframework:spring-context-support:5.1.3.RELEASE'
''
'' // mybatis
'' compile 'org.mybatis:mybatis:3.4.6'
'' compile 'org.mybatis:mybatis-spring:1.3.2'
''
'' //其他
'' // json 转换
'' compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8'// json数据转换
'' compile 'jstl:jstl:1.2'//jsp标准标签库
'' compile 'org.jasypt:jasypt:1.9.2'//Strong Password encorypt
'' compile 'org.projectlombok:lombok:1.18.4'//精简class
'' compile 'org.slf4j:slf4j-log4j12:1.7.25' //日志记录
'' compile 'commons-codec:commons-codec:1.11'//编解码
'' compile 'commons-fileupload:commons-fileupload:1.3.2'//文件上
修改完后使用右下角弹窗中的import 或者auto import 都可以,会在网络环境下自动下载。

二、创建包结构

个人喜欢提前创建好包结构,而且喜欢在一个总的包下创建包

  • src
    • project
      • controller(控制器)
      • service(服务层)
      • dao(数据层)
      • obj(对象包,也可以使用pojo、model等)
      • util(其他工具类)

三、配置文件

目录结构

  • resources
    • mapper
      • xxxx-mapper.xml
    • jdbc.properties
    • log4j.properties
    • mybatis-config.xml (可以集成到application.xml中,非必须)
    • application.xml
  • webapp
    • WEB-INF
      • web.xml
      • web-servlet.xml

详细配置

  1. jdbc.properties

    jdbc.Url 中传入了三个参数,由于使用本地数据库访问,故没有使用ssl加密;其他两个为编码方式使用utf-8,否则可能会出现中文乱码(需要参照数据库编码方式,建库的时候也最好配置下。mac下mysql使用UTF8)
    配置如下
    ''jdbc_Driver = com.mysql.jdbc.Driver
    '' jdbc_Url = jdbc:mysql:///?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    '' jdbc_user = java
    '' jdbc_password = java

  2. mapper/*-mapper.xml

    为了第5项配置方便,才创建mapper文件夹,并命名为*-mapper.xml
    基本配置为
    ''
    ''
    ''
    ''
    ''

    内容物包含5种常用标签:insert, delete, update, select 和 resultMap
    前4种分别对应增删改查,第五种则是为了对象中一对多或多对多属性。
    result map中又包含四个常用标签:id、result、association、collection
    association用于一对一属性,collection用于一对多属性

  3. log4j.properties

    日志记录的配置文件
    官方有四种配置方式:xml、json、yaml 和 Properties
    我采用的是第四种配置,详细配置和其他方式配置请参考[apache官网]
    基本配置:
    ''# 1. rootLogger: all trace debug info warn error fatal off
    '' log4j.rootLogger = WARN, console
    '' log4j.logger.demo = WARN
    ''
    '' # 2. appender
    '' log4j.appender.console = org.apache.log4j.ConsoleAppender
    ''
    '' # 3. layout
    '' log4j.appender.console.layout = org.apache.log4j.PatternLayout
    '' log4j.appender.console.layout.ConversionPattern = %d\t%p\t%c{1}\t%m%n

  4. mybatis-config.xml

    此文件其实不需要,因为可以集成到application.xml中(个人理解)
    简单配置
    ''
    ''
    ''
    ''
    ''
    ''

    ''

  5. application.xml

    我个人认为这里替代了mybatis-config.xml配置文件,创建了数据库连接池
    ''
    '' <beans xmlns="http://www.springframework.org/schema/beans"
    '' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    '' 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.xsd"
    '' <context:property-placeholder location="classpath:jdbc.properties"/
    ''
    ''
    ''
    ''
    ''
    ''
    ''

    ''
    ''
    ''
    ''
    ''
    ''
    ''

    ''
    ''
    ''
    ''

    ''

  6. web.xml

    webapp / WEB-INF / web.xml
    ''
    '' <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    '' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    '' xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    '' version="4.0">
    ''
    ''
    '' encoding
    '' org.springframework.web.filter.CharacterEncodingFilter
    ''

    ''
    '' encoding
    '' /
    ''

    ''
    ''
    ''
    '' cors
    '' project.util.Cross_domain_access
    ''

    ''
    '' cors
    '' /

    ''
    ''
    ''
    ''
    '' web
    '' org.springframework.web.servlet.DispatcherServlet
    ''

    ''
    '' web
    '' /
    ''

    ''
    ''
    ''
    '' default
    '' .html
    ''
    .css
    '' .js
    ''
    .ico
    '' /static/*
    ''

    ''
    ''
    ''
    '' org.springframework.web.context.ContextLoaderListener
    ''

    ''
    '' org.springframework.web.context.request.RequestContextListener
    ''

    ''
    '' contextConfigLocation
    '' classpath:applicationContext.xml
    ''

    ''
    ''

  7. web-servlet.xml

    webapp / WEB-INF / web-servlet.xml
    ''
    '' <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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    '' mvc:annotation-driven/
    '' <context:component-scan base-package="project"/>
    ''
    ''
    ''

    ''
    以上就是基础环境的配置过程,谢谢观看。END

posted @ 2019-01-23 13:30  wgh0807  阅读(382)  评论(0编辑  收藏  举报