对于数据访问层, 无论是SQL还是NOSQL, Spring Boot默认采用整合Spring Data的方式进行同一处理, 添加大量的自动配置, 屏蔽了很多设置. 引入各种xxxTemplate, xxxRepository 来简化我们对数据访问层的操作. 对于我们来说只需要进行简单的设置即可.
在项目创建时可添加关于JDBC的场景, 并导入关于数据库的驱动, 在pom文件中会生成以下依赖坐标
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>使用datasource属性, 可以配置关于数据源的连接信息
spring: datasource: username: root password: Hhn004460 url: jdbc:mysql://10.39.2.153:3306/jdbc driver-class-name: com.mysql.cj.jdbc.DriverSpringBoot2.x默认使用的HikariDataSource作为数据源
数据源全部相关配置可在DataSourceProperties中查看.
SpringBoot默认可以支持com.zaxxer.hikari.HikariDataSource和org.apache.tomcat.jdbc.pool.DataSource两种数据源.
自定义数据源类型: 可在配置文件中使用type属性指定数据源
@ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type") static class Generic { @Bean public DataSource dataSource(DataSourceProperties properties) { //使用DataSourceBuilder创建数据源,利用反射创建响应type的数据源,并且绑定相关属性 return properties.initializeDataSourceBuilder().build(); } }DataSourceInitializer可以用于自动创建数据表
runSchemaScripts()方法; 运行建表语句
runDataScripts()方法; 运行插入语句
默认只需要将文件命名为: schema-*.sql 或 data-*.sq
也可以通过配置指定配置文件
schema: - classpath:people.sql在SpringBoot2.x以上时需要自动创建数据表需要额外配置以下内容
spring: datasource: initialization-mode: always使用type属性切换数据源
spring: datasource: username: root password: Hhn004460 url: jdbc:mysql://10.39.2.153:3306/jdbc driver-class-name: com.mysql.cj.jdbc.Driver # 更改数据源 type: com.alibaba.druid.pool.DruidDataSource # 数据源其他配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500将Druid注入到IoC容器中
配置监控页面Servlet
配置监控的Filter
@Configuration public class DruidConfig { /** * 将DataSource添加到容器 * @return Druid */ @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druid(){ return new DruidDataSource(); } //配置Druid的监控 /** * 配置一个管理后台的Servlet * @return ServletRegistrationBean */ @Bean public ServletRegistrationBean <StatViewServlet> statViewServlet(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(4); //配置登录信息 initParams.put("loginUsername", "admin"); //用户名及密码 initParams.put("loginPassword", "123456"); //访问权限 initParams.put("allow", ""); initParams.put("deny", ""); bean.setInitParameters(initParams); return bean; } /** * 配置后台监控Filter * @return FilterRegistrationBean */ @Bean public FilterRegistrationBean<WebStatFilter> webStatFilter(){ FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); Map<String, String> initParams = new HashMap<>(1); //排除过滤资源 initParams.put("exclusions", "*.js,*.css,/druid/*"); bean.setUrlPatterns(List.of("/*")); bean.setInitParameters(initParams); return bean; } }在项目创建时可添加关于 MyBatis, Mysql的场景, 并导入关于数据库的驱动, 在pom文件中会生成以下依赖坐标
<!-- mybatis start --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>是定其是一个操作数据库的Mapper
@Mapper public interface DepartmentDao { @Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into department(departmentName) values (#{departmentName})") int insertDept(Department department); ... }其它使用方式参考MyBatis官方文档
给容器中添加一个ConfigurationCustomizer组件, 重写customize中的方法进行规则的添加
@org.springframework.context.annotation.Configuration public class MyBatisConfig { @Bean public ConfigurationCustomizer configurationCustomizer(){ return new ConfigurationCustomizer() { @Override public void customize(Configuration configuration) { //开启驼峰式命名 configuration.setMapUnderscoreToCamelCase(true); } }; } }使用MapperScan批量扫描所有的Dao接口
可以标注在MyBatis配置类中或Application主类中
@SpringBootApplication @MapperScan("com.study.mybatis.dao") public class DataMybatisApplication { public static void main(String[] args) { SpringApplication.run(DataMybatisApplication.class, args); } }更多使用参照: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
SpringData 项目的目的是为了简化构建基于Spring框架应用的数据访问技术, 包括非关系数据库, Map-Reduce 框架, 云数据服务等等; 另外也包含对关系数据库的访问支持. SpringData为我们提供使用统一的API来对数据访问层进行操作.Spring Data Commons让我们在使用关系型或非关系型数据访问技术时都基于Spring提供的统一标准, 标准包含了CRUD等操作
通过添加SpingData JPA场景, 会导入以下坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>@Entiy: 表示实体类, 其属性和数据表可映射
@Table: 指定与那张表进行映射, 默认为类名
@Id: 指定主键
@GeneratedValue: 主键自增等信息
@Column(name = "last_Name", length = 50): 指定字段名, 默认为属性名
@Entity @Table(name = "t_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "last_Name", length = 50) private String lastName; @Column private String email; 省略getter/setter方法 }使用JPA操作数据库, 其Dao接口需要基础JpaRepository<T, V>类, 其实现了CRUD等方法
泛型T为对应的实体类, 泛型V为实体类主键的包装类
public interface UserDao extends JpaRepository<User, Integer> { ... }在配置文件中使用spring.jpa属性即可配置jpa的相关信息
spring: jpa: hibernate: # 更新或创建数据表结构 ddl-auto: update # 在控制台显示sql show-sql: true在项目启动时, JPA则会自动创建表结构
