100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java项目整合mybatis_JavaWeb项目整合Spring SpringMVC Mybatis框架

java项目整合mybatis_JavaWeb项目整合Spring SpringMVC Mybatis框架

时间:2020-10-09 01:27:45

相关推荐

java项目整合mybatis_JavaWeb项目整合Spring SpringMVC Mybatis框架

衔接上篇:

版本信息

spring 4.4.13

mybatis 3.4.1

One Step!

根据所需,导入相应jar包,添加依赖。

//spring 系列包 4.4.13

// spring mvc

compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.13.RELEASE'

// spring 核心包

compile group: 'org.springframework', name: 'spring-core', version: '4.3.13.RELEASE'

// spring beans

compile group: 'org.springframework', name: 'spring-beans', version: '4.3.13.RELEASE'

// spring 上下文

compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'

// spring web

compile group: 'org.springframework', name: 'spring-web', version: '4.3.13.RELEASE'

// spring orm

compile group: 'org.springframework', name: 'spring-orm', version: '4.3.13.RELEASE'

// spring测试包

compile group: 'org.springframework', name: 'spring-test', version: '4.3.13.RELEASE'

// 日志记录 1.7.25

compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

// 数据库连接 5.1.38

compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'

// 数据库连接池 2.7.7

compile group: 'com.zaxxer', name: 'HikariCP', version: '2.7.7'

// spring与Mybatis整合 1.3.0

compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.0'

// mybatis 3.4.1

compile group: 'org.mybatis', name: 'mybatis', version: '3.4.1'

这里使用的连接池为HikariCP,没了解过的建议百度一下。

Two Step!

进行相关的基础配置

修改web.xml,如下图所示。

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

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

xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"

version="3.1">

index.jsp

SetCharacterEncoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

SetCharacterEncoding

/*

contextConfigLocation

classpath:/spring/SpringApplicationContext.xml

org.springframework.web.context.ContextLoaderListener

springMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/SpringMVC.xml

1

true

springMVC

/

在Resources目录下创建SpirngApplicationContext.xml,SpringMVC.xml,jdbc.properties,logback.xml配置文件。

SpirngApplicationContext.xml

xmlns:xsi="/2001/XMLSchema-instance" xmlns:context="/schema/context" xmlns:tx="/schema/tx" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd /schema/tx /schema/tx/spring-tx.xsd /schema/aop /schema/aop/spring-aop.xsd">

destroy-method="shutdown">

SpringMVC.xml

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

xmlns:context="/schema/context"

xmlns:mvc="/schema/mvc"

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

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false

jdbc.username=root

jdbc.password=123456

jdbc.readOnly=false

jdbc.connectionTimeout=30000

jdbc.idleTimeout=600000

jdbc.maxLifetime=1800000

jdbc.maximumPoolSize=60

jdbc.minimumIdle=10

logback.xml

%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n

添加完配置文件不要忘了Create new application context。

对于SpringMVC.xml也是如此。

到这里我们基本已经配置完成,所以剩下一步,我们只需要测试一下是否成功运行就可以了。

Three Step!

测试是否整合成功

运行Tomcat,没有出现404,说明我们成功了第一步

接下来,我测试一下是否能够根据id查找出数据库中的信息

页面并没有输出信息,结果出现了500错误。原因是缺少jar包。所以Gradle要添加以下依赖。

// 页面输出json数据所需jar包

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0'

compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0'

compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.0'

再次测试,成功运行输出内容。

此时我们的SSM框架基本整合成功!!

下一节!SSM项目添加swagger2支持

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