整合
在使用Springboot开发Web应用的时候一般需要使用MVC架构,而其中设计三个层
分别用Springboot的三个注解代表
- Controller层
- Service层
- Dao层(mapper)
第一步:导包
导包在maven中进行,在项目中要注意项目成员的mysql版本要一致,否则会导致出现版本不一致的问题。特别是在Springboot的开发背景下。springbootStarter包会对mysql的驱动进行版本仲裁。特别明显的是mysql8与mysql5之间会出现驱动不一样的问题。
也可以自己自定义版本。
导包需要什么包?
- 对应数据库的驱动包(mysql/oracle)
- 选择使用的orm框架的包
- 数据库连接池的包(druid等)
第二步:配置数据源(dataSource)
数据源有很多种,例如Springboot底层使用的HikariDataSource,这里配置的是DruidDataSource
原本在Spring Framework阶段一般是使用XML文件中通过通过Bean标签以及DI注入对应的属性去配置
在Springboot中则不使用XML配置类与配置文件。
@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
而在Yaml中
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/db1?&useSSL=false&serverTimezone=UTC
username: root
password: root
第三步:配置Druid的监控页面(如果只是做CRUD不做监控可以不做)
@Bean
public ServletRegistrationBean staticViewServlet(){
StatViewServlet statViewServlet = new StatViewServlet();
ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<>(statView