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="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_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配置文件。

225b0e09508b

SpirngApplicationContext.xml

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

destroy-method="shutdown">

SpringMVC.xml

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

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

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

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 http://www.springframework.org/schema/mvc http://www.springframework.org/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也是如此。

225b0e09508b

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

Three Step!

测试是否整合成功

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

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

225b0e09508b

页面并没有输出信息,结果出现了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'

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

225b0e09508b

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值