目录:
POM.xml
4.0.0
com.ali
springBoot
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.7.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
org.springframework.boot
spring-boot-starter-thymeleaf
Application启动类:
@SpringBootApplication
@MapperScan(basePackages = "com.ali.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
pojo:
public class TbDept implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
mapper:
@Repository
public interface DeptMapper {
List findList();
}
service:
@Service
public class DeptService {
@Autowired
private DeptMapper deptMapper;
public List findList(){
return deptMapper.findList();
}
}
controller:
@Controller
public class DemoController {
@Autowired
private DeptService deptService;
@RequestMapping("findList")
@ResponseBody
public List findList(){
return deptService.findList();
}
@RequestMapping("findAll")
public String findAll(ModelMap modelMap){
List list = deptService.findList();
modelMap.addAttribute("list",list);
modelMap.addAttribute("msg","hello");
return "haha";
}
}
拦截器:
public class LoginInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
logger.debug("preHandle method is now running!");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
logger.debug("postHandle method is now running!");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
logger.debug("afterCompletion method is now running!");
}
}
config:
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Bean
public LoginInterceptor loginInterceptor(){
return new LoginInterceptor();
}
//添加拦截器
public void addInterceptors(InterceptorRegistry registry) {
// 通过registry来注册拦截器,通过addPathPatterns来添加拦截路径
registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**");
}
}
resource中的资源:
mappers:
DeptMapper.xml:
templates:中主要放置themleaf的html页面:
//其中要有xmlns:th="http://www.thymeleaf.org"
haha
我是h1
1
admin
88
application.properties:为springBoot默认使用的配置文件
#修改端口
server.port=9001
# 连接四大参数
spring.datasource.url=jdbc:mysql://localhost:3306/miu
spring.datasource.username=root
spring.datasource.password=root
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#设置日志级别
logging.level.com.ali=debug
# mybatis 别名扫描
mybatis.type-aliases-package=com.ali.pojo
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml
#禁用thyemelaaf 模板缓存
spring.thymeleaf.cache=false
static中存放静态图片