码云私人令牌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")
@ResponseBody
public 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: qingian
age: 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 {
@Autowired
private Dog dog;
@Test
void contextLoads() {
System.out.println(dog );
}
}
3.yaml直接注入
类注入,只要在spring中就能自动注入
为了确保能够扫描到类,需要加入@Component
person类
/**
将配置文件中的配置的每一个属性的值,一一映射到本类中,进行绑定。
只要SpringBoot容器中的组件才能使用@configurationProperties(prefix = "person")
不只是实体类中能使用,配置类config也可以
*/
@ConfigurationProperties(prefix = "person")
application.yaml
person:
name: qiang
age: 20
happy: true
birth: 2001/9/06
maps: {k1: v1,k2: v2}
lists:
-code
-music
-boy
dog:
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中
@Bean
public ViewResolver myViewResolve(){
return new MyViewResolver();
}
//自定义一个自己的视图解析器
public static class MyViewResolver implements ViewResolver{
@Override
public 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;
//员工所属的部门
@Autowired
private 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 {
//添加视图控制
@Override
public 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
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
- 扩展MVC实现国际化的组件,由源码分析可知只要实现LocaleResolver
原理:获取参数
public class MyLocaleResolver implements LocaleResolver {
//解析请求
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数
String language = request.getParameter("l");
//如果没有就使用默认的
Locale locale = Locale.getDefault();
//如果请求的链接携带了国际化的参数
if(!StringUtils.isEmpty(language)){
//zh_CN
String[] split = language.split("_");
//国家,地区
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public 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();
}
总结
- 如果浏览器是中文就是中文,如果是英文就是英文,实现页面的语言,我们就需要国际化额配置文件,spring会自动使用默认的配置login.properties
- 如果要实现国际化中英文按钮的转换,我们还需要配置组件,并注入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不放行
@Override
public 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 {
@Autowired
EmployeeDao 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: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
2.使用
原理 :springboot自动配置,会自动产生对象
xxxtemplate,springBoot配置的模板,拿来即用
//返回字符串
@RestController
public class JDBCController {
@Autowired
JdbcTemplate 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: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
#指定数据源为druid
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: 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")
@Bean
public 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 loginPassword
initParameters.put("loginPassword", "123456");
//允许谁能访问
initParameters.put("allow", "");//谁都能访问
bean.setInitParameters(initParameters);//设置初始化参数
return bean;
}
//filter过滤器
@Bean
public 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: root
password: 123456
url: jdbc:mysql:/localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.qiang.pojo
mapper-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 mapper
PUBLIC "-//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 {
@Autowired
private 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:…"是进行重定向**