SpringBoot实战之11 整合mybatis

前言

引用百度百科:

  • 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件+配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现。
  • 灵活:mybatis不会对应用程序或者数据库的现有设计强加任何影响。 sql写在xml里,便于统一管理和优化。通过sql基本上可以实现我们不使用数据访问框架可以实现的所有功能,或许更多。
  • 解除sql与程序代码的耦合:通过提供DAL层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。
  • 提供映射标签,支持对象与数据库的orm字段关系映射
    提供对象关系映射标签,支持对象关系组建维护
    提供xml标签,支持编写动态sql。

code实现

依赖

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>
<dependency>
    <groupId>com.hsy.java</groupId>
    <artifactId>java-bean</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

dao层实现

TExerciseZoneMapper.java

public interface TExerciseZoneMapper {

    List<TExerciseZone> selectAll(@Param(value = "offset") Integer offset, @Param(value = "limit") Integer limit) ;

    int update(@Param(value = "parentId") Integer parentId,@Param(value = "id") Long id) ;
}

mapper文件

<mapper namespace="com.hsy.springboot.mybatis.mapper.TExerciseZoneMapper">

    <select id="selectAll" resultType="TExerciseZone">
        select * from t_exercise_zone_test where 1 = 1 limit #{offset} , #{limit};
    </select>

    <select id="getProvinceById" resultType="TExerciseZone">
        select * from t_exercise_zone_test WHERE 1 = 1
        <if test="id!=null || ''!=id">
            AND id = #{id}
        </if>
    </select>

    <update id="update">
        update t_exercise_zone_test set parent_id = #{parentId} WHERE id = #{id}
    </update>
</mapper>

service层实现

@Service(value = "exerciseZoneService")
public class TExerciseZoneServiceImpl implements ITExerciseZoneService{
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired private TExerciseZoneMapper tExerciseZoneMapper ;
    @Override
    public List<TExerciseZone> getAll(Integer offset,Integer limit) {
        return tExerciseZoneMapper.selectAll(offset,limit);
    }
    @Transactional
    @Override
    public Boolean update(Integer parentId,Long id) {
        tExerciseZoneMapper.update(100000,1l) ;
        int i = 1 / 0 ;
        tExerciseZoneMapper.update(110000,2l) ;
        return true;
    }
}

web层实现

@RestController
@RequestMapping("/api/rest")
public class RestfulController extends BaseController{

    @Autowired private ITExerciseZoneService exerciseZoneService ;

    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseBodyBean<Boolean> update(@PathVariable Long id,@RequestParam Integer parentId){
        return success(exerciseZoneService.update(parentId,id)) ;
    }

    @GetMapping(value = {"/v1/zones/{offset}/{limit}"})
    public ResponseBodyBean<List<TExerciseZone>> zoneList(@PathVariable Integer offset, @PathVariable Integer limit){
        return success(exerciseZoneService.getAll(offset,limit)) ;
    }
}

配置文件

server.port=9527

spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://192.168.175.128:3306/exercise?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root@mariadb

mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.hsy.java.bean.po

项目入口

@SpringBootApplication
@MapperScan("com.hsy.springboot.mybatis.mapper")
public class SpringBootMybatisApplication {
    public static void main(String[] args){
        SpringApplication.run(SpringBootMybatisApplication.class,args) ;
    }
}

项目结构图

这里写图片描述

接口测试图

这里写图片描述

源码

springboot-mybatis

历史文章

SpringBoot实战之入门

springboot实战之文章汇总

springboot实战之读取配置文件

springboot实战之整合jsp模版引擎

springboot实战之整合freemarker模版引擎

springboot实战之注册自定义Servlet

springboot实战之注册filter和listener

springboot实战之注册interceptor

springboot实战之整合slf4j日志系统

springboot实战之整合CommandLineRunner

springboot实战之整合restful工具swagger2

springboot实战之整合jdbc进行crud操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值