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的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HuCheng1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值