第一个Spring、第一个SpringBoot、Spring-Mybatis整合、SpringBoot-Mybatis整合

news2024/9/30 1:25:51

目录

  • 一、第一个Spring程序
  • 二、第一个SpringBoot
  • 三、Spring-Mybatis整合
  • 四、SpringBoot-Mybatis整合第一个程序

一、第一个Spring程序

  1. 添加依赖——用以支持spring
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.9.RELEASE</version>
    </dependency>
  1. 创建一个Hello类作为JavaBean类
public class Hello {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("Hello,"+ name );
    }
}
  1. 将Hello类装配到Spring容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--bean就是java对象 , 由Spring创建和管理-->
    <!--使用Spring来创建对象,在Spring这些都称为Bean   -->
    <!--id = 变量名-->
    <!--class = new的对象-->
    <!--property相对于给对象中的属性设置一个值-->
    <bean id="hello" class="com.kuang.pojo.Hello">
        <property name="name" value="Spring"/>
    </bean>
</beans>
  1. 测试类
//ApplicationContext这里,一旦读取了bean.xml,那么Spring容器中的所有bean都被实例化了,即在bean.xml中的所有bean都被创建了,并且所有bean都执行了无参构造方法
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//getBean : 参数即为spring配置文件中bean的id;
//就是将容器中“hello”这个JavaBean拿出来,注入到Hello hello中;
Hello hello = (Hello) context.getBean("hello");
hello.show();
  1. 测试结果
    在这里插入图片描述

二、第一个SpringBoot

SpringBoot项目创建官网

  1. 添加依赖
<!--pom.xml文件依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
  1. 创建一个Controller类
    注意:在启动类的同级目录下,新建一个controller包,一定要在同级目录下,否则识别不到
@RestController
public class HelloController {
   @RequestMapping("/hello")
   public String hello() {
       return "Hello World";
   }  
}
  1. 从主程序启动项目,浏览器发起请求,看页面返回;控制台输出了 Tomcat 访问的端口号!
    在这里插入图片描述
  2. 启动类
//SpringBoot启动类
@SpringBootApplication
public class UrgencyThirdApplication {

	public static void main(String[] args) {
		SpringApplication.run(UrgencyThirdApplication.class, args);
	}

}
  1. 个人感觉SpringBoot实际上相当于JavaConfig类
1. @SpringBootApplication类中包含了@Configuration和@ComponentScan
	1.1. @Configuration将启动类设为配置类
	1.2. @ComponentScan可以basePackageClasses或basePackages来定义要扫描的特定包。 如果没有定义特定的包,将从声明该注解的类的包开始扫描。

三、Spring-Mybatis整合

  1. 引入Spring配置文件Spring-dao.xml(以前的beans.xml)
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

</beans>
  1. 将Mybatis配置的数据源移到Spring-dao.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

	<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    我们这里使用Spring提供的JDBC:-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
</beans>
  1. SqlSessionFactoy,关联MyBatis
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

	<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    我们这里使用Spring提供的JDBC:-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--关联mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
    </bean>
</beans>
  1. 注册sqlSessionTemplate,关联sqlSessionFactory
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

	<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    我们这里使用Spring提供的JDBC:-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--关联mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
</beans>
  1. 需要UserMapper接口的UserMapperImpl 实现类,私有化sqlSessionTemplate
public class UserMapperImpl implements UserMapper {

    //我们的所有操作,都使用sqlSession来执行,在原来,现在都使用SqlsessionTemplate
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}
  1. 将自己写的实现类,注入到Spring配置文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd

	<!--DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    我们这里使用Spring提供的JDBC:-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--关联mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kuang/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate:就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    
    <bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>
</beans>
  1. 测试
    @Test
    public void test () throws IOException {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }

四、SpringBoot-Mybatis整合第一个程序

  1. 导入依赖
<!--Springboot需要的依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

<!--Mybatis项目需要的依赖-->
    <!--mysqlq驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>
    <!--mybatis-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.4</version>
    </dependency>
    <!--junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

<!--Springboot-Mybatis整合需要的依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
  1. application.yml文件
  • 放在Resource文件夹下,SpringBoot会自己扫描到
spring:
  datasource:
    name: pro_man_sys_db
    url: jdbc:mysql://localhost:3306/pro_man_sys_db?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  resources:
    static-locations: classpath:/static,classpath:/resources,file:/root/uploadFiles

mybatis:
  type-aliases-package: com.shiliuzi.model
  mapper-locations: classpath:mapper/*.xml
  1. 创建实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    private Integer id;
    private String name;
    private String pwd;

}
  1. 创建mapper目录以及对应的Mapper接口
@Repository
public interface UserMapper {
    public static final int age=18;
    List<User> queryUserList();
    User queryUserById(Integer id);
    int addUser(User user);
    int updateUser(User user);
    int deleteUserById(Integer id);
}
  1. 配置mapper.xml文件
  • 这里建议创建在resources的目录下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration core file-->
<mapper namespace="com.guo.mapper.UserMapper">

    <select id="queryUserList" resultType="User">
    </select>
</mapper>

在这里插入图片描述
6. 编译controller调用mapper

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/list")
    public List<User> List(){

        return userMapper.queryUserList();

    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/174333.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

线程池的简单介绍以及实现一个线程池

文章目录1、线程池存在的意义2、什么是线程池&#xff1f;3、线程池的使用2、java标准库中的线程池3、认识一下不同的线程池&#xff1a;4、认识一下线程池里的参数&#xff1a;4、实现一个简单的线程池1、线程池存在的意义 线程存在的意义&#xff1a;使用进程来实现并发编程…

鼠标右键没有git bash here(图文详解)

升级Win11后突然发现右键没有git bash here了解决&#xff1a;1. winr键&#xff0c;打开命令窗口,输入regedit打开注册表2. 在注册表中按照路径打开\HKEY_CLASSES_ROOT\Directory\Background\shell\3. 在shell上右键新建项&#xff0c;取名Git Bash Here&#xff0c;再点击Git…

SpringCloudConsul

上篇文章注册中心选出了Consul 和 K8S&#xff0c;现在我需要把他们集成到SpringCloud里&#xff0c;体验一下他们的服务注册发现、动态配置与权限分配难易 问题&#xff0c;以便选出更适合我们的。SpringCloudConsul首先用Docker搭建出Consul集群&#xff0c;这一步忽略了&…

8、Ubuntu22.4Server安装MariaDB10.10初始化密码Navicat远程登录

安装MariaDB10.10 查找源 apt search mariadb 在Ubuntu系统上从MariaDB存储库安装MariaDB10.10时&#xff0c;需要运行以下命令 sudo apt-get install apt-transport-https curl sudo curl -o /etc/apt/trusted.gpg.d/mariadb_release_signing_key.asc https://mariadb.org…

【微服务】Feign远程调用

本系列介绍的是Spring Cloud中涉及的知识点&#xff0c;如有错误欢迎指出~ 一.引子 我们以前基于RestTemplate发起的http请求远程调用服务&#xff1a; 存在下面的问题&#xff1a; 代码可读性差&#xff0c;编程体验不统一 参数复杂URL难以维护&#xff0c;字符串拼接硬编码…

逆卷积(ConvTranspose2d)是什么?

上图是一个卷积操作&#xff08;蓝色为输入&#xff0c;绿色为输出&#xff09;。 输入的特征图为x&#xff1a;( 4&#xff0c;4 &#xff0c;channels_in&#xff09;其中channels_in表示通道数。 卷积核设置&#xff1a;无padding&#xff0c; kernel size为3*3&#xff0c…

<关键字(1)>——《C语言深度剖析》

目录 关键字 - 第一讲 1.关键字分类 2.定义与声明 2.1 什么是变量(是什么) 2.2如何定义变量(怎么用) 2.3为什么要定义变量(为什么) 2.4 变量定义的本质 2.5 变量声明的本质 3. 最宽宏大量的关键字 - auto 3.1 变量的分类 3.2 变量的作用域 3.3 变量的生命周期 …

汇编语言(第四版)第八章 实验7 习题解答

Power idea 公司从1975年成立一直到1995年的基本情况如下&#xff1a; 下面的程序中&#xff0c;已经定义好了这些数据&#xff1a; assume cs:codesg,ds:datasgdatasg segmentdb 1975,1976,1977,1978,1979,1980,1981,1982,1983db 1984,1985,1986,1987,1988,1989,1990,1991,19…

【12】C语言_几个循环的经典练习

目录 1. 打印n的阶乘; 2、计算 1!2!3!......10! 3、用二分查找在一个有序数组中查找一个数 4、打印如下 5、输入三次密码 6、写一个猜数字游戏 7、如题 8、打印1到100之间 3的倍数 9、给两个数&#xff0c;求出最大公约数 10、找出从1000到2000之间的闰年 11、找出10…

Java是编译性语言还是解释型语言 ?

首先我们应该了解这两种语言的概念 . 高级语言在计算机上执行 , 有两种方式 , 分为编译型语言和解释型语言 . 编译型语言 : 编写源代码–>编译–>链接. 典型的编译型语言 : C/C . 特点 : 源代码中一处有错 , 就不允许编译 ; 编译过程中出现一处错误 , 就停止编译 . 优…

论文投稿指南——中文核心期刊推荐(武器工业)

【前言】 &#x1f680; 想发论文怎么办&#xff1f;手把手教你论文如何投稿&#xff01;那么&#xff0c;首先要搞懂投稿目标——论文期刊 &#x1f384; 在期刊论文的分布中&#xff0c;存在一种普遍现象&#xff1a;即对于某一特定的学科或专业来说&#xff0c;少数期刊所含…

4个免费数据恢复软件:免费恢复您的数据

意外的文件删除或文件损坏可能会令人头疼&#xff0c;尤其是在您不使用云存储的情况下。两者通常都支持已删除的文件恢复和版本控制&#xff0c;以帮助您摆脱此类困境。如果您不使用云&#xff0c;通常唯一的机会就是使用数据恢复软件来找回丢失或损坏的数据。 这种方法有两个…

使用 Burpsuite 测试的常用操作(二)

大家好啊&#xff0c;我是大田。接上篇文章「 使用 Burpsuite 测试的常用操作&#xff08;一&#xff09;」&#xff0c;今天继续分享一下 Burpsuite 在工作中常用操作三、本文中 2 个常用操作1、Burpsuite_proxy 篡改请求通过代理模式可以拦截、查看、修改所有客户端和服务器端…

(二)Linux嵌入式开发——软件安装(Ubuntu)

文章目录&#xff08;二)Linux嵌入式开发——软件安装&#xff08;Ubuntu&#xff09;APP StoreAPT工具问题解决办法1解决办法2deb软件包程序源码问题1解决办法问题2解决办法总结&#xff08;二)Linux嵌入式开发——软件安装&#xff08;Ubuntu&#xff09; 接下来&#xff0c;…

【GD32F427开发板试用】FOC矢量算法研究系列之一:PWM实现呼吸灯

本篇文章来自极术社区与兆易创新组织的GD32F427开发板评测活动&#xff0c;更多开发板试用活动请关注极术社区网站。作者&#xff1a;Q_dGHvwj 前言 有幸被选为此次开发板测评活动的参与者&#xff0c;万分感谢。收到板子后迫不及待的拆来查看&#xff0c;画风整齐美观&#x…

SpringCloudAlibabaNacosConfig学习笔记

目录 1. Nacos配置中心使用 2 搭建nacos-config服务 3 Config相关配置 4 .配置的优先级 5. RefreshScope 1. Nacos配置中心使用 官方文档&#xff1a; https://github.com/alibaba/springcloudalibaba/wiki/Nacosconfig Nacos 提供用于存储配置和其他元数据的key/value 存…

23种设计模式(十三)——代理模式【接口隔离】

文章目录 意图什么时候使用代理真实世界类比代理模式的实现代理模式的优缺点亦称:Proxy 意图 由于某些原因需要给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标对象,代理对象作为访问对象和目标对象之间的中介。 什么时候使用代理 1、…

论文投稿指南——中文核心期刊推荐(环境科学)

【前言】 &#x1f680; 想发论文怎么办&#xff1f;手把手教你论文如何投稿&#xff01;那么&#xff0c;首先要搞懂投稿目标——论文期刊 &#x1f384; 在期刊论文的分布中&#xff0c;存在一种普遍现象&#xff1a;即对于某一特定的学科或专业来说&#xff0c;少数期刊所含…

操作系统-操作系统引论(1)

操作系统的基本概念一、操作系统的定义二、操作系统的产生与发展三、操作系统的特征四、操作系统的功能五、操作系统的运行机制六、操作系统的体系结构一、操作系统的定义 资源管理的观点&#xff1a;操作系统是控制和管理计算机的软、硬件资源&#xff0c;并且合理的组织计算…

通过C++对【图】进行抽丝剥茧(包括广度、深度优先遍历,求最小生成树,求最短路径)

目录 一.图的基本概念 二.图的存储结构 1.邻接矩阵 &#xff08;1&#xff09;无向图、有向图矩阵存储 &#xff08;2&#xff09;实现&#xff1a; 2.邻接表 &#xff08;1&#xff09;无向图邻接表存储 &#xff08;2&#xff09;有向图邻接表存储 &#xff08;3&a…