当前位置:   article > 正文

【SpringBoot3+Vue3】一【基础篇】_springboot+vue

springboot+vue

目录

一、Spring Boot概述

1、Spring Boot 特性

1.1 起步依赖

1.2 自动配置 

1.3 其他特性

1.3.1 内嵌的Tomcat、Jetty (无需部署WAR文件)

1.3.2 外部化配置

1.3.3 不需要XML配置(properties/yml)

二、Spring Boot入门

1、一个入门程序需求

2、步骤

2.1 创建Maven工程

2.2 导入Spring-boot-start-web起步依赖 

2.3 编写Controller 

2.4 提供启动类 

3、手动创建SpringBoot工程(不重要)

3.1 创建Maven工程

3.2 引入依赖

3.3 提供启动类

3.3.1 修改App文件名为启动类名

3.3.2 手动生成resources文件夹

3.3.3 手动生成application.properies 等

三、SpringBoot配置文件

1、properties配置文件

2、yaml配置文件(推荐)

3、yml配置信息书写与获取

2.1 第三方技术配置信息

2.2 自定义配置信息

2.2.1 引入发送邮件依赖

2.2.2 工具类MailUtil 

2.2.3 controller

2.2.4 service

2.2.5 serviceImpl

2.2.6 yml配置文件

四、整合Mybatis-plus

1、pom

2、yml

3、emp.sql

4、统一返回类

5、controller

6、service

7、serviceImpl

五、Bean管理

1、Bean扫描

2、Bean注册 

2.1 @Bean 

2.1.1 @Bean方法一:启动类创建一个方法(不建议使用)

2.1.2 @Bean方法二:配置类(建议)

2.2 @Import

2.2.1 导入配置类

2.2.2 导入ImportSelector接口实现类

 3、注册条件

六、自动配置

七、自定义starter


前言:通过SpringBoot3+Vue3学习,我们可以学会单体前后端当中的常用技术,独立完成后端接口和页面的开发,大大提升编码能力.本课程需要jdk17+、idea2021+、maven3.5+,Spring3+Vue3

一、Spring Boot概述

Spring Boot是Spring提供的一个子项目,用于快速构建Spring应用程序

1、Spring Boot 特性

1.1 起步依赖

本质上就是一个Maven坐标,整合了完成一个功能需要的所有坐标

1.2 自动配置 

遵循约定大约配置的原则,在boot程序启动后,一些bean对象会自动注入到ioc容器,不需要手动声明,简化开发。

1.3 其他特性

1.3.1 内嵌的Tomcat、Jetty (无需部署WAR文件)
1.3.2 外部化配置
1.3.3 不需要XML配置(properties/yml)
 

二、Spring Boot入门

1、一个入门程序需求

需求:使用SpringBoot开发一个web应用,浏览器发起请求/hello后,给浏览器返回字符串“hello world ~".

2、步骤

2.1 创建Maven工程

2.2 导入Spring-boot-start-web起步依赖 

2.3 编写Controller 

  1. package com.bocai.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @RequestMapping("/hello")
  7. public String hello(){
  8. return "Hello World!";
  9. }
  10. }

2.4 提供启动类 

 启动服务访问:http://localhost:8080/hello

3、手动创建SpringBoot工程(不重要)

3.1 创建Maven工程

3.2 引入依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>3.1.5</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

3.3 提供启动类

3.3.1 修改App文件名为启动类名

一般为项目名+Application

  1. package com.bocai;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class SpringbootQuickstartApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(SpringbootQuickstartApplication.class, args);
  8. }
  9. }
3.3.2 手动生成resources文件夹

3.3.3 手动生成application.properies 等

三、SpringBoot配置文件

1、properties配置文件

server.port=9090

2、yaml配置文件(推荐)

  1. server:
  2. port: 9091
  3. servlet:
  4. context-path: /start2

3、yml配置信息书写与获取

2.1 第三方技术配置信息

2.2 自定义配置信息

发送邮件为例

2.2.1 引入发送邮件依赖
  1. <!-- 发送邮件 -->
  2. <dependency>
  3. <groupId>org.eclipse.angus</groupId>
  4. <artifactId>jakarta.mail</artifactId>
  5. </dependency>
2.2.2 工具类MailUtil 
  1. package com.bocai.utils;
  2. import com.bocai.pojo.EmailProperties;
  3. import jakarta.mail.*;
  4. import jakarta.mail.internet.InternetAddress;
  5. import jakarta.mail.internet.MimeMessage;
  6. import java.util.Properties;
  7. public class MailUtil {
  8. /**
  9. * 发送邮件
  10. * @param emailProperties 发件人信息(发件人邮箱,发件人授权码)及邮件服务器信息(邮件服务器域名,身份验证开关)
  11. * @param to 收件人邮箱
  12. * @param title 邮件标题
  13. * @param content 邮件正文
  14. * @return
  15. */
  16. public static boolean sendMail(EmailProperties emailProperties, String to, String title, String content){
  17. MimeMessage message = null;
  18. try {
  19. Properties properties = new Properties();
  20. properties.put("mail.smtp.host", emailProperties.getHost());
  21. properties.put("mail.smtp.auth",emailProperties.isAuth());
  22. properties.put("mail.user", emailProperties.getUser());
  23. properties.put("mail.password", emailProperties.getCode());
  24. // 构建授权信息,用于进行SMTP进行身份验证
  25. Authenticator authenticator = new Authenticator() {
  26. @Override
  27. protected PasswordAuthentication getPasswordAuthentication() {
  28. return new PasswordAuthentication(emailProperties.getUser(), emailProperties.getCode());
  29. }
  30. };
  31. // 使用环境属性和授权信息,创建邮件会话
  32. Session mailSession = Session.getInstance(properties, authenticator);
  33. // 创建邮件消息
  34. message = new MimeMessage(mailSession);
  35. }catch (Exception e){
  36. e.printStackTrace();
  37. }
  38. //如果邮件创建失败,直接返回
  39. if (message==null){
  40. return false;
  41. }
  42. try {
  43. // 设置发件人
  44. InternetAddress form = new InternetAddress(emailProperties.getUser());
  45. message.setFrom(form);
  46. // 设置收件人
  47. InternetAddress toAddress = new InternetAddress(to);
  48. message.setRecipient(Message.RecipientType.TO, toAddress);
  49. // 设置邮件标题
  50. message.setSubject(title);
  51. // 设置邮件的内容体
  52. message.setContent(content, "text/html;charset=UTF-8");
  53. // 发送邮件
  54. Transport.send(message);
  55. }catch (Exception e){
  56. e.printStackTrace();
  57. }
  58. return true;
  59. }
  60. }
2.2.3 controller
  1. package com.bocai.controller;
  2. import com.bocai.service.EmailServcie;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class EmailController {
  8. @Autowired
  9. private EmailServcie emailServcie;
  10. @RequestMapping("/send")
  11. public Boolean send(){
  12. //收件人信箱
  13. String to = "sdadae@qq.com";
  14. //邮件标题
  15. String title = "测试邮件";
  16. //正文
  17. String content = "使用java进行邮件发送测试的正文";
  18. boolean flag = emailServcie.send(to,title,content);
  19. return flag;
  20. }
  21. }
2.2.4 service
  1. package com.bocai.service;
  2. public interface EmailServcie {
  3. boolean send(String to, String title, String content);
  4. }
2.2.5 serviceImpl
  1. package com.bocai.service.impl;
  2. import com.bocai.pojo.EmailProperties;
  3. import com.bocai.service.EmailServcie;
  4. import com.bocai.utils.MailUtil;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class EmailServiceImpl implements EmailServcie {
  9. @Autowired
  10. private EmailProperties emailProperties;
  11. /**
  12. *
  13. * @param to 收件人邮箱
  14. * @param title 标题
  15. * @param content 正文
  16. * @return
  17. */
  18. @Override
  19. public boolean send(String to, String title, String content) {
  20. boolean flag = MailUtil.sendMail(emailProperties,to,title,content);
  21. return flag;
  22. }
  23. }
2.2.6 yml配置文件

  1. server:
  2. port: 9091
  3. # 发送邮件配置信息
  4. email:
  5. user: 11332@qq.com
  6. code: fdsdsfsdfs
  7. host: smtp.qq.com
  8. auth: true

本文使用的是这个 

@ConfigurationProperties(prefix = "email")

 可忽略上面的红色提示不管

四、整合Mybatis-plus

1、pom

mybatis-plus在springboot3.X版本之后要使用3.5.3以上版本

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.1.5</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.bocai</groupId>
  12. <artifactId>mybatis-plus-module</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>mybatis-plus-module</name>
  15. <description>mybatis-plus-module</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.mysql</groupId>
  26. <artifactId>mysql-connector-j</artifactId>
  27. <scope>runtime</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.projectlombok</groupId>
  31. <artifactId>lombok</artifactId>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-test</artifactId>
  37. <scope>test</scope>
  38. </dependency>
  39. <!-- 3、 引入mybatisplus -->
  40. <dependency>
  41. <groupId>com.baomidou</groupId>
  42. <artifactId>mybatis-plus-boot-starter</artifactId>
  43. <version>3.5.3</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>com.alibaba</groupId>
  47. <artifactId>druid-spring-boot-starter</artifactId>
  48. <version>1.1.23</version>
  49. </dependency>
  50. </dependencies>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. <configuration>
  57. <image>
  58. <builder>paketobuildpacks/builder-jammy-base:latest</builder>
  59. </image>
  60. <excludes>
  61. <exclude>
  62. <groupId>org.projectlombok</groupId>
  63. <artifactId>lombok</artifactId>
  64. </exclude>
  65. </excludes>
  66. </configuration>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

2、yml

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/springboot_vue?serverTimezone=UTC
  6. username: root
  7. password: @@@@0
  8. mybatis-plus:
  9. configuration:
  10. map-underscore-to-camel-case: true # 在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
  11. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制台显示sql
  12. default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler # 配置全局枚举处理器,好像还有说json的
  13. global-config:
  14. db-config:
  15. id-type: auto # 数据库id生产规则全局 配置 # ASSIGN_ID雪花算法,数据库id建议使用Long类型
  16. logic-delete-field: deleted # 全局配置逻辑删除字段名
  17. logic-delete-value: 0 # 全局配置# 逻辑已删除值(默认为 1)这里因为我是反的所以改成0
  18. logic-not-delete-value: 1 # 逻辑未删除值(默认为 0)这里因为我是反的所以改成1
  19. # table-prefix: tbl_ # 数据库表前缀全局配置
  20. banner: false # 关闭控制台mybatis-plus的logo
  21. # type-enums-package: com.bocai.enums # 扫描通用枚举包 或者使用上面那个枚举全局配置

3、emp.sql

  1. -- 员工管理(带约束)
  2. CREATE TABLE emp (
  3. id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT 'ID',
  4. username VARCHAR(20) NOT NULL UNIQUE COMMENT '用户名',
  5. PASSWORD VARCHAR(32) DEFAULT '123456' COMMENT '密码',
  6. NAME VARCHAR(10) NOT NULL COMMENT '姓名',
  7. gender TINYINT UNSIGNED NOT NULL COMMENT '性别, 说明: 1 男, 2 女',
  8. image VARCHAR(300) COMMENT '图像',
  9. job TINYINT UNSIGNED COMMENT '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管, 5 咨询师',
  10. entrydate DATE COMMENT '入职时间',
  11. dept_id INT UNSIGNED COMMENT '部门ID',
  12. create_time DATETIME NOT NULL COMMENT '创建时间',
  13. update_time DATETIME NOT NULL COMMENT '修改时间'
  14. ) COMMENT '员工表';
  15. INSERT INTO emp
  16. (id, username, PASSWORD, NAME, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES
  17. (1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,NOW(),NOW()),
  18. (2,'zhangwuji','123456','张无忌',1,'2.jpg',2,'2015-01-01',2,NOW(),NOW()),
  19. (3,'yangxiao','123456','杨逍',1,'3.jpg',2,'2008-05-01',2,NOW(),NOW()),
  20. (4,'weiyixiao','123456','韦一笑',1,'4.jpg',2,'2007-01-01',2,NOW(),NOW()),
  21. (5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,NOW(),NOW()),
  22. (6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,NOW(),NOW()),
  23. (7,'jixiaofu','123456','纪晓芙',2,'7.jpg',1,'2005-08-01',1,NOW(),NOW()),
  24. (8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,NOW(),NOW()),
  25. (9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,NOW(),NOW()),
  26. (10,'zhaomin','123456','赵敏',2,'10.jpg',1,'2013-09-05',1,NOW(),NOW()),
  27. (11,'luzhangke','123456','鹿杖客',1,'11.jpg',5,'2007-02-01',3,NOW(),NOW()),
  28. (12,'hebiweng','123456','鹤笔翁',1,'12.jpg',5,'2008-08-18',3,NOW(),NOW()),
  29. (13,'fangdongbai','123456','方东白',1,'13.jpg',5,'2012-11-01',3,NOW(),NOW()),
  30. (14,'zhangsanfeng','123456','张三丰',1,'14.jpg',2,'2002-08-01',2,NOW(),NOW()),
  31. (15,'yulianzhou','123456','俞莲舟',1,'15.jpg',2,'2011-05-01',2,NOW(),NOW()),
  32. (16,'songyuanqiao','123456','宋远桥',1,'16.jpg',2,'2007-01-01',2,NOW(),NOW()),
  33. (17,'chenyouliang','123456','陈友谅',1,'17.jpg',NULL,'2015-03-21',NULL,NOW(),NOW());

4、统一返回类

  1. package com.bocai.common;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @Data
  6. @NoArgsConstructor
  7. @AllArgsConstructor
  8. public class Result {
  9. private Integer code;//响应码,1 代表成功; 0 代表失败
  10. private String msg; //响应信息 描述字符串
  11. private Object data; //返回的数据
  12. //增删改 成功响应
  13. public static Result success(){
  14. return new Result(1,"success",null);
  15. }
  16. //查询 成功响应
  17. public static Result success(Object data){
  18. return new Result(1,"success",data);
  19. }
  20. //失败响应
  21. public static Result error(String msg){
  22. return new Result(0,msg,null);
  23. }
  24. }

5、controller

  1. package com.bocai.controller;
  2. import com.bocai.common.Result;
  3. import com.bocai.pojo.Emp;
  4. import com.bocai.service.EmpService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. @RestController
  12. @Slf4j
  13. @RequestMapping("/emps")
  14. public class EmpController {
  15. @Autowired
  16. private EmpService empService;
  17. @GetMapping("/{id}")
  18. public Result queryById(@PathVariable Integer id){
  19. Emp emp = empService.getById(id);
  20. return Result.success(emp);
  21. }
  22. }

6、service

  1. package com.bocai.service;
  2. import com.bocai.pojo.Emp;
  3. import com.baomidou.mybatisplus.extension.service.IService;
  4. /**
  5. * @author cheng
  6. * @description 针对表【emp(员工表)】的数据库操作Service
  7. * @createDate 2023-11-13 16:53:52
  8. */
  9. public interface EmpService extends IService<Emp> {
  10. }

7、serviceImpl

  1. package com.bocai.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.bocai.pojo.Emp;
  4. import com.bocai.service.EmpService;
  5. import com.bocai.mapper.EmpMapper;
  6. import org.springframework.stereotype.Service;
  7. /**
  8. * @author cheng
  9. * @description 针对表【emp(员工表)】的数据库操作Service实现
  10. * @createDate 2023-11-13 16:53:52
  11. */
  12. @Service
  13. public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp>
  14. implements EmpService{
  15. }

8、mapper

  1. package com.bocai.mapper;
  2. import com.bocai.pojo.Emp;
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import org.apache.ibatis.annotations.Mapper;
  5. /**
  6. * @author cheng
  7. * @description 针对表【emp(员工表)】的数据库操作Mapper
  8. * @createDate 2023-11-13 16:53:52
  9. * @Entity com.bocai.pojo.Emp
  10. */
  11. @Mapper
  12. public interface EmpMapper extends BaseMapper<Emp> {
  13. }

http://localhost:8080/emps/1

五、Bean管理

1、Bean扫描

SpringBoot默认扫描启动类所在包及其子包

2、Bean注册 

 

2.1 @Bean 

2.1.1 @Bean方法一:启动类创建一个方法(不建议使用)

2.1.2 @Bean方法二:配置类(建议)

 

 

2.2 @Import

2.2.1 导入配置类

2.2.2 导入ImportSelector接口实现类

优化一下使用配置文件

  1. package com.itheima.config;
  2. import org.springframework.context.annotation.ImportSelector;
  3. import org.springframework.core.type.AnnotationMetadata;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class CommonImportSelector implements ImportSelector {
  11. @Override
  12. public String[] selectImports(AnnotationMetadata importingClassMetadata) {
  13. //读取配置文件的内容
  14. List<String> imports = new ArrayList<>();
  15. InputStream is = CommonImportSelector.class.getClassLoader().getResourceAsStream("common.imports");
  16. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  17. String line = null;
  18. try {
  19. while((line = br.readLine())!=null){
  20. imports.add(line);
  21. }
  22. } catch (IOException e) {
  23. throw new RuntimeException(e);
  24. } finally {
  25. if (br!=null){
  26. try {
  27. br.close();
  28. } catch (IOException e) {
  29. throw new RuntimeException(e);
  30. }
  31. }
  32. }
  33. return imports.toArray(new String[0]);
  34. }
  35. }

 3、注册条件

  1. package com.itheima.config;
  2. import cn.itcast.pojo.Country;
  3. import cn.itcast.pojo.Province;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  7. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. @Configuration
  11. public class CommonConfig {
  12. //注入Country对象
  13. //如果配置文件中配置了指定的信息,则注入,否则不注入
  14. @ConditionalOnProperty(prefix = "country",name = {"name","system"})
  15. @Bean
  16. public Country country(@Value("${country.name}") String name,@Value("${country.system}") String system){
  17. Country country = new Country();
  18. country.setName(name);
  19. country.setSystem(system);
  20. return country ;
  21. }
  22. //对象默认的名字是: 方法名
  23. //@Bean("aa")
  24. //如果方法的内部需要使用到ioc容器中已经存在的bean对象,那么只需要在方法上声明即可,spring会自动的注入
  25. /*@Bean
  26. public Province province(Country country){
  27. System.out.println("province: "+country);
  28. return new Province();
  29. }*/
  30. //如果ioc容器中不存在Country,则注入Province,否则不注入
  31. @Bean
  32. //@ConditionalOnMissingBean(Country.class)
  33. //如果当前环境中存在DispatcherServlet类,则注入Province,否则不注入
  34. //如果当前引入了web起步依赖,则环境中有DispatcherServlet,否则没有
  35. @ConditionalOnClass(name = "org.springframework.web.servlet.DispatcherServlet")
  36. public Province province(){
  37. return new Province();
  38. }
  39. }

六、自动配置

七、自定义starter

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <version>3.1.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  9. <version>3.0.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.mybatis</groupId>
  13. <artifactId>mybatis</artifactId>
  14. <version>3.5.13</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.mybatis</groupId>
  18. <artifactId>mybatis-spring</artifactId>
  19. <version>3.0.0</version>
  20. </dependency>

  1. package com.itheima.config;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.mapper.MapperScannerConfigurer;
  5. import org.springframework.beans.factory.BeanFactory;
  6. import org.springframework.boot.autoconfigure.AutoConfiguration;
  7. import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
  8. import org.springframework.context.annotation.Bean;
  9. import javax.sql.DataSource;
  10. import java.util.List;
  11. @AutoConfiguration//表示当前类是一个自动配置类
  12. public class MyBatisAutoConfig {
  13. //SqlSessionFactoryBean
  14. @Bean
  15. public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
  16. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
  17. sqlSessionFactoryBean.setDataSource(dataSource);
  18. return sqlSessionFactoryBean;
  19. }
  20. //MapperScannerConfigure
  21. @Bean
  22. public MapperScannerConfigurer mapperScannerConfigurer(BeanFactory beanFactory){
  23. MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
  24. //扫描的包:启动类所在的包及其子包
  25. List<String> packages = AutoConfigurationPackages.get(beanFactory);
  26. String p = packages.get(0);
  27. mapperScannerConfigurer.setBasePackage(p);
  28. //扫描的注解
  29. mapperScannerConfigurer.setAnnotationClass(Mapper.class);
  30. return mapperScannerConfigurer;
  31. }
  32. }
org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.itheima.config.MyBatisAutoConfig

 

  1. <dependency>
  2. <groupId>com.itheima</groupId>
  3. <artifactId>dmybatis-spring-boot-autoconfigure</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter</artifactId>
  9. <version>3.1.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-data-jdbc</artifactId>
  14. <version>3.0.0</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.mybatis</groupId>
  18. <artifactId>mybatis</artifactId>
  19. <version>3.5.13</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mybatis</groupId>
  23. <artifactId>mybatis-spring</artifactId>
  24. <version>3.0.0</version>
  25. </dependency>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/567668
推荐阅读
相关标签
  

闽ICP备14008679号