700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > spring mvc + mybatis 框架搭建 ( idea + gradle)

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

时间:2019-06-10 07:11:02

相关推荐

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

spring mvc + mybatis 框架搭建

idea + gradle

刚刚入门,只是个人见解,如有错误或者问题欢迎指出指正。

邮箱: [ wgh0807@ ]

文章引用:

[apache - log4j]

[mybatis 配置]

一、 build.gradle 加载相关包

在dependencies下配置

相关包的搜索请见[]

个人常用的包:

''// 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.xmljdbc.propertieslog4j.propertiesmybatis-config.xml (可以集成到application.xml中,非必须)application.xmlwebapp WEB-INF web.xml

web-servlet.xml

详细配置

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

mapper/*-mapper.xml

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

基本配置为

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

'' <!DOCTYPE mapper

'' PUBLIC "-////DTD Mapper 3.0//EN"

'' "/dtd/mybatis-3-mapper.dtd">

''

''

''

内容物包含5种常用标签:insert, delete, update, select 和 resultMap

前4种分别对应增删改查,第五种则是为了对象中一对多或多对多属性。

result map中又包含四个常用标签:id、result、association、collection

association用于一对一属性,collection用于一对多属性

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

mybatis-config.xml

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

简单配置

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

'' <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/mybatis-3-config.dtd">

''

''

''

''

''

application.xml

我个人认为这里替代了mybatis-config.xml配置文件,创建了数据库连接池

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

'' <beans xmlns="/schema/beans"

'' xmlns:xsi="/2001/XMLSchema-instance"

'' xmlns:context="/schema/context"

'' xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd"

'' <context:property-placeholder location="classpath:jdbc.properties"/

''

''

''

''

''

''

''

''

''

''

''

''

''

''

''

''

''

''

''

web.xml

webapp / WEB-INF / web.xml

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

'' <web-app xmlns="/xml/ns/javaee"

'' xmlns:xsi="/2001/XMLSchema-instance"

'' xsi:schemaLocation="/xml/ns/javaee /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

''

''

''

web-servlet.xml

webapp / WEB-INF / web-servlet.xml

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

'' <beans xmlns="/schema/beans"

'' xmlns:xsi="/2001/XMLSchema-instance" xmlns:mvc="/schema/mvc"

'' xmlns:context="/schema/context"

'' xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/mvc /schema/mvc/spring-mvc.xsd /schema/context /schema/context/spring-context.xsd">

''

''

''

''

''

''

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。