源码:https://github.com/Morty123456/SpringBoot-Mysql.git 参考教程:https://www.bilibili.com/video/av20965295
1:配置pom文件
需要导入的依赖包括:
thymleaf web jquery
-webjar Druid mybatis
log4j(不导入,druid会报错
)
2:配置yml
主要包括
国际化配置(登录界面中英文切换)模板引擎的缓存设置日期格式设置数据库连接信息设置mybatis设置信息
mybatis设置可以把所有的查询语句放在resource文件夹下
3:静态文件
主要包括css、js、和html使用thymleaf模板引擎
主要是使用了th来进行赋值、判断、循环等
4:构造实体类(entities)
按照数据库设计,构键employee,department,user三个实体类Alt+Insert快捷键构造get,set,toString方法
5:构造mapper类
mapper用来操作数据库,所有的mapper类都要有@Mapper注解,或者在main函数处添加 @MapperScan注解,参数写mapper文件夹的路径,这样做是配置扫描所有的此路径下的mapper详细代码见源文件
6:构建controller
类文件前面要有@Controller注解用到的Mapper需要有@Autowired注解,@Autowired是用在JavaBean中的注解,通过byType形式,用来给指定的字段或方法注入所需的外部资源,能减少或者消除属性或构造器参数的设置进行业务层的操作
通过model传值到页面
@GetMapping("/emps")
public String
list(Model model
){
Collection
<Employee> employees
= employeeMapper
.getAllEmployee();
model
.addAttribute("emps", employees
);
return "emp/list";
}
在html获取值
<tbody>
<tr th
:each
="emp:${emps}">
<td th
:text
="${emp.id}">1</td
>
<td th
:text
="${emp.lastName}">1</td
>
<td th
:text
="${emp.email}">1</td
>
<td th
:text
="${emp.gender}">1</td
>
<td th
:text
="${emp.department}">1</td
>
<td th
:text
="${#dates.format(emp.birth,'yyyy-MM-dd HH:mm:ss')}">1</td
>
</tr
>
</tbody
>
controller的路由地址和thymleaf中的匹配,就可以跳转。用@PathVariable蝴蝶请求中的参数
@DeleteMapping("/emp/{id}")
public String
deleteEmployee(@PathVariable("id") Integer id
){
employeeMapper
.deleteEmployee(id
);
return "redirect:/emps";
}
遇到的问题
删除员工时报错
Request method
'POST' not supported
为啥咱也有点不知道,好像是版本问题 修改pom的<modelVersion>4.0.0</modelVersion>版本号为2.0.0,我的之前是4.0.0