Spring.xml和SringMVC.xml和Mybatis.xml的基本配置

目录

Spring.xml和SringMVC.xml和Mybatis.xml的基本配置

一、Spring.xml配置文件

        1、文件头

        2、扫描@mapper、@service、@component等注解

        3、创建数据源——水井

        4、创建数据工厂——抽水机

        5、配置事务

        6、配置Mapper扫描器(非常重要!!!)

二、SpringMVC.xml配置文件

        1、文件头

        2、扫描@controller中的注解

        3、开启SpringMVC注解支持

        4、静态资源被忽略js、css、image等

        5、前端控制器(视图解析器)

三、Mybatis.xml配置文件

        1、文件头

        2、映射对应表(Admin)的MapperAdmin.xml

一、Spring.xml配置文件

1、文件头

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

扫描mapper包和service包

2、扫描@mapper、@service、@component等注解

<context:component-scan base-package="com.mapper" />
<context:component-scan base-package="com.service" />

3、创建数据源——水井

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

可以通过编写db.properties文件引入value的值——value=${jdbc."对应的name"}

加载db.properties文件

<context:property-placeholder location="classpath:db.properties" />
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url= jdbc:mysql://127.0.0.1/library?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456

4、创建数据工厂——抽水机

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis.xml的核心配置文件 -->
        <property name="configLocation" value="classpath:Mybatis.xml" />
    </bean>

注意:数据源的id="dataSource"要和数据工厂的ref="dataSource"保持一致

5、配置事务

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

例:注解事务

<tx:annotation-driven transaction-manager="transactionManager"/>

6、配置Mapper扫描器(非常重要!!!)

spring会根据com.mapper包的接口完成实现类的方法(隐藏)

<bean id="mapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mapper"/>
</bean>

二、SpringMVC.xml配置文件

1、文件头

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

扫描controller包

2、扫描@controller中的注解

<context:component-scan base-package="com.controller"/> 

3、开启SpringMVC注解支持

<mvc:annotation-driven/>

4、静态资源被忽略js、css、image等

<mvc:default-servlet-handler/>

5、前端控制器(视图解析器)

<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

三、Mybatis.xml配置文件

重点

1、文件头

<?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">

2、映射对应表(Admin)的MapperAdmin.xml

<configuration>
        <mappers>
            <mapper resource="com/MapperAdmin.xml"/>
        </mappers>
</configuration>

MapperAdmin.xml文件头要修改configuration和Config——mapper和Mapper

<?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">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值