SpringBoot与数据访问

一、JDBC

<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>
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/jdbc
    driver-class-name: com.mysql.jdbc.Driver

SpringBoot默认使用的数据是org.apache.tomcat.jdbc.pool.DataSource,SpringBoot默认可以支持org.apache.tomcat.jdbc.pool.DataSource、HikariDataSource、BasicDataSource,若要修改数据源的类型,在配置文件中添加spring.datasource.type属性即可。

​ 在resource目录下,创建schema-*.sql、data-*sql的建表语句,在SpringBoot启动时,就会执行命令。或者指定spring.datasource.schema=- classpath:a.sql可以自定义名建表语句。

SpringBoot自动配置了JdbcTemplate去操作数据库。

二、整合 Druid 数据源

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

​ 指定spring.datasource.typecom.alibaba.druid.pool.DruidDataSource即可。

@Configuration
public class DruidConfig {
    
    //配置数据源,或在application.yml中指定spring.datasource.type
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid() {
        return new DruidDataSource();
    }

    //配置Druid的监控
    //1、配置一个管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new StatViewServlet());
        bean.addUrlMappings("/druid/*");
        Map<String, String> initParams = new HashMap<>();
        initParams.put("loginUserName", "admin");
        initParams.put("loginPassword", "admin");
        //设置白名单
        initParams.put("allow", "");
        //设置黑名单
        initParams.put("deny", "");
        bean.setInitParameters(initParams);
        return bean;
    }

    //2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        bean.setUrlPatterns(Arrays.asList("/*"));
        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(initParams);
        return bean;
    }
}

三、整合 MyBatis

添加依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

3.1 注解方式

@Mapper
public interface DeptMapper {
    @Select("select * from dept where dept_no = #{deptNo}")
    Dept getDeptByDeptNo(@Param("deptNo") Integer deptNo);

    @Delete("delete from dept where dept_no = #{deptNo}")
    int deleteDeptById(@Param("deptNo") Integer deptNo);

    @Options(useGeneratedKeys = true, keyProperty = "deptNo")
    @Insert("insert into dept(name,loc) values(#{dept.name},#{dept.loc})")
    int insertDept(@Param("dept")Dept dept);

    @Update("update dept set name = #{dept.name},loc = #{dept.loc} where dept_no = #{dept.deptNo}")
    int updateDept(@Param("dept") Dept dept);
}

@Mapper指定这个文件是一个操作数据库的Mapper,并不能使该Mapper添加到IOC容器中,需要在启动类上添加注解MapperScan()扫描该包。

​ 设置支持驼峰mybatis.configuration.map-underscore-to-camel-case=true

3.2 配置文件方式

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml 指定全局配置文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml  指定sql映射文件的位置

四、整合 SpringData JPA

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/jdbc
  jpa:
    database: mysql
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: update
    #懒加载支持
    open-in-view: true
    properties:
      hibernate:
        enable_lazy_load_no_trans: true
//String为主键的类型
public interface RoleRepository extends JpaRepository<Role,String> {
}

​配置完这些后在Service层就可以直接操作持久层了,JPA通用xMapper很像,但支持一对多,多对多…这种会更好。因为JPA的作者也是Hibernate的作者,所以JPA用到了很多Hibernate的注解,后面我将写一遍详细关于JPA的使用。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页