当前位置:   article > 正文

spring boot整合mybatis完全注解开发_springboot mybatis注解开发

springboot mybatis注解开发

在ssm项目中mybatis大多使用xml的配置文件,为了简洁也可以完全注解开发。在spring boot中已经整合了mybaits,只需要使用响应的注解启动即可。

  • 导入依赖相关注解加载即可。
 <dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter</artifactId>
     <version>2.2.2</version>
 </dependency>

 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 配置数据源及驱动

数据源没用第三方框架,直接是mybatis的数据源。

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smbms?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.type=com.mysql.cj.jdbc.MysqlDataSource
  • 1
  • 2
  • 3
  • 4
  • 5
  • 编写mapper映射
@Mapper
public interface ProviderMapper {

    @Select("select * from smbms_provider")
    @Results(id = "providerMap",value = {
            @Result(column = "id",property = "id", jdbcType = JdbcType.INTEGER,id = true),
            @Result(column = "proCode",property = "proCode"),
            @Result(column = "proName",property = "proName",jdbcType = JdbcType.CHAR),
            @Result(column = "proDesc",property = "proDesc",jdbcType = JdbcType.CHAR),
            @Result(column = "proContact",property = "proContact", jdbcType = JdbcType.CHAR),
            @Result(column = "proPhone",property = "proPhone",jdbcType = JdbcType.CHAR),
            @Result(column = "proAddress",property = "proAddress",jdbcType = JdbcType.CHAR),
            @Result(column = "proFax",property = "proFax",jdbcType = JdbcType.CHAR),
            @Result(column = "createdBy",property = "createdBy",jdbcType = JdbcType.INTEGER),
            @Result(column = "creationDate",property = "creationDate",jdbcType = JdbcType.CHAR),
            @Result(column = "modifyDate",property = "modifyDate",jdbcType = JdbcType.CHAR),
            @Result(column = "modifyBy",property = "modifyBy",jdbcType = JdbcType.INTEGER)

    })
    List<Provider> selectAll();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

@Mapper注解是mybatis-spring的启动类,使用该注解的类,spring会在IoC容器中时启用SqlSession调用getMapper方法创建并返回BlogMapper。在spring boot项目中不需要使用类似@Repository注解将其注入到IoC容器中,@Mapper已经集成。也可以使用@MapperScan包扫描。

  • 编写业务层
@Repository
public class ProviderServiceImpl implements ProviderService {

    @Autowired
    private ProviderMapper providerMapper;

    @Override
    public Provider selectById(int id) {

        List<Provider> providers = providerMapper.selectAll();
        Provider provider = providers.get(id);
        return provider;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
public interface ProviderService {
    Provider selectById(int id);
}
  • 1
  • 2
  • 3
  • 控制器返回数据
@RestController

@RequestMapping(value = "/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @GetMapping(value = "/{id}")
    public Provider getById(@PathVariable int id){
        Provider provider = providerService.selectById(id);
        return provider;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在这里插入图片描述

spring boot使用mybatis真的十分方便!mybatis也还有其他注解详见mybatis注解开发

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/922020
推荐阅读
相关标签
  

闽ICP备14008679号