SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
原生开发SpringMVC程序过程
①:创建新模块,选择Spring初始化,并配置模块相关基础信息
②:选择当前模块需要使用的技术集
③:开发控制器类
@RestController
@RequestMapping("/books")
public class BookController {@GetMapping("/{id}")public String getById(@PathVariable Integer id) {System.out.println("id ==> " + id);return "hello , spring boot! ";}
}
④:运行自动生成的Application类
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.0 com.itheima springboot-01-quickstart 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
注意事项:
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
① 对SpringBoot项目打包(执行Maven构建指令package)
② 执行启动指令
java -jar springboot_01_quickstart.jar # 项目的名称根据实际情况修改
注意事项:
jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件。
org.springframework.boot spring-boot-maven-plugin
学习了SpringBoot入门案例之后,感觉对比SpringMVC哪一个更加方便简洁?
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.0 com.itheima springboot-01-quickstart 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web
4.0.0 org.springframework.boot spring-boot-dependencies 2.5.0 pom 4.0.1 ...
4.0.0 org.springframework.boot spring-boot-dependencies 2.5.0 spring-boot-starter-parent pom ...
junit junit ${junit.version}
javax.servlet javax.servlet-api ${servlet-api.version}
org.springframework.boot spring-boot-starter-parent 2.5.0 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
@SpringBootApplication
public class Springboot01QuickstartApplication {public static void main(String[] args) {SpringApplication.run(Springboot01QuickstartApplication.class, args);}
}
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty
框架常见的配置文件有哪几种形式?
http://localhost:8080/books/1 >>> http://localhost/books/1
SpringBoot提供了多种属性配置方式
server.port=80
server:port: 81
server:port: 82
操作步骤:
注意事项:
什么是yaml,和properties有什么区别?
封装全部数据到Environment对象
自定义对象封装指定数据【常用】
public class Enterprise {private String name;private Integer age;private String tel;private String[] subject;//自行添加getter、setter、toString()等方法
}
org.springframework.boot spring-boot-configuration-processor true
在实际开发中,项目的开发环境、测试环境、生产环境的配置信息是否会一致?如何快速切换?
#主启动配置文件 application.properties
spring.profiles.active=pro
#环境分类配置文件 application-pro.properties
server.port=80
#环境分类配置文件 application-dev.properties
server.port=81
#环境分类配置文件application-test.properties
server.port=82
java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
Maven与SpringBoot多环境兼容(步骤)
①:Maven中设置多环境属性
dev_env dev true pro_env pro test_env test
②:SpringBoot中引用Maven属性
③:执行Maven打包指令
Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
④:对资源文件开启对默认占位符的解析
maven-resources-plugin utf-8 true
SpringBoot的配置文件可以放在项目的哪些地方?
java –jar springboot.jar --spring.profiles.active=test --server.port=85 --server.servlet.context-path=/heima --server.tomcat.connection-timeout=-1 ... ...
SpringBoot中4级配置文件
1级: file :config/application.yml 【最高】
2级: file :application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml 【最低】
作用:
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性
回忆一下Spring整合JUnit的步骤?
【第一步】添加整合junit起步依赖(可以直接勾选)
org.springframework.boot spring-boot-starter-test test
【第二步】编写测试类,默认自动生成了一个
@SpringBootTest
class Springboot07JunitApplicationTests {@Autowiredprivate BookService bookService;@Testpublic void testSave() {bookService.save();}
}
回忆一下Spring整合MyBatis的核心思想?
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
public class SpringConfig {}
#jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=itheima
public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource getDataSource() {DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) {SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;
}
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itheima.dao");return msc;
}
①:创建新模块,选择Spring初始化,并配置模块相关基础信息
②:选择当前模块需要使用的技术集(MyBatis、MySQL)
③:设置数据源参数
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTCusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
注意事项:
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
④:定义数据层接口与映射配置
@Mapper
public interface UserDao {@Select("select * from tbl_book where id=#{id}")Book getById(Integer id);
}
⑤:测试类中注入dao接口,测试功能
@SpringBootTest
class Springboot08MybatisApplicationTests {@Autowiredprivate BookDao bookDao;@Testpublic void testGetById() {Book book = bookDao.getById(1);System.out.println(book);}
}
【第一步】创建SpringBoot工程,添加druid依赖
com.alibaba druid 1.2.6
【第二步】复制springmvc_11_page工程各种资源(主java类、页面、测试类)
【第三步】删除config包中的所有配置,在BookDao接口上加@Mapper注解
//todo 3 在BookDao接口上加@Mapper注解,让SpringBoot给接口创建代理对象
@Mapper
public interface BookDao {//...
}
【第四步】将application.properties修改成application.yml,配置端口号和连接参数
server:port: 80
# todo 4 配置数据库连接参数
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_dbusername: rootpassword: roottype: com.alibaba.druid.pool.DruidDataSource
【第五步】修改BookServiceTest配置类,进行配置
// todo 5 修改单元测试类,添加@SpringBootTest主键,修复@Test注解导包
@SpringBootTest
public class BookServiceTest {@Autowiredprivate BookService bookService;@Testpublic void testGetById(){Book book = bookService.getById(2); //传递参数1会抛出异常System.out.println(book);}@Testpublic void testGetAll(){List all = bookService.getAll();System.out.println(all);}
}
【第六步】在static目录中提供index.html页面,跳转到"pages/books.html"
最后:运行引导类即可访问