发表于: 2019-10-28 21:13:32
1 959
今天做了什么:
学习spring boot
初期的Spring通过代码加配置的形式为项目提供了良好的灵活性和扩展性,但随着Spring越来越庞大,其配置文件也越来越繁琐,太多复杂的xml文件也一直是Spring被人诟病的地方,特别是近些年其他简洁的WEB方案层出不穷,如基于Python或Node.Js,几行代码就能实现一个WEB服务器,对比起来,大家渐渐觉得Spring那一套太过繁琐,此时,Spring社区推出了Spring Boot,它的目的在于实现自动配置,降低项目搭建的复杂度
spring与spring boot区别:
1.Spring Boot可以建立独立的Spring应用程序;
2.内嵌了如Tomcat,Jetty和Undertow这样的容器,也就是说可以直接跑起来,用不着再做部署工作了。
3.无需再像Spring那样搞一堆繁琐的xml文件的配置;
4.可以自动配置Spring;
5.提供了一些现有的功能,如量度工具,表单数据验证以及一些外部配置这样的一些第三方功能;
6.提供的POM可以简化Maven的配置;
Spring Boot提供了默认配置的模板引擎主要有以下几种:
Thymeleaf
FreeMarker
Velocity
Groovy
Mustache
SpringBoot的Demo:
一个SpringBoot基础项目的结构:
分别是程序入口,配置文件和测试入口
建立一个controller:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(){
System.out.println("Doing something ...");
return "First spring boot demo";
}
}
运行主程序,访问localhost:8080:
Spring Boot项目构建起来很迅速,运行起来也只要两秒不到.用过spring boot后就再也不想用原始Spring了.
spring boot + mybatis:
在application.properties中写数据库配置.
可以直接在mapper接口上写sql语句:
package com.jnshu.dao;
import com.jnshu.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("select count(status) from students where status = #{status}")
int countStudent(int status);
@Select("select * from students")
List<Student> findStudent();
@Select("select count(status) from students where status = 0 and major_id = #{id}")
int countByMI(int id);
}
关于前端页面, 因为Spring Boot官方不推荐jsp,所以卡在前端页面的显示上了.
收获:
重新看看Spring:
spring通过ioc容器管理Bean(spring是基于bean的编程)
目的是降低耦合度
如何装配bean.优先级:
1.隐式bean的发现机制和自动装配原则
2.java接口和类中实现
3.xml中显式配置
遇到的问题:
踩了个坑
spring boot版本2.0以上时,spring-boot-starter-web依赖就会报错.
明天的计划
评论