SSM中, 常用配置文件的约束头以及它们相互之间关系.

前言

一, 二, 三 是MyBatis相关的配置文件
四, 五, 六 是Spring相关的配置文件
七, 八 是SpringMVC相关的配置文件

一. MyBatis的配置文件

还是那句话, 名字可以随便取. 我这里叫 mybatis-config.xml

具体位置如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jr629ENy-1602427568595)(C:\Users\WZR11\AppData\Roaming\Typora\typora-user-images\image-20201011100136807.png)]

它的头文件是:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

</configuration>

二. DAO层的xml

配合接口使用的xml, 具体位置如图所示

在这里插入图片描述

它的头文件是

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.vandarkholme.dao.BookMapper">

</mapper>

可以看到, 它的头文件和 “一. MyBatis层的配置文件” 的头文件非常相似. 实际上只是改动了几个地方

mapper标签里面的namespace就是你要绑定的那个接口的位置 一个mapper对应一个接口

三.jdbc.properties

注意, 这玩意虽然是数据库相关的, 按理说应该MyBatis的配置文件调用它, 但是完全整合SSM后, 实际上却是Spring连接MyBatis的那个文件(spring-dao)在调用它.

调用方法: spring-dao.xml中添加如下代码:

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

jdbc.properties本身的代码如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/你的数据库名
jdbc.username=root
jdbc.password=root

这其实是连接数据库的那个配置文件, 里面内容很简单, 但是有以下几点需要注意

如果你的版本不对劲(MySQL版本过高, 或者其他导致不兼容的情况), 你需要改一下以下两个字段

  1. com.mysql.jdbc.Driver -> com.mysql.cj.jdbc.Driver

  2. url改成如下:
    mysql://localhost:3306/test?serverTimezone=Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false

    其实就是添加了字符集编码和时区设定.

四.applicationContext.xml

这玩意一般充当Spring总的配置文件, 它是Spring的大哥, 它包含了如下几个小弟:

applicationContext是Spring总的配置文件,

spring-dao.xml是Spring整合Mybatis的,

spring-mvc.xml是Spring整合SpringMVC的,

spring-service.xml是spring控制service层的.

spring-dao.xml , spring-mvc.xml , spring-service.xml 它们的关系都是一样的, 统统都是Spring的小弟, 受Spring管理

凡是Spring的小弟, 它的配置文件的头部, 就和大哥applicationContext长得一样

applicationContext是他们3个文件的大哥,如下图所示:

在这里插入图片描述

那么如何让applicationContext认识他的小弟们呢? 详见下文 “五.spring-dao.xml” 中写的内容

以下是可以复制粘贴的applicationContext代码模板(添加了几个常用的命名空间):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


</beans>

新创建出来的applicationContext, 在idea中会出现下图:

在这里插入图片描述

这里千万不要不管它, 而是根据提示配置好, 很简单.

五.spring-dao.xml

这是spring整合mybatis用的一个xml. 它的头和applicationContext是一样的

但是需要注意:新创建的spring-dao.xml会出现下图warning:

在这里插入图片描述

这里千万不要不管它, 而是根据提示配置好, 将新创建的spring-dao.xml添加进总的ApplicationContext里面

(以下与spring-dao.xml平级的东西们(说的就是spring-mvc.xml和spring-service.xml们), 都要这么设置!)

在这里插入图片描述

重点来了: 多个spring配置文件的整合, 有两个步骤把它们关联在一起

(也就是如何让applicationContext认识他的小弟们)

第一步(这一步是必做):

在applicationContext.xml文件开头敲代码

<import resource="classpath:spring-dao.xml"></import>
<import resource="classpath:spring-service.xml"></import>

通过这种方法可以在一个spring配置文件中, 关联其他的配置文件(有几个就import几个)

第二步:

就是我们开头的方法, idea设置一下

添加进applicationContext里面之后, 查看下图,就可以知道它们都关联进Spring中了
在这里插入图片描述

六.spring-service.xml

这是spring整合service层用的一个xml, 用于配置事务 它的头和applicationContext是一样的

spring要整合2个地方 :

一个是dao层. 整合要用的的配置文件是spring-dao.xml
另一个要整合的地方就是service层. 因为要配置事务. 整合要用的的配置文件是spring-service.xml

这两个文件其实都是spring的子配置文件, applicationContext都要import这两个东西, 所以它的头和说明就和spring-dao.xml一样

七.spring-mvc.xml

它是SpringMVC的配置文件, 但它同时又是Spring的配置文件(是Spring管理的小弟之一)

它的头和applicationContext是一样的

但是注意!! 这里的约束头要较之前增加一组:

xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd

如果是懒人, 也可以直接复制下面的代码块

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

</beans>

八. web.xml

只有集成SpringMVC并创建完web项目之后才会出现这个web.xml

web.xml的版本问题 每个版本都有不同的约束头, 这点自己百度去

不同版本可能导致版本冲突 反正我这里用的是web4.0 下面给出代码:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    
    <!--从这里写代码-->
    
</web-app>

web.xml文件是用来初始化配置信息:比如Welcome页面、DispatcherServlet这种SpringMVC核心配置, servlet、servlet-mapping、filter、listener、启动加载级别等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值