Spring整合SpringMVC + Mybatis基础框架的配置文件详解

网友投稿 193 2023-02-05

Spring整合SpringMVC + Mybatis基础框架的配置文件详解

前言

新建一个普通的Maven项目

基本目录结构

├── src #

│ ├── main #

│ │ └── java # java代码目录

│ │ └── resources # 配置文件目录, 存放下面Spring配置文件

│ ├── test # 单元测试目录

├── web # web目录

│ └── WEB-INF # web.xml 配置文件目录

1. Mybatis层编写

1、在 resources 目录下新建数据库配置文件 database.properties

jdbc.driver=com.mysql.jdbc.Driver

# 如果是使用 MySQL8.0+ 那么还需要增加一个时区的配置; serverTimezone=Asia/Shanghai

jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password=123456

2、在 resources 目录下创建Mybatis配置文件 mybatis-config.xml

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

2. Spring层编写

1. Spring整合Mybatis

配置Spring整合MyBatis,这里数据源使用c3p0连接池;

编写Spring整合Mybatis的相关的配置文件;在 resources 目录下创建 spring-dao.xml

注意:这里要引入上面Mybatis层的两个配置文件,配置文件的名称不要写错

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

2. Spring整合service

将业务层的类注入到Spring中,在 resources 目录下创建 spring-service.xml

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

xmlns:context="http://springfraPpWRekmework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

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

xmlns:context="http://springfraPpWRekmework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd">

3. SpringMVC层编写

1. 编写web.xml

修改 WEB-INF 下的 web.xml 文件

这里引入Spring整合的配置文件 applicationContext.xml

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

xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"

version="5.0">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

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

xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"

version="5.0">

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

springmvc

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encodingFilter

/*

15

2. 编写spring-mvc.xml

在 resources 目录下创建 spring-mvc.xml

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd">

4. Spring配置整合文件,applicationContext.xml

在 resources 目录下创建 applicationContext.xml

这里引入上面三个配置文件 spring-dao.xml、spring-service.xml、spring-mvc.xml 整合成一个总的配置文件

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

依赖

org.projectlombok

lombok

1.18.10

junit

junit

4.13

mysql

mysql-connector-java

5.1.47

com.mchange

c3p0

0.9.5.2

javax.servlet

servlet-api

2.5

javax.servlet.jsp

jsp-api

2.2

javax.servlet

jstl

1.2

org.mybatis

mybatis

3.5.2

org.mybatis

mybatis-spring

2.0.2

org.springframework

spring-webmvc

5.1.9.RELEASE

org.springframework

spring-jdbc

5.1.9.RELEASE

org.aspectj

aspectjweaver

1.9.4

src/main/java

**/*.properties

**/*.xml

false

src/main/resources&lPpWRekt;/directory>

**/*.properties

**/*.xml

false

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Intellij IDEA 2020.3 配置教程详解
下一篇:快递查询免费接口api(单号查询快递查询接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~