码云私人令牌70e0a7b76ff3e5f2d8871729ac48ee28

a.该阶段如何学习

b.SpringBoot

核心思想:约定大于配置

基于Spring,默认配置了许多框架的使用,用于敏捷,迅速开发基于Spring框架的应用程序

SpringBoot的优点:

  • 为所有的Spring开发者更快的入门
  • 开箱即用,提供各种默认配置,简化项目配置
  • 内嵌式简化web项目
  • 没有多余代码,和xml配置

c.微服务框架

将service变成小模块

一种架构风格,建成一系类小服务组合,可以通过http进行互通

缺点:给部署和运维带来难度

单体应用架构:易于开发和测试,修改维护困难

微服务架构:将独立的元素动态组合,节省资源,每个功能元素的服务都可以替换,升级

d.第一个SpringBoot程序

1.创建项目

原理:官方集成了****https://start.spring.io/这个spring的官网

使用idea创建,选择spring initalizr,依赖用spring web,Java8使用

2.创建包

需要Apllication的同级目录下

细节:

  • web层简化到只要写一个接口
  • 打包形成的jar包就是一个个服务,前后端分离,数据由后端写的 服务jar包接口提供,不需要依赖idea,本身就是一个可以执行的程序
  • 创建时包名可以只要前面部分

controller

@Controller
@RequestMapping("/hello")
public class HelloController {@GetMapping("/hello")@ResponseBodypublic String hello(){return "hello";}
}

application.properties

\#更改项目的端口号
server.port=8081

e.自动配置原理

pom.xml

  • spring-boot-dependencies : 核心依赖在在父工程中
  • 引入依赖时,不需要指定版本号,因为有这些版本仓库

启动器

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

springboot的启动场景

比如spring-boot-starter-web,会自动导入web的所有依赖

SpringBoot会将所有功能场景,变成一个个启动器,使用功能时,只要找到对应的启动器

主程序

//@SpringBootApplication标注这是一个SpringBoot项目
@SpringBootApplication
public class HellowWordApplication {//将springboot启动public static void main(String[] args) {SpringApplication.run(HellowWordApplication.class, args);}}

注解 ctrl+左键 进入注解

SpringBootConfiguration :springboot配置
Configuration spring配置
Component spring的一个组件

结论:SpringBoot的所有自动配置都是在启动时扫描并加载:spring.factories所有的自动配置类都在这里面,会以类名的方式将需要的组件添加到容器中,但是不一定会生效,要判断条件是否成立,只有导入了对应的starter ,有了对应的启动器,自动装配才会生效,才会配置成功,这样以前需要手动配置的东西,就自动配置好了;

f.主启动run方法

不只是运行了一个run方法,开启了一个服务

1.首先判断是一个普通项目还是web项目

2.推断并设置main方法的定义类,找到运行的主类

3.存在监听器,获取上下文,处理bean

g.yaml语法讲解

原理:自定义才能解决更多的功能,自动配置只能写个helloword

springboot使用全局的配置文件,配置文件是固定的

application.properties

语法:key=value

server.port=8081

applicaion.yaml

key:空格value

server
poet: 8081

语法结构

#普通的key=value
name: qingjiang
#对象
student:name: qingianage: 3
#行内写法
student1: {name: qinjiang,age: 15}
#数组
pets:-cat-dog-pig
pets1: [cat,dog,pig]
#可以注入到配置类中,值可以写到yaml中,Java类中可以为空

细节:

对空格的要求很高,空格表示层级关系
student:
name:age:表示student和name两个类,age.name age下的属性 

h.给属性赋值的几种方式

原理:

1.new

2.Spring方式@Value

类
@Component
public class Dog {@Value("旺财")private String name;@Value("3")private Integer age;
测试
class SpringBoot02ConfigApplicationTests {@Autowiredprivate Dog dog;@Testvoid contextLoads() {System.out.println(dog );}}

3.yaml直接注入

类注入,只要在spring中就能自动注入

为了确保能够扫描到类,需要加入@Component

person类

/**
将配置文件中的配置的每一个属性的值,一一映射到本类中,进行绑定。
只要SpringBoot容器中的组件才能使用@configurationProperties(prefix = "person")
不只是实体类中能使用,配置类config也可以
*/
@ConfigurationProperties(prefix = "person")

application.yaml

person:name: qiangage: 20happy: truebirth: 2001/9/06maps: {k1: v1,k2: v2}lists:-code-music-boydog:name: 旺财age: 3

扩展:

yaml中可以使用EI表达式

name: ${person.hello:hello}_旺财

i.JSR303验证

@Validated //数据校验
public class Person {@Email()

松散绑定

yaml中可以用first-name,可以与类中firstName对应,因为-后面是默认大写的

j.多环境配置及配置文件位置

优先级1234,默认的是优先级最低的

多环境切换

同一个项目中有不同的环境

application.yml

server:port: 8081
spring:profiles:active: dev
---
server:port: 8082
spring:profiles: dev
---
server:port: 8083
spring:profiles: test

k.自动配置原理再理解

SpringBoot程序中,在spring.factories有大量的自动配置类xxxconfiguration,当我们要写某个功能时,先判断功能有没有在自动配置类中,有的话然后看所需要的组件有没有在自动配置类,存在组件的话就不用手动配置,不存在需要手动添加组件,需要从properties自动配置类中获取某些属性,然后在yml配置文件中给属性赋值就行了

l.web开发探究

m.静态资源导入探究

创建应用前先写一个 controller,确保tomcat等一些配置能用

@RestController
public class HelloController {@RequestMapping("/hello")public String hello(){return "hello,world";}
}

看源码,需要下载

找类,双击shirt

if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");return;}addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {registration.addResourceLocations(this.resourceProperties.getStaticLocations());if (this.servletContext != null) {ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);registration.addResourceLocations(resource);}});
}

静态资源可以放在

  • resources目录下的public static(默认)resources和根目录/* 访问地址 localhost:8080/hello.js*
  • webjars 访问地址localhost:8080/webjars/
  • 优先级 resources>static>public

n.首页和图标定制

首页放在static resouces public都可

图标定制 版本更新,源码也会改变

o.thymeleaf模板引擎

模板引擎thymeleaf:其实jsp(后台查询数据发给jsp,jsp可以实现数据的显示,和交互)也是一个模板引擎,由于springboot使用war包和内嵌tomcat,不支持jsp,只能用纯静态页面,所以SpringBoot用thymeleaf代替jsp

thymeleaf官网:Thymeleaf 教程 - Thymeleaf | Docs4dev

模板引擎工作原理:按照data中的数据把hello${user}表达式解析,把数据填充到指定位置,然后把内容写出去

原理:首先需要模板引擎thymeleaf支持数据显示,并且在templates中的所有页面都只能通过Controller跳转,不能通过RestController

1.maven导入thymeleaf依赖

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

2.templates中写html页面

test.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>测试</title>
</head>
<body>
<!--html中的所有元素都被thymeleaf接管,th:元素名-->
<div th:text="${msg}"></div>
</body>
</html>

使用thymeleaf需要导入命名约束,方便提示

<html lang="en" xmlns:th="http://www.thymeleaf.org">

3.controller中设置接收请求,跳转页面

@GetMapping("/test")
public String test(Model model){model.addAttribute("msg","helloSpringboot");//通过前缀访问return "test";
}

4、访问即可

p.thymeleaf语法

model.addAttribute("msg", " <h1>springboot<h1>");
model.addAttribute("users", Arrays.asList("qiang","zhi"));<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
<div th:each="user:${users}" th:text="${user}"></div>

官方文档Thymeleaf 教程 - Thymeleaf | Docs4dev

q.mvc配置原理

狂神说SpringBoot12:MVC自动配置原理 - 遇见狂神说 - 博客园 (cnblogs.com)

自定义视图解析器:自己写一个视图解析器,注册到Bean中,就会自动装配

//扩展mvc配置视图解析器
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {//viewResolver 实现了视图解析器的类,我们就把它当做视图解析器,配置Bean,放在Spring中@Beanpublic ViewResolver myViewResolve(){return new MyViewResolver();}//自定义一个自己的视图解析器public static class MyViewResolver implements ViewResolver{@Overridepublic View resolveViewName(String viewName, Locale locale) throws Exception {return null;}}
}

在SpringBoot中有非常多xxxconfiguration帮助我们自动配置,只要我们看见了,就要注意了

员工管理系统

a.准备工作

细节:date导包需要导入util中

区分:类型Employee 对象employee 员工daoEmployeeDao

1.导入静态资源

细节:

  • 静态资源下载网站bootstrap模板 bootstrap模板免费下载 (chinaz.com)
  • css img js放在static下,html放在templates下

2.创建实体类

添加lombook依赖,为实体类的自动生成做准备

@Data

@AllArgsConstructor

@NoArgsConstructor

 <!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>

员工表

Employee

@Data
@NoArgsConstructor
public class Employee {private Integer id;private String lastName;private String email;private Integer gender; //性别 0 女, 1,男private Department department;private Date birth;public Employee(Integer id, String lastName, String email, Integer gender, Department department) {this.id = id;this.lastName = lastName;this.email = email;this.gender = gender;this.department = department;this.birth = new Date();}
}

细节:

  • date data的区别,date导包要是util下的
  • 可以查看目录结构
  • lombook依赖自动生成

部门表

Department

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {private Integer id;private String departmentName;
}

3.dao层创建临时数据库

DepartmentDao

/部门dao
//被spring托管
@Repository
public class DepartmentDao {//模拟数据库中的数据,初始化去加载private static Map<Integer, Department> departments = null;//创建一个库static {departments = new HashMap<Integer, Department>();//创建一个部门表departments.put(101, new Department(101, "教学部"));departments.put(102, new Department(102, "市场部"));departments.put(103, new Department(103, "教研部"));departments.put(104, new Department(104, "运营部"));departments.put(105, new Department(105, "后勤部"));}//获得所有部门的信息public Collection<Department> getDepartments(){return departments.values();}//通过id得到部门public Department getDepartmentById(Integer id){return departments.get(id);}
}

EmployeeDao

//员工dao
@Repository //被string托管
public class EmployeeDao{//模拟数据库中的数据private static Map<Integer, Employee> employees= null;//员工所属的部门@Autowiredprivate DepartmentDao departmentDao;static {employees = new HashMap<Integer, Employee>(); //创建一个部门表employees.put(1001,new Employee(  1001,"AA","1622840727@qq.com",1,new Department(101,"教学部")));employees.put(1002,new Employee(  1002,"BB","2622840727@qq.com",0,new Department(102,"市场部")));employees.put(1003,new Employee(  1003,"CC","4622840727@qq.com",1,new Department(103,"教研部")));employees.put(1004,new Employee(  1004,"DD","5628440727@qq.com",0,new Department(104,"运营部")));employees.put(1005,new Employee(  1005,"FF","6022840727@qq.com",1,new Department(105,"后勤部")));}//主键初始化private static Integer initId = 1006;//增加一个员工public void save(Employee employee){//主键自增if(employee.getId() == null){employee.setId(initId++);}//通过编号取部门,部门关联外键employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));//往employees中放employee,默认信息employees.put(employee.getId(),employee);}//查询全部员工信息public Collection<Employee> getAll(){return employees.values();}//通过id查询员工public Employee getEmployeeById(Integer id){return employees.get(id);}//删除员工public void delete(Integer id){employees.remove(id);}}

细节:

  • **外表部门注入,EmployeeDao和DepartmentDao需要@**Repository 被spring托管,,并且还要引入@Autowired
  • employees的用法
  • alt全选书写

b.首页实现

原理:扩展mvc,添加视图控制

1.扩展mvc,添加视图控制

MyMvcConfig.java

//扩展mvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {//添加视图控制@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");}
}

2.修改html的url路径,使用thymeleaf接管,@{}

<html lang="en"xmlns:th="http://www.thymeleaf.org">
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">

细节:

  • “/"代表项目的根目录,即resouces下,static是静态目录不需要写
  • 没改后端,只改前端,可以build就行,没改代码同样只要build
  • 模板thymeleaf存在缓存,可以通过application,yaml配置文职关闭缓存浏览器清除缓存
#关掉模板引擎的缓存
spring.thymeleaf.cache=false

c.页面国际化

需求:实现按钮的中英文的切换

1.修改idea中的文件编码,全部改为UTF-8

2.resouces文件下创建国际化的配置文件

  • 1.创建国际化配置文件

  • 2.资源包下进行赋值

3.识别国际化配置文件

原理:查看源码MessageSourceAutoConfiguration

application

#绑定识别国际化配置文件
spring.messages.basename=i18n.login

4.使用thymeleaf实现html下的输出 #{},就可以实现中文,即默认语言

<body class="text-center"><form class="form-signin" action="dashboard.html"><img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1><input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus=""><input type="password" class="form-control" th:placeholder="#{login.password}" required=""><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.btn}]]</button><p class="mt-5 mb-3 text-muted">© 2017-2018</p><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a><a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></form>

细节:

标签不同输出文本不同

 th:text="#{login.tip}[[#{login.remember}]]th:placeholder="#{login.password}

5.实现中英文的切换

原理:扩展MVC实现组件,并注入到Bean中,根据获取链接的参数zh_CN和en_US不同,spring自动配置时,就会实现中英文的切换

源码分析:官网找到MessageCodesResolver,在WebMvcAutoConfiguration查找localeResolver,然后调到AcceptHeaderLocalResolver分析可知需要实现LocaleResolver就行

AcceptHeaderLocalResolver

WebMvcAutoConfiguration

  1. index.html中英文按钮的链接

    <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
    

细节:

  • 符号‘ ’
  • thymeleaf中参数赋值可以用()代替jsp中的?username=xxx
  1. 扩展MVC实现国际化的组件,由源码分析可知只要实现LocaleResolver

原理:获取参数

public class MyLocaleResolver implements LocaleResolver {//解析请求@Overridepublic Locale resolveLocale(HttpServletRequest request) {//获取请求中的语言参数String language = request.getParameter("l");//如果没有就使用默认的Locale locale = Locale.getDefault();//如果请求的链接携带了国际化的参数if(!StringUtils.isEmpty(language)){//zh_CNString[] split = language.split("_");//国家,地区locale = new Locale(split[0], split[1]);}return locale;}@Overridepublic void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {}
}

bug分析判断有没有进去方法,可以写输出语句,输出language

//获取请求中的语言参数
String language = request.getParameter("l");
System.out.println("debug=="+language);

**3.**组件注入Spring容器中@Bean,就能实现自动配置

MyMvcConfig

//注入Bean使自定义国际化组件生效,实现自动配置
@Bean
public LocaleResolver localeResolver(){return new MyLocaleResolver();
}

总结

  1. 如果浏览器是中文就是中文,如果是英文就是英文,实现页面的语言,我们就需要国际化额配置文件,spring会自动使用默认的配置login.properties
  2. 如果要实现国际化中英文按钮的转换,我们还需要配置组件,并注入SpringBoot容器Bean中,实现点击不同的按钮获取获取不同的参数,spring就会自动配置,就可以完成中英文的切换

d.登录功能的实现

需求:点击登录,跳转到页面

1.改变index.html中表单中action的地址,controller实现跳转页面

index.html

//增加name属性
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
//修改action地址
<form class="form-signin" th:action="@{/user/login}">

2.controller实现页面跳转

LoginController

@Controller
public class LoginController {@RequestMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model){//具体业务if(!StringUtils.isEmpty(username) && "123456".equals(password)){return "redirect:/main.html";}else {//告诉用户,你登录失败了model.addAttribute("msg", "用户名或者密码错误!");return "index";}}
}

细节:

  • Model用于回显数据

    • @RequestParam(“username”)获取参数
    • redirect:main.html 重定向隐藏密码信息
    • @ResponseBody,需要删除,不然不能跳转视图

3.错误信息提示

  如果msg的值为空就正常访问--><p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

细节:

  • 前端添加东西,定位位置,F12审查元素
  • thymeleaf语法#string工具类的使用,判断是否为空

4.用户信息的隐藏

MyMvcConfig

registry.addViewController("/main.html").setViewName("dashboard");

LoginController

使用重定向隐藏信息
return "redirect:/main.html";

e.登陆拦截器

需求:只有点击登录才能进入mian.html,不能直接地址localhost:8080/mian.html访问,也就是要拦截main.html,只能通过登录成功才能进去,只要访问一次,就会保存到session中,下次可以直接访问

1.config中实现拦截器

LoginHandlerInterceptor

原理:实现HandlerInterceptor接口,完成拦截器,并以session的值作为判断条件

//通过实现HandlerInterceptor接口,实现拦截器
public class LoginHandlerInterceptor implements HandlerInterceptor {//实现preHandel放行方法,返回true放行,返回false不放行@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//登陆成功后,应该有用户的session,以此为条件判断是否登录成功Object  loginUser = request.getSession().getAttribute("loginUser");//没有权限,请先登录if(loginUser==null){request.setAttribute("msg", "没有权限,请登录");request.getRequestDispatcher("/index.html").forward(request, response);return false;}else {return true;}}
}

LoginController

原理:加入Session参数,如果登录给session赋值

@Controller
public class LoginController {@RequestMapping("/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Model model, HttpSession session){//具体业务if(!StringUtils.isEmpty(username) && "123456".equals(password)){//登录成功后给session赋值,判断是否登陆成功session.setAttribute("loginUser", username);return "redirect:/main.html";}else {//告诉用户,你登录失败了model.addAttribute("msg", "用户名或者密码错误!");return "index";}}
}

2.MVC配置中添加拦截器,重写方法addInterceptors

MyMvcConfig

@Override
//拦截"/**",不拦截"/index.html"等需要访问的资源和静态资源
public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/css/*","/js/**","/img/**");
}

3.输出公司名

dashboard.html

[[${session.loginUser}]]

f.展示员工列表

1.页面跳转

原理:点击员工管理,调用dao层,查询所有,返回给model,然后前端展示就行

@Controller
public class EmployeeController {@AutowiredEmployeeDao employeeDao;@RequestMapping("/emps")public String list(Model model){Collection<Employee> employees = employeeDao.getAll();model.addAttribute("emps", employees);return "emp/list";}
}
th:href="@{/emps}"

2.代码复用

templates下创建common包存放复用代码

list.html
<div th:replace="~{commons/commons::topbar}"></div>
<div th:replace="~{commons/commons::sidebar(active='list.html')}"></div>
dashboard.html
<div th:replace="~{commons/commons::topbar}"></div>
<div th:replace="~{commons/commons::sidebar(active='main.html')}"></div>

3.高亮

<a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/index.html}">
<a th:class="${active=='list.html'?'nav-link active':'nav-link'}" th:href="@{/emps}">

3.列表循环输出

list.html

<table class="table table-striped table-sm"><thead><tr><th>id</th><th>lastName</th><th>email</th><th>gender</th><th>department</th><th>birth</th><th>操作</th></tr></thead><tbody><tr th:each="emp:${emps}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastName()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==0?'女':'男'}"></td><td th:text="${emp.department.getDepartmentName()}"></td><td th:text="${#dates.format(emp.getBirth(),'yyyy-mm-dd HH:mm:ss')}"></td><td><button class="btn btn-sm btn-primary">编辑</button><button class="btn btn-sm btn-danger">删除</button></td></tr></tbody>
</table>

细节:

  • 三目表达书 ${emp.getGender()==0?‘女’:‘男’}
  • 外表 ${emp.department.getDepartmentName()}
  • 日期格式化 ${#dates.format(emp.getBirth(),‘yyyy-mm-dd HH:mm:ss’)}

g.增加员工实现

Controller

@GetMapping("/emp")public String toAddPage(Model model){//查出所有部门的信息Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments", departments);return "emp/add";}
@PostMapping("/emp")
public String addEmp(Employee employee){employeeDao.save(employee);return "redirect:/emps";
}

日期格式

日期格式转化

配置文件中

spring.mvc.date-format=yyyy-MM-dd

删除员工

h.修改员工信息

1.进入编辑页面,保留数据

//去员工的修改页面
@GetMapping("/emp/{id}")
public String toUpdateEmp(@PathVariable("id")Integer id,Model model){//查出原来的数据Employee employee = employeeDao.getEmployeeById(id);model.addAttribute("emp", employee);//查询部门信息Collection<Department> departments = departmentDao.getDepartments();model.addAttribute("departments", departments);return "emp/update";
}

2.修改数据

@PostMapping("/updateEmp")
public String updateEmp(Employee employee){employeeDao.save(employee);return "redirect:/emps";
}

i.404处理

springboot约定:templates下创建error包存放404.html就行

j.怎么写一个网站

k.整合jdbc

原理:底层采用springdate,拿到bean

1.配置数据库连接文件

<!--        JDBC--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<!--        web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
<!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>

application.yml

spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driver

2.使用

原理 :springboot自动配置,会自动产生对象

xxxtemplate,springBoot配置的模板,拿来即用

//返回字符串
@RestController
public class JDBCController {@AutowiredJdbcTemplate jdbcTemplate;//查询数据库的所有信息@GetMapping("/userList")public List<Map<String,Object>> userList(){String sql = "select * from user";List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);return list_maps;}@GetMapping("/addUser")public String addUser(){String sql = "insert into mybatis.user(id,name,pwd) values(4,'小明','123456')";jdbcTemplate.update(sql);return "updateadd_OK";}@GetMapping("/deleteUser/{id}")public String deleteUser(@PathVariable("id") int id){String sql = "delete from mybatis.user where id=?";jdbcTemplate.update(sql,id);return "updatedelete_OK";}@GetMapping("/updateUser/{id}")public String updateUser(@PathVariable("id") int id){String sql = "update mybatis.user  set name=?,pwd=?where id="+id;//封装Object[] objects = new Object[2];objects[0]="小明1";objects[1]="123";jdbcTemplate.update(sql,objects);return "updateUpdate_OK";}
}

数据源同样也能拿来用

@Autowired
DataSource dataSource;

l.数据源druid

DataSourceAutoConfigaration

查源码:默认是HIkari,但是可以通过Sping.datasource,指定type,改变数据源的类型

Druid优点:数据库连接池的实现,加入了日志监控,

Hikari优点:快

1.导入依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>    <groupId>com.alibaba</groupId><artifactId>druid</artifactId>   <version>1.1.21</version>
</dependency>

2.数据源使用配置参数

要在DataSource:的下一级

spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driver#指定数据源为druidtype: com.alibaba.druid.pool.DruidDataSource#Spring Boot 默认是不注入这些属性值的,需要自己绑定#druid 数据源专有配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: true#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入#如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4jfilters: stat,wall,log4jmaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

使用log4j需要导包

<!--        log4j--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency>

3.自定义容器,与参数进行绑定,将自定义的容器注入spring中

  • yml文件中配置的属性要注入到容器中,需要先注入,首先要注册Bean,然后加上

@ConfigurationProperties注解表示绑定成功

  • 因为springboot内置了servlet容器,没有web.xml,使用替代方法,配置监控器和过滤器ServletRegistrationBean
@Configuration
public class DruidConfig {@ConfigurationProperties(prefix = "spring.datasource")@Beanpublic DataSource druidDatasource(){return new DruidDataSource();}//注入Bean@Bean//后台监控,相当于为web.xml中的servlet配置,但是SpringBoot中没有,通过ServletRegistrationBean进行注册public ServletRegistrationBean statViewServlet(){//获取后台监控页面,固定死代码ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");//后台需要有人登录,账号密码配置HashMap<String,String> initParameters = new HashMap<>();//增加配置initParameters.put("loginUsername", "admin");//登录key固定的loginUsername loginPasswordinitParameters.put("loginPassword", "123456");//允许谁能访问initParameters.put("allow", "");//谁都能访问bean.setInitParameters(initParameters);//设置初始化参数return bean;}//filter过滤器@Beanpublic FilterRegistrationBean webStartFilter(){FilterRegistrationBean bean = new FilterRegistrationBean();bean.setFilter(new WebStatFilter());Map<String,String> initParameters = new HashMap<>();bean.setInitParameters(initParameters);initParameters.put("exclusions","*.js,*.css,/druid/*");return bean;}
}

j.整合mybatis

1.导入依赖,配置数据库连接信息文件

pom.xml

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version>
</dependency>

application.properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTIme=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#整合mybatis,能被spring识别,确保User能识别,Mapper.xml文件能识别
mybatis.type-aliases-package=com.qiang.pojo
mybatis.mapper-locations=classpath:mybatis/Mapper/*.xml

appilation.yml

spring:datasource:username: rootpassword: 123456url: jdbc:mysql:/localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivermybatis:type-aliases-package: com.qiang.pojomapper-locations: claapath:mybatis/Mapper/*.xml

2.实体类

User

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {private int id;private String name;private String pwd;
}

导入lombok依赖

pom.xml

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>

3.Mapper接口

UserMapper

//表示是mybatis下的Mapper注解
@Mapper
@Repository//被spring整合
public interface UserMapper {List<User> queryUserList();User queryUserById(int id);int addUser(User user);int updateUser(User user);int deleteUser(int id);
}

细节:

  • 注解写全@Mapper @Repository
  • 是接口

4.resource目录下创建Mapper.xml

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qiang.Mapper.UserMapper"><select id="queryUserList" resultType="User">select * from user</select><select id="queryUserById" resultType="User">select * from user where id = #{id}</select><insert id="addUser" parameterType="User">insert into user (id,name,pwd) values (#{id},#{name},#{pwd})</insert><update id="updateUser" parameterType="User">update user set name=#{name},pwd=#{pwd} where id = #{id}</update><delete id="deleteUser" parameterType="int">delete from user where id = #{id}</delete>
</mapper>

5.Controller 前后端通过controller连接传递数据,分为三层架构

@RestController
public class UserController {@Autowiredprivate UserMapper userMapper;@GetMapping("/queryUserList")public List<User> queryUserList(){List<User> userList = userMapper.queryUserList();return userList;}@GetMapping("/queryUserById")public User queryUserById(){User user = userMapper.queryUserById(3);return user;}@GetMapping("/addUser")public String addUser(){userMapper.addUser(new User(1,"小谢","987"));return "OK";}@GetMapping("/updateUser")public String updateUser(){userMapper.updateUser(new User(1,"小谢","654"));return "OK";}@GetMapping("/deleteUser")public String deleteUser(){userMapper.deleteUser(1);return "OK";}
}

pom.xml

依赖

<!--        web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
<!--        lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
<!--        mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><!--thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
<!--jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
<!--        mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
<!--        test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
  • model对象的addAttribute是将要添加的属性添加到RequestScope中,即添加到请求域中。
  • 当使用return进行跳转页面,默认是跳转到templates下的页面,请求栏地址不会改变,并且会携带请求参数里的参数,Map, List, 数组, Model对象都可以携带。
  • return "forward:…“是进行请求转发,可以在”/emp/{id}“GET请求中再请求”/emp"GET请求,最终请求域中的数据既有 emp,又有dept。
  • return "redirect:…"是进行重定向

ng-boot-starter-web

    <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
    <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><!--thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>
    <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

- **model对象的addAttribute是将要添加的属性添加到RequestScope中,即添加到请求域中。**
- **当使用return进行跳转页面,默认是跳转到templates下的页面,请求栏地址不会改变,并且会携带请求参数里的参数,Map, List, 数组, Model对象都可以携带。**
- **return "forward:…“是进行请求转发,可以在”/emp/{id}“GET请求中再请求”/emp"GET请求,最终请求域中的数据既有 emp,又有dept。**
- **return "redirect:…"是进行重定向**

狂神springboot相关推荐

  1. 狂神SpringBoot学习笔记12天-Day 06 基于SpringBoot的员工管理系统

    6.基于SpringBoot的员工管理系统 写在前面 参考CSDN博主Baret-H 原文链接(77条消息) 狂神Spring Boot 员工管理系统 超详细完整实现教程(小白轻松上手~)_Baret ...

  2. 狂神-SpringBoot笔记-总

    给大家推荐一款好用的CSDN云服务,新人首购折扣哦,点击下图跳转: [笔记整理来源 B站狂神说https://www.bilibili.com/video/BV1PE411i7CV] SpringBo ...

  3. 狂神springboot员工管理系统

    1.创建springboot项目,添加web依赖 只能添加web依赖,不能添加jdbc,mysql,不然添加数据库配置 测试tomcat @RestController public class He ...

  4. springboot的使用!

    首先了解下什么是springboot 是可以用于搭建spring应用的脚手架的框架. 我自己觉得的比较明显的好处: 配置很少就可以用了 导入一个启动器会导入很多依赖,方便省事啊 springboot对 ...

  5. 结合大学四年经验,带你揭秘高效自学Java的方法和路线(从认识、方法、反馈3个角度出发)

    首发公众号:bigsai 收藏不是真支持,点赞.关注三连才是真支持! 前言 大家好,我是bigsai 好久不见,甚是想念,文章同时收录在回车课堂.本文就自学Java的n个问题和n个方法做出一些建议,并 ...

  6. SpringBoot2基础篇

    目录 前言 从今天开始进入微服务阶段 一. HelloWorld 1.1.什么是SpringBoot 1.3.微服务架构 二.第一个SpringBoot程序 2.1.环境配置 2.2.创建基础项目说明 ...

  7. 跟着狂神学SpringCloud(Rest环境搭建+Eureka服务注册与发现+ribbon+Feign负载均衡+Hystrix+服务熔断+Zuul路由网关+SpringCloud config分布)

    跟着狂神学SpringCloud SpringCloud 回顾之前的知识- JavaSE 数据库 前端 Servlet Http Mybatis Spring SpringMVC SpringBoot ...

  8. springdata数据源的配置

    对于数据访问层,无论是nosql还是sql数据库,SpringBoot的底层都是用Springdata去统一处理 SpringBoot:Springdata的官方文档地址: https://sprin ...

  9. swagger:快速入门

    swagger 学习视频链接:小狂神Springboot 每日格言 贵在坚持.难在坚持.成在坚持. 学习目标: 了解Swagger的作用和概念 了解前后端分离 在SpringBoot中集成Swagge ...

最新文章

  1. 线性代数投影法在线性回归中的应用
  2. 未来哲学的六个问题域
  3. UVa11464 - Even Parity(递推法)
  4. 清除XCode缓存和生成文件
  5. vue index.html环境变量,vue-cli环境变量与模式
  6. [转载][QT][SQL]sql学习记录3_sqlite之update delete like
  7. php 强制刷新一次,强制浏览器使用PHP刷新所有内容
  8. Shell数组操作(添加、删除元素、清空、遍历)
  9. FMS 中文帮助 (下载)
  10. CentOS 6.9/7通过yum安装指定版本的Redis
  11. 5G手机是否值得买?北京5G基站将超1.4万个
  12. Zend Studio12.0配置Xdebug
  13. Swift iOS : Core Data
  14. vs2012新建项目时出现错误提示框解决办法
  15. PYTHON处理MOD/MYD11(LST)产品
  16. JEP 379:将低暂停时间的垃圾收集器Shenandoah推向生产
  17. contiki编程(一)
  18. 呼叫系统管理服务器图片,电话呼叫中心系统架构,电话呼叫系统软件
  19. android10加载图片问题解决思路全程记录 FileNotFoundException: /content:/media/external/images
  20. qrcode生成二维码

热门文章

  1. 流氓迅雷,永别了万恶的迅雷,永别了万恶的迅雷播放组件
  2. 使用Origin绘制边际分布曲线(Marginal distributions plots)的思路和方法
  3. 联想hx系列服务器,联想ThinkAgile HX系列 融合
  4. 前端学习——vue基础知识
  5. Mixly16:震动报警器
  6. r语言list 转换成 vector
  7. 支持nfc的android手机,NFC手机有哪些 2017支持NFC功能的手机推荐 (5)
  8. Python发送邮件实例
  9. 多电脑共享键鼠,传输数据,共享剪贴板
  10. c 语言单片机开发,单片机C语言开发离不开它