springboot源码分析,整合数据源,mybatis,pageHelper,restful风格关于前台传数组分析

springboot扫描包的原理

(1)从SpringBootApplication类中,按着Ctrl键进去@SpringBootApplication
在这里插入图片描述
(2)再点着@EnableAutoConfiguration
@EnableConfigurationProperties,就是用来配置和初始化bean的在这里插入图片描述
(3)点着@AutoConfigurationPackage进去
在这里插入图片描述(4)在这里面@Import(AutoConfigurationPackages.Registrar.class)点着Registrar进去
在这里插入图片描述
(5)找到这个代码
在这里插入图片描述
(6)在这行打个断点就能看到扫描的包
在这里插入图片描述
(7)扫描包如下:
在这里插入图片描述

springboot自动装配的原理

(1)从SpringBootApplication类中,按着Ctrl键进去@SpringBootApplication
在这里插入图片描述
(2)再点着@EnableAutoConfiguration
@EnableConfigurationProperties,就是用来配置和初始化bean的在这里插入图片描述
(2)再点着@Import(AutoConfigurationImportSelector.class)中的
AutoConfigurationImportSelector进去

在这里插入图片描述
(3)找到这段代码
在这里插入图片描述
(4)在这打个断点
在这里插入图片描述
(5)扫描出来有127个在这里插入图片描述
(6)自动装配类从这里可以找到
在这里插入图片描述
(7)所有的配置都是由org.springframework.boot.autoconfigure.EnableAutoConfiguration来完成注入。只有再pom.xml文件中引入了哪些启动类的jar时,该启动类才会被加
载进来。
在这里插入图片描述
(8)就是通过这种方式注入到bean容器并且初始化的,这就是springboot的“约定大于配置”。
注意:这里默认放行一切静态资源,包名必须交static
启动时会自动注册DispatcherServlet原理:
-----DispatcherServletAutoConfiguration
在这里插入图片描述
(9)这里的Bean中的name
在这里插入图片描述
(10)这里的path默认就为**/**
在这里插入图片描述

springboot整合数据源

(1)引入·druid依赖

<!--        druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>
<!--        mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--       数据源 jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        

(2)在application.properties中配置数据源信息

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名?characterEncoding=utf8
spring.datasource.username=root
spring.activemq.password=root

(3)测试

@SpringBootTest
class Springboot03ApplicationTests {

    @Autowired
    private DataSource dataSource;
    @Test
    void contextLoads() throws  Exception{
        System.out.println(dataSource);
    }
}

springboot整合mybatis

(1)引入mybatis和springboot的整合依赖

 <!--mybatis和springboot整合的依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

(2)在application.properties中引入映射文件的配置

#映射文件所在的路径
#mybatis.mapper-locations=classpath:mapper/*.xml

(3)在启动类MmApplication出加入dao接口的扫描
在这里插入图片描述

@MapperScan(basePackages = {“com.ykq.dao”})
//为dao包下的所有接口生产实现类

@SpringBootApplication
@MapperScan(basePackages = {"com.ykq.dao"}) //为dao包下的所有接口生产实现类
public class Springboot03Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot03Application.class, args);
    }
}

在这里插入图片描述

springboot整合pageHelper

(1)引入pageHelper依赖

 <!--引入pageHelper的依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
        </dependency>

(2)测试

   @Test
    void contextLoads() throws  Exception{
        PageHelper.startPage(1,5);
        List<Emp> list = empMapper.selectAll();
        PageInfo pageInfo=new PageInfo(list);
        System.out.println("总页码:"+pageInfo.getPages());
        System.out.println("总条数:"+pageInfo.getTotal());
        System.out.println("当前页码的记录:"+pageInfo.getList().size());
    }

设置全局json时间格式

#配置json的时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

添加dao的sql日志

#添加sql日志com.zz.dao必须为自己dao接口所在的包
logging.level.com.zz.dao=debug

常见的注解

springmvc中常见的注解:
@RestController
@GetMapping(查询)
@PostMapping(添加)
@DeleteMapping(删除)
@PutMapping(修改)
@RequestBody (把json对象转化为java对象 用于接受参数)
@ResponseBody (把java对象转化为json对象 方法接口的返回值)
@RequestMapping(请求路径)
@PathVariable (获取请求地址栏的占位符的值)
@ExceptionHandler (处理异常的方法上添加的注解)
@RestControllerAdvice //处理异常的类
2. Spring的注解
@controller
@Service
@Autowired
@Component

案例注意事项

(1)使用restful风格
①在路径上传参数,后台要用**@PathVariable**来接收参数
注意:{page}要跟@PathVariable括号里面的值对应,使用@PathVariable注解一定要传值,不传则会报错
在这里插入图片描述

②**@RequestBody**这个注解将前台传来的json数据转化为对象类型
**eg1:**第一种前台传数组在这里插入图片描述
注意:
通过ajax把json数组传到后台需要用@RequestBody接收
1、把数据封装成json数组
2、把数据通过data:JSON.stringify(ids),传参
3、 headers: {
‘Content-Type’: ‘application/json’
},
设置ajax发送方式为json
4、后台通过@RequestBody Integer[] ids 接收

 var data = checkStatus.data;
                    var ids = [];
                    $.each(data, function (index, item) {
                        ids.push(item.emp_id);
                    })

                    $.ajax({
                        // 设置请求为json  后台就ok  参数就能@RequestBody 对上
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        url: "/emp/deleteAll",
                        data:JSON.stringify(ids),
                        // data: JSON.stringify(ids),
                        dataType: "json",
                        type: "post",
                        //通过ajax把数组传到后台
                        // traditional: true,
                        success: function (dataInfo) {
                            if (dataInfo.code == "200") {
                                layer.msg(dataInfo.msg);
                                //重载表格
                                tablei.reload();
                            } else {
                                layer.msg(dataInfo.msg);
                            }
                        }
                    })

                    break;

在这里插入图片描述

第二种:前台传数组。
前台通过ajax发送请求,前台ajax添加,后台就不用加**@RequestBody**这个注解

  //通过ajax把数组传到后台
      traditional: true,

在这里插入图片描述

**eg2:**前台传来的json数据传到后台
在这里插入图片描述

(1) 统一返回类型

public class CommonResult {
    private int code;
    private String msg;
    private Object data;
 }

(2) 全局异常处理类。

@RestControllerAdvice //处理异常的类
public class GlobalExceptionHandler {
    //当发生Exception异常则会执行该方法
    @ExceptionHandler(value = Exception.class)
    public CommonResult handleException(){
        return new CommonResult(5000,"失败");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值