SpringBoot整合mybatis实现增删改查

步骤:
1、创建项目;
2、添加mybatis依赖;
3、在application.yml进行数据源属性配置;
4、定义数据源对象与配置文件中前缀是spring.datasource的配置进行绑定;
5、创建数据表(department,employee);
6、创建实体类;
7、创建mapper接口;
8、编写controller处理请求,调用mapper对象执行查询方法。

1.创建项目

项目结构:

2,添加Druid依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

application.yml

#端口号
server:
  port: 8080

  #数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myjdbc212?characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
      #sql:
      #init:
      #指定脚本文件位置
      #schema-locations: classpath:user.sql
      #初始化方式
    #mode: always
    #设置数据源类型C
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  configuration:
     map-underscore-to-camel-case: true
#mybatis:
  #指定mybatis配置文件的位置
  #config-location: classpath:mybatis/mybatis-config.xml
  #指定映射文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml

3,config包下

DruidConfig类

@Configuration
public class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource getDurid(){
        return new DruidDataSource();
    }
}

MyBatisConfig类

@Configuration
public class MyBatisConfig {



    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                //下划线与驼峰命名进行自动映射
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

4,controller类

DepartmentController类:

@RestController
public class DepartmentController {
    @Autowired
    private DepartmentMapper departmentMapper;

    //根据id查询部门
    @RequestMapping("/dept/{id}")

    public Department getDepById(&#
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

kongbaidaima

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

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

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

打赏作者

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

抵扣说明:

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

余额充值