Sping Data 官网:https://spring.io/projects/spring-data
数据库相关的启动器 :可以参考官方文档:
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
配置文件application.yamlorg.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter org.thymeleaf thymeleaf-spring5 org.thymeleaf.extras thymeleaf-extras-java8time log4j log4j 1.2.17 com.alibaba druid 1.1.21
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
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
banner.txt(springboot启动项目的图标)
命名为banner.txt文件,放在resources目录下
* * * *
* *
* * ___
* * | | |
* _________## * / | |
@\\\\## * | |--o|===|-|
* @@@\\\\## |/|/ |---| |d|
@@ @@\\\\\ |\|//|/ * / |w|
* @@@@@@@\\\\\ ||/|/ | U | |b|
@@@@@@@@@----------| \|// | S |=| |
__ @@ @@@ @@__________| |/ | A | | |
____|_@|_ @@@@@@@@@__________| |/ |_______| |_|
=|__ _____ |= @@@@ .@@@__________| | |@| |@| | |
____0_____0__|/__@@@@__@@@__________|_|/__|___|/__|/___________|_|_
-- 北纬
实体类Employee.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Employee {
private Integer id;
private String name;
private String pwd;
private String email;
private int sex;// 0:女 1:男
private String departmentID;
private Department department;
private Date birth;
}
mapper接口EmployeeMapper.java
@Repository
@Mapper
public interface EmployeeMapper {
//查询所有员工
public List selectAllEmp();
//添加员工信息
public void addEmp(Employee employee);
//删除员工信息
public void deleteEmp(int id);
//通过id查询员工信息
public Employee selectEmpById(int id);
//更新员工信息
public void updateEmp(Employee employee);
}
映射文件EmployeeMapper.xml
业务层接口EmployeeService.java
public interface EmpService {
//查询所有员工
public List selectAllEmp();
//添加员工信息
public void addEmp(Employee employee);
//删除员工信息
public void deleteEmp(int id);
//通过id查询员工信息
public Employee selectEmpById(int id);
//更新员工信息
public void updateEmp(Employee employee);
}
业务层实现类EmployeeServiceImpl.java
@Service("empService ")
public class EmpServiceImpl implements EmpService {
@Autowired
EmployeeMapper employeeMapper;
@Override
public List selectAllEmp() {
return employeeMapper.selectAllEmp();
}
@Override
public void addEmp(Employee employee) {
employeeMapper.addEmp(employee);
}
@Override
public void deleteEmp(int id) {
employeeMapper.deleteEmp(id);
}
@Override
public Employee selectEmpById(int id) {
return employeeMapper.selectEmpById(id);
}
@Override
public void updateEmp(Employee employee) {
employeeMapper.updateEmp(employee);
}
}
控制层EmployeeController.java
@Controller
@ComponentScan(basePackages = {"com.example.helloword.config"})
public class EmployeeController {
@Autowired
EmployeeMapper employeeMapper;
@Autowired
DepartmentSerive departmentSerive;
@Autowired
EmpService empService;
//查询所有的员工信息
@GetMapping("/emps")
public String queryEmp(Model model){
List empList = employeeMapper.selectAllEmp();
model.addAttribute("emps",empList);
for(Employee employee:empList){
System.out.println("遍历employee:"+employee);
System.out.println("遍历employee.getDepartmentID():===>"+employee.getBirth());
}
return "emp/list";
}
//查询部门号
@GetMapping("/emp")
public String selectDepartment(Model model){
List departments = departmentSerive.selectAllDepartment();
for(Department department:departments){
System.out.println("遍历department:====="+department);
}
model.addAttribute("departments",departments);
return "emp/add";
}
//添加员工信息
@PostMapping("/emp")
public String addEmp(HttpServletRequest request,Employee employee){
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String birth = request.getParameter("birth");
String departmentid = request.getParameter("department.id");
System.out.println("密码:"+pwd+";用户名:"+name+";出生日期:"+birth+";departmentid:"+departmentid);
employeeMapper.addEmp(employee);
System.out.println("添加数据成功!");
return "redirect:/emps";
}
//删除员工信息
@GetMapping("/deleteEmp/{id}")
public String deleteEmp(@PathVariable("id") int id){
empService.deleteEmp(id);
System.out.println("删除员工数据成功!");
return "redirect:/emps";
}
//修改员工页面
@GetMapping("/updateEmp/{id}")
public String updateEmp(@PathVariable("id")int id,Model model){
Employee employee = employeeMapper.selectEmpById(id);
System.out.println("Did:"+employee.getDepartmentID());
model.addAttribute("employee",employee);
return "emp/update";
}
@PostMapping("/updateEmp")
public String updateEmp(Employee employee){
empService.updateEmp(employee);
return "redirect:/emps";
}
}
添加了thymeleaf模板引擎并启动IndexController控制器就可以跳转到index.html页面了
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//配置服务器启动自动跳转到index.html页面中
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//自定义的国际化组件
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login","/css/**","/js/**","/img/**");
}
}
LoginHandlerInterceptor.java
public class LoginHandlerInterceptor implements HandlerInterceptor {
@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;
}
}
}
Thymeleaf模板引擎的使用
Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf
Spring官方文档:找到我们对应的版本
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
Thymeleaf 语法学习org.springframework.boot spring-boot-starter-thymeleaf
要学习语法,还是参考官网文档最为准确,我们找到对应的版本看一下;
Thymeleaf 官网:https://www.thymeleaf.org/ , 简单看一下官网!我们去下载Thymeleaf的官方文档!
- 我们做个最简单的练习 :我们需要查出一些数据,在页面中展示
@RequestMapping("/t1")
public String test1(Model model){
//存入数据
model.addAttribute("msg","Hello,Thymeleaf");
//classpath:/templates/test.html
return "test";
}
- 我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。
- 我们可以去官方文档的#3中看一下命名空间拿来过来:
xmlns:th="http://www.thymeleaf.org"
- 我们去编写下前端页面
狂神说
测试页面
- 我们编写一个Controller,放一些数据
@RequestMapping("/t2")
public String test2(Map map){
//存入数据
map.put("msg","Hello");
map.put("users", Arrays.asList("qinjiang","kuangshen"));
//classpath:/templates/test.html
return "test";
}
- 测试页面取出数据
狂神说
测试页面
[[${user}]]
拓展SpringMVC
修改SpringBoot的默认配置
这么多的自动配置,原理都是一样的,通过这个WebMVC的自动配置原理分析,我们要学会一种学习方式,通过源码探究,得出结论
SpringBoot的底层,大量用到了这些设计细节思想,所以,没事需要多阅读源码!得出结论;
SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(如果用户自己配置@bean),如果有就用用户配置的,如果没有就用自动配置的;
如果有些组件可以存在多个,比如我们的视图解析器,就将用户配置的和自己默认的组合起来!



