Spring学习

这篇博客介绍了如何将Spring MVC与MyBatis进行整合。首先,通过Maven仓库引入Spring Web MVC依赖,然后配置Spring自动装配Bean,讲解了ByName和ByType两种方式。接着,展示了使用注解进行开发的方法,并指定了扫描包和启用注解驱动。最后,提到了两种整合MyBatis的实现方式,包括将MyBatis配置融入Spring以及创建Dao接口的实现类。
摘要由CSDN通过智能技术生成

1.获取spring

官网
maven仓库

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.13</version>
</dependency>

Hello,Spring
spring01

2.配置

请添加图片描述
请添加图片描述

3.自动装配Bean

自动装配

  • 一个人有两个宠物
  • ByName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid !
  • ByType:会自动在容器上下文中查找,和自己对象属性类型相同的bean !

用注解装配

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

    <context:annotation-config/>

</beans>

自动装配
spring02

4. 注解开发

注解开发
spring03

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

    <!--指定要扫描的包,这个包下的注解就会生效-->
    <context:component-scan base-package="com.kuang"/>
    <!--注解驱动-->
    <context:annotation-config/>

</beans>

5.整合mybatis

整合mybatis

回忆mybatis

1.在pom.xml里导入jar包
2.写pojo实体类
3.写mybatis-config.xml配置文件
4.写Mapper接口
5.写接口对应的Mapper.xml映射文件
6.写utils工具类
7.写测试类test

实现方法一

spring04
1.加spring的配置文件beans.xml
把mybatis配置文件的东西拿过来,去掉mybatis里面的部分
2.加一个UserDaoImpl继承UserMapper接口
增加Dao接口的实现类;私有化sqlSessionTemplate
3.改测试类

实现方法二

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值