回顾Java知识点,面试题汇总Day15(持续更新)
Spring手动配置web.xml、spring.xml、springmvc.xml、config.xmlSpring Boot 不需要进行任何通用配置不需要任何的XML文件自动配置。Spring Boot自动装配一、项目创建测试Spring Boot Spring MVC MyBatis1.创建项目File-新建-项目Java:17 最低版本JDK最低需要17才能适配。我目前有JDK26就选择了JDK26.点击“创建”,就创建了新项目。thymeleaf 前端模板html2.项目测试controller、entity、mapper同ssm中的pom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.5.14/version relativePath/ !-- lookup parent from repository -- /parent groupIdorg.dyz/groupId artifactIdmyspringboot/artifactId version0.0.1-SNAPSHOT/version namemyspringboot/name descriptionmyspringboot/description url/ licenses license/ /licenses developers developer/ /developers scm connection/ developerConnection/ tag/ url/ /scm properties java.version17/java.version /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter/artifactId version3.0.5/version /dependency dependency groupIdcom.mysql/groupId artifactIdmysql-connector-j/artifactId scoperuntime/scope /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency dependency groupIdorg.mybatis.spring.boot/groupId artifactIdmybatis-spring-boot-starter-test/artifactId version3.0.5/version scopetest/scope /dependency /dependencies build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId configuration excludes exclude groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /exclude /excludes /configuration /plugin plugin groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId executions execution iddefault-compile/id phasecompile/phase goals goalcompile/goal /goals configuration annotationProcessorPaths path groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /path /annotationProcessorPaths /configuration /execution execution iddefault-testCompile/id phasetest-compile/phase goals goaltestCompile/goal /goals configuration annotationProcessorPaths path groupIdorg.projectlombok/groupId artifactIdlombok/artifactId /path /annotationProcessorPaths /configuration /execution /executions /plugin /plugins resources resource directorysrc/main/java/directory includes include**/*.xml/include /includes /resource /resources /build /projectapplication.ymlspring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/car_rental_separate username: root password: 123456index.html!DOCTYPE html html xmlns:thhttp://www.thymeleaf.org html langen head meta charsetUTF-8 titleTitle/title /head body table border1 tr tdID/td td标题/td td内容/td td创建时间/td td操作员/td td操作/td /tr tr th:eachnews:${list} td th:text${news.id}/td td th:text${news.title}/td td th:text${news.content}/td td th:text${news.createtime}/td td th:text${news.opername}/td td a href/get?id${news.id}编辑/a a href/delete?id${news.id}删除/a /td /tr /table /body /html启动类自动生成。加入MapperScan(com.dyz.mapper)package com.dyz; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication MapperScan(com.dyz.mapper) public class MyspringbootApplication { public static void main(String[] args) { SpringApplication.run(MyspringbootApplication.class, args); } }启动测试 localhost:8080/list二、CRUD不能直接通过url访问templates目录thymeleaf必须通过controller解析后才能获取index.htmla href/add/aHelloControllerRequestMapping(/add) public String add(){ return add; }表示跳转到add页面。CRUD实现index.html!DOCTYPE html html xmlns:thhttp://www.thymeleaf.org html langen head meta charsetUTF-8 titleTitle/title /head body a href/add添加/a !--get的方式-- table border1 tr tdID/td td标题/td td内容/td td创建时间/td td操作员/td td操作/td /tr tr th:eachnews:${list} td th:text${news.id}/td td th:text${news.title}/td td th:text${news.content}/td td th:text${news.createtime}/td td th:text${news.opername}/td td !--字符串拼接-- a th:href{/get?id ${news.id}}编辑/a !--th:href {} -- !-- a th:href{/get(id${news.id})}编辑/a-- a th:href{/delete?id ${news.id}}删除/a /td /tr /table /body /htmledit.html!DOCTYPE html html xmlns:thhttp://www.thymeleaf.org html langen head meta charsetUTF-8 titleTitle/title /head body form actionupdate methodpost table tr tdID: /td td input typetext nameid th:value${news.id} readonly /td /tr tr tdtitle: /td td input typetext nametitle th:value${news.title} /td /tr tr tdcontent: /td td input typetext namecontent th:value${news.content} /td /tr tr tdcreatetime: /td td input typetext namecreatetime th:value${news.createtime} readonly /td /tr tr tdopername: /td td input typetext nameopername th:value${news.opername} /td /tr tr td input typesubmit name提交 /td td input typereset name重置 /td /tr /table /form /body /htmladd.html!DOCTYPE html html langen head meta charsetUTF-8 titleTitle/title /head body form action/add methodpost table tr tdTitle:/td td input typetext nametitle /td /tr tr tdcontent: /td td input typetext namecontent /td /tr tr tdopername: /td td input typetext nameopername /td /tr tr td input typesubmit name提交 /td td input typereset name重置 /td /tr /table /form /body /html实体类 Newspackage com.dyz.entity; import lombok.Data; import java.util.Date; Data public class News { private int id; private String title; private String content; private Date createtime; private String opername; }NewsMapperpackage com.dyz.mapper; import com.dyz.entity.News; import java.util.List; public interface NewsMapper { public ListNews list(); public void add(News news); public News getById(Integer id); public void update(News news); public void deleteById(Integer id); }NewsMapper.xml?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.dyz.mapper.NewsMapper select idlist resultTypecom.dyz.entity.News select * from sys_news /select insert idadd parameterTypecom.dyz.entity.News insert into sys_news(title,content,createtime,opername) values (#{title},#{content},#{createtime},#{opername}) /insert select idgetById parameterTypejava.lang.Integer resultTypecom.dyz.entity.News select * from sys_news where id #{id} /select select idupdate parameterTypecom.dyz.entity.News update sys_news set title #{title},content#{content},opername #{opername} where id ${id} /select select iddeleteById parameterTypecom.dyz.entity.News delete from sys_news where id #{id} /select /mapperxml文件可以不单独写使用注解在NewsMapper中生成SQLHelloControllerpackage com.dyz.controller; import com.dyz.entity.News; import com.dyz.mapper.NewsMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date; import java.util.List; Controller public class HelloController { Autowired private NewsMapper newsMapper; RequestMapping(/list) public String List(Model model){ ListNews list this.newsMapper.list(); model.addAttribute(list,list); return index; } GetMapping(/add) //网页跳转 public String add(){ return add; } PostMapping(/add) //add方法 public String add(News news){ news.setCreatetime(new Date()); this.newsMapper.add(news); return redirect:/list; } GetMapping(/get) public String get(Model model,Integer id){ News news this.newsMapper.getById(id); model.addAttribute(news,news); return edit; } PostMapping(/update) public String update(News news){ this.newsMapper.update(news); return redirect:/list; } GetMapping(delete) public String delete(Integer id){ this.newsMapper.deleteById(id); return redirect:/list; } }三、Thymeleaftext用于显示文本信息if用来判断内容是否显示。条件范围内的显示不在条件范围内的不显示。!DOCTYPE html html langen head meta charsetUTF-8 titleTitle/title /head body h1 th:text${name}/h1 h1 th:if${score 90}优秀/h1 h1 th:if${score 90 score 80}良好/h1 /body /htmlGetMapping(/test) public String index(Model model){ model.addAttribute(name,Tom); model.addAttribute(score,82); return /test; }each用来遍历集合tr th:eachnews:${list} td th:text${news.id}/td td th:text${news.title}/td td th:text${news.content}/td td th:text${news.createtime}/td td th:text${news.opername}/td td !--字符串拼接-- a th:href{/get?id ${news.id}}编辑/a !--th:href {} -- !-- a th:href{/get(id${news.id})}编辑/a-- a th:href{/delete?id ${news.id}}删除/a /td /trRequestMapping(/list) public String List(Model model){ ListNews list this.newsMapper.list(); model.addAttribute(list,list); return index; }value 用来给标签赋值GetMapping(/get) public String get(Model model,Integer id){ News news this.newsMapper.getById(id); model.addAttribute(news,news); return edit; }tr tdopername: /td td input typetext nameopername th:value${news.opername} /td /tr四、application配置文件Spring Boot工程项目的配置文件名称必须是application有两种不同的格式分别是properties和yaml,如果两种格式同时存在properties的优先级更高。application.propertiesserver.port8082 spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver spring.datasource.urljdbc:mysql://localhost:3306/car_rental_separate spring.datasource.usernameroot spring.datasource.password123456application.ymlserver: port: 8081 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/car_rental_separate username: root password: 123456