SpringBoot之数据访问

1 . 原生态JDBC

  • 导入spring-boot-start-data-jdbc依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
  • 导入数据库驱动包mysql-connection-java依赖,springboot自动对其依赖进行了版本仲裁,图中所示版本为8.0.27

  • 数据库版本和springboot提供的驱动版本需要一致,若不一致需要修改,有两种修改方式

  • 1.直接在依赖中写入<version>版本</version>,利用了maven的就近依赖原则

  • 2.在<properties>属性中添加mysql的版本,利用了maven的就近优先原则

<properties>
<mysql.version版本号</mysql.version>
</properties>

 编写yaml配置文件

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxx?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

进行测试即可

2 . 导入druid数据库连接池

  • 导入相应的jar包就行

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.17</version>
</dependency>

导入之后数据库的连接池会自动替换成druid,可进行测试看是不是连接了Druid连接池

@Autowired
DataSource source;
@Test
void contextLoads() {
    System.out.println(source.getClass());
}

说明导入成功  

3 . 整合Mybatis

  • 导入mybatis-spring-boot-starter第三方依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>//版本号
</dependency>

mybatis的依赖中已经有了spring-boot-starter-jdbc的包,可以将之前写入的jdbc依赖删除

 现在来回顾之前使用mybatis来操作数据库的步骤

 第一步:编写核心配置文件mybatis-config.xml,不需要任何配置,因为第三方依赖包帮我们全部配置好了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

</configuration>

第二步:编写mapper接口,并在类上添加注解@Mapper

@Mapper
public interface UserMapper {

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    User queryUserById(int id);

}

第三步:编写mapper接口的对应的.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
    <select id="queryUserById" resultType="com.example.domain.User">
        select * from user where id=#{id};
    </select>
</mapper>

第四步:编写application.yaml的mybatis配置

mybatis:
#主核心配置文件的路径
  config-location: classpath:mybatis/mybatis-config.xml
#mapper配置文件的路径
  mapper-locations: classpath:mybatis/mapper/*.xml

第五步:编写service层调mapper接口,service层的实现类需要标注@Service注解注入到容器中

public interface UserService {
    /**
     * 根据id查询用户名
     * @param id
     * @return
     */
    User queryById(int id);
}

注意:这里的mapper添加了@Autowired自动注入后会报错,其实不影响程序的执行,也可以在mapper层将@mapper注解换成@Repository,就不会报错了

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper mapper;
    @Override
    public User queryById(int id) {
        return mapper.queryUserById(id);
    }
}

第六步:编写控制器层进行测试,这里的自动注入是注入service 的接口还是实现类都可以,不会报错,一般是注入service层的接口

@Autowired
UserService service;

@GetMapping("/user")
@ResponseBody
public User getUserById(@RequestParam("id") int id){
    User user = service.queryById(id);
    return user;
}

测试后发现了一个问题:user类的userName值为空,并没有和数据库中的user_name绑定,这是因为mybatis没有设置驼峰命名规则,要手动开启。

 在主配置文件添加驼峰命名的设置即可

<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

也可以不需要添加设置,只需要在application.yaml中设置一下mybatis就行,此时不需要设置mybatis的全局配置文件了,所以后面的开发不需要再写mybatis的主核心配置文件了  

mybatis:
  #config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
  • 在yaml中设置mybatis包的别名

  • type-aliases-package:xxx

mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
  type-aliases-package: com.example.domain

有时在插入操作中不会进行添加主键的操作,若需要将自增主键的值同时封装到实体类中,可以使用如下属性:useGeneratedKeys="true表示将自增主键也封装到实体类中,keyProperty="id",表示自增主键的字段值在实体类中的属性名是id

<insert id="addUser" useGeneratedKeys="true" keyProperty="id">
    insert into user (user_name,pwd) values (#{userName},#{pwd});
</insert>

可以直接在核心启动类上添加注解@MapperScan(全限定类名),这样就不需要在每个mapper接口上都添加@Mapper注解了

 

4 . 快速导入mybatis依赖

  • 可以直接在创建springboot项目时添加MyBtis Framework

吐槽:感觉无所谓吧哈哈哈哈哈哈  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值