栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021.12.30 新手入门Springboot精辟教程(上)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021.12.30 新手入门Springboot精辟教程(上)

Springboot
·使用Springboot免除了很多配置文件,越来越容易上手,更轻量级的Web开发;
·免除了Server(Tomcat),只需要导出一个jar包,用java ·jar即可启动web项目

第一个Springboot程序


在resources中放配置文件,在pom中引入jar依赖。
Helloword两种写法
1.@Controller 只返回界面,先使用
2.@RestController 返回JSON格式的数据
启动方法:
1.@EnableAutoConfiguration启动
2.目录中新建一个Application类,注解@SpringBootApplication启动

package org.jeecg.modules.manage.helloword;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@EnableAutoConfiguration
public class Helloword {

    @RequestMapping("/hello")
    public String hello(){
        return "hello111";
    }
    public static void main(String[] args){
        SpringApplication.run(Helloword.class,args);
    }
}
Springboot访问静态文件

Springboot基本采用了默认扫描的方式:内部默认去扫描某一些文件夹或者文件,配置:默认扫描application.properties 和 静态文件:static,默认就扫描这个文件,放置如css,js,img,font。

Springboot捕获全局异常
    @RequestMapping("/except")
    public String except(){
       try{
           int a = 2 / 0;
       } catch (Exception e){
           System.out.println("发生异常了");
       }
        return "Exception!";
    }

全局捕获改进,无需手写AOP捕获,重新定义一个类 GlobleExceptionAop。

package org.jeecg.modules.manage.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class GrobleExceptionAop {
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public String runtimeException(){
        return "出现异常了,在这里捕获全局异常,相当于手写AOP捕获异常"
    }
}

Springboot集成Thymeleaf

SSM框架采用JS
Springboot不推荐使用JSP(JSP编写成Servlet,属于重量级的)Springboot中推荐使用模板
原生HTML:Thymeleaf模板、Freemaker模板、Groovy模板
使用这三个框架,减少服务器消耗
·前台框架使用路径:templates文件夹路径
需要先在application.properties中配置文件

然后在application.properties中添加依赖

新建一个index.html

新建一个PageController类来启动

Model:向前台返回一些数据
例如
后端

页面

Thymeleaf数据展示

后端

页面

Springboot集成Mybatis

先在pom里面引入mybatis相关的jar包

然后在application.properties中填写连接数据库相关的配置信息

新建Mapper类

在controller中新建类


或者用
只是get方法中找不到insert 要用post方法

Springboot事务管理

事务:多个sql作为单一逻辑单元进行执行操作,要么完全执行,要么完全不执行。
特性:
原子性(多个SQL语句,要么都执行,要么都不执行)
一致性(事务完成,必须使所有的数据保持一致)
隔离性(当前事务的修改必须与其他事务隔离开,不能同时进行操作一个数据)
持久性(事务执行完毕过后,对数据的影响是永久的)

例子:先建立一个Service接口

再新建一个实现类impl

再在Mapper中写语句

最后在controller中执行操作


以上 没有体现原子性,Service impl中加上注解@Transactional即可

出现异常就自动回滚。

Springboot集成多数据源

resources里面配置

在config中新建类

MapperScan扫描 com.majiaxueyuan.maper.test1中的DataSource1Config
把@Primary删除,因为此表明为主数据库,多数据源没有主库。

复制粘贴 再建一个DataSource2Config 即可;

多数据源Mapper与单数据库操作一样,以查询为例:

新建两个Mapper;

Controller中新建:

新建APP启动类:

即可以集成多个数据库,缺点:效率低。
采用用分布式可以很好解决。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/686911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号