SpringBoot实战之整合mybatis

来源:互联网 发布:淘宝亏本冲销量 编辑:程序博客网 时间:2024/05/21 09:13

前言

引用百度百科:

  • 简单易学:本身就很小且简单。没有任何第三方依赖,最简单安装只要两个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=9527spring.datasource.driver-class-name=org.mariadb.jdbc.Driverspring.datasource.url=jdbc:mariadb://192.168.175.128:3306/exercise?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=root@mariadbmybatis.mapper-locations=classpath*:mybatis/*Mapper.xmlmybatis.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操作

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩早上起太早怎么办 老人忌日忘了怎么办 五行金水太旺怎么办 土命人偏偏水多怎么办 小野鸡不吃东西怎么办 宝宝发烧一天一夜不退怎么办 跟老公八字不合怎么办 做俯卧撑胸肌疼怎么办 健身完肌肉酸痛怎么办 不小心杀生了怎么办 名字取得太大了怎么办 麻将馆没客人怎么办 顺丰快递着急怎么办 孕妇吃菠萝过敏怎么办 燕窝炖稀了怎么办 玻尿酸隆鼻变宽怎么办? 三无燕窝被打假 怎么办 喜用神为土应该怎么办 喜用神互相克害怎么办? 姓名总格不好怎么办 姓名学里人格不好怎么办 买家订单下不了怎么办 公积金不够月供怎么办 房贷担保费没交怎么办 营业执照过期1年怎么办 营业执照和公章丢失怎么办 个体户怎么办对公账户 公章法人章丢失怎么办 广州买房没有社保怎么办 辞职后计生关系怎么办 广州换工作社保怎么办 学校更名了盖章怎么办 工商注册资金没有交怎么办 住公司宿舍怎么办居住证 住在公司宿舍怎么办居住证 滴滴没有的车型怎么办 假的租房合同怎么办 代注册公司被骗怎么办 公司跨区迁址怎么办 公司搬走注册地怎么办 注册公司没有房产证怎么办