1.不需要多余的配置文件信息
1 | application.properties |
Dao层mapper 添加注解 @Repository
1 | @Repository |
mapper.xml文件
1 | <?xml version="1.0" encoding="UTF-8" ?> |
Service层只需要一个service类即可 不需要一个接口一个实现类 添加注解
1 | @Service |
启动类添加注解 @MapperScan(“com.mxxd.SCM.Dao”)自动扫描Dao层mapper
1 | @SpringBootApplication |
2.前端不推荐jsp,推荐thymeleaf或freemarker
使用freemarker
pom.xml文件添加依赖
1 | <dependency> |
前端使用 .ftl 文件
在resources文件夹下创建两个目录:static 和 template
static:目录下创建css、js、img三个目录,存放静态资源文件
template:目录下存放 XX.ftl 文件
在com.mxxd.SCM目录下创建一个Conf目录,配置springboot的静态资源文件目录
1 | @Configuration |
在 ftl 文件中如下引用
如果要引用其他页面,使用 <#include />
1 | <#include "header.ftl" encoding="UTF-8"/> |
3.启动项目必须启动XXXApplication启动类
不能进行单元测试!!! 因为需要等SpringBoot把所有的配置全部编译完成之后才能运行,不然会找不到BeanFactory
4.测试controller层调用service调用mapper连接数据库返回值是否正确
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/*
* 启动之后访问 /user/test 会自动跳转到index.ftl
* 如果直接访问index.ftl是无法访问成功的
* 因为ftl文件是一个模板文件,需要经过后台跳转才能进行渲染成网页
*/
@RequestMapping("/test")
public String home(){
return "index";
}
@RequestMapping("/home")
public ModelAndView login() {
UserEntity userEntity = userService.login("admin", "admin");
ModelAndView mv = new ModelAndView();
if (userEntity == null) {
mv.addObject("message", "用户名或密码错误,请重新输入!");
mv.setViewName("index");
} else {
mv.addObject("user", userEntity);
mv.setViewName("index");
}
System.out.println(userEntity);
System.out.println(mv.getModel());
System.out.println(mv.getViewName());
return mv;
}
}
直接在页面上进行测试,@Test 使用会报空指针错误
添加 @ResponseBody 注解不用返回页面,直接打印输出结果
前端直接使用 ${user.name} 获取user里name属性值 ${user}获取user整个对象值