资讯专栏INFORMATION COLUMN

springboot 整合 mybatis(无spring开发经验版本)

gotham / 610人阅读

摘要:相关代码开始驼峰命名与下划线命名的转换

springboot 整合 mybatis(无spring开发经验版本) 目录结构

目录解释

controller 定义路由

service 业务逻辑处理

entity 实体类 与数据库中的表一一对应

mapper 数据库操作,定义对数据库各种CUDR的接口,myBatis框架会自动生成实体类

mapping 数据库操作的XML文件,通过namespace 与 mapper一一对应,通过每一项中的id对应接口类中定义的方法,通过每一项中的resultType对应实体类,表明数据返回的对象。

相关代码

application.yml

</>复制代码

  1. spring:
  2. profiles:
  3. active: dev

application-dev.yml

</>复制代码

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. username: root
  6. password: 19961110
  7. url: jdbc:mysql://47.95.110.227:3308/news?useUnicode=true&characterEncoding=utf-8
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. mybatis:
  10. mapper-locations: classpath:mapping/*Mapper.xml
  11. configuration:
  12. # 开始驼峰命名与下划线命名的转换
  13. map-underscore-to-camel-case: true
  14. #showSql
  15. logging:
  16. level:
  17. com:
  18. example:
  19. mapper : debug

indexController.Java

</>复制代码

  1. package com.crxk.myBatisTset.controller;
  2. import com.crxk.myBatisTset.service.catService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @Controller
  8. @ResponseBody
  9. public class indexController {
  10. @Autowired
  11. private catService catService;
  12. @RequestMapping("/getCat")
  13. public String getCat(int id){
  14. return catService.getCat(id).toString();
  15. }
  16. }

catService.java

</>复制代码

  1. package com.crxk.myBatisTset.service;
  2. import com.crxk.myBatisTset.entity.Cat;
  3. import com.crxk.myBatisTset.mapper.catMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. @Service
  7. public class catService {
  8. @Autowired
  9. catMapper catMapper;
  10. public Cat getCat(int id){
  11. return catMapper.getCat(id);
  12. }
  13. }

cat.java

</>复制代码

  1. package com.crxk.myBatisTset.entity;
  2. public class Cat {
  3. int catId;
  4. String catName;
  5. public Cat() {
  6. }
  7. public Cat(int catId, String catName) {
  8. this.catId = catId;
  9. this.catName = catName;
  10. }
  11. public int getCatId() {
  12. return catId;
  13. }
  14. public void setCatId(int catId) {
  15. this.catId = catId;
  16. }
  17. public String getCatName() {
  18. return catName;
  19. }
  20. public void setCatName(String catName) {
  21. this.catName = catName;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Cat{" +
  26. "catId=" + catId +
  27. ", catName="" + catName + """ +
  28. "}";
  29. }
  30. }

catMapper.java

</>复制代码

  1. package com.crxk.myBatisTset.mapper;
  2. import com.crxk.myBatisTset.entity.Cat;
  3. import org.apache.ibatis.annotations.Mapper;
  4. @Mapper
  5. public interface catMapper {
  6. Cat getCat(int id);
  7. }

catMapper.xml

</>复制代码

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75246.html

相关文章

  • SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+Pa

    摘要:当禁用时,所有关联对象都会即时加载。不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动。需要适合的驱动。系统默认值是设置字段和类是否支持驼峰命名的属性。   上篇文章我们介绍了SpringBoot和MyBatis的整合,可以说非常简单快捷的就搭建了一个web项目,但是在一个真正的企业级项目中,可能我们还需要更多的更加完善的框架才能开始真正的开发,比如连接池、分...

    hatlonely 评论0 收藏0
  • 新手也能实现,基于SpirngBoot2.0+ 的 SpringBoot+Mybatis 多数据源配

    摘要:下面基于,带着大家看一下中如何配置多数据源。注意版本不一致导致的一些小问题。配置配置两个数据源数据库和数据库注意事项在配置数据源的过程中主要是写成和。五启动类此注解表示启动类这样基于的多数据源配置就已经完成了,两个数据库都可以被访问了。 在上一篇文章《优雅整合 SpringBoot+Mybatis ,可能是你见过最详细的一篇》中,带着大家整合了 SpringBoot 和 Mybatis...

    shiina 评论0 收藏0
  • 写这么多系列博客,怪不得找不到女朋友

    摘要:前提好几周没更新博客了,对不断支持我博客的童鞋们说声抱歉了。熟悉我的人都知道我写博客的时间比较早,而且坚持的时间也比较久,一直到现在也是一直保持着更新状态。 showImg(https://segmentfault.com/img/remote/1460000014076586?w=1920&h=1080); 前提 好几周没更新博客了,对不断支持我博客的童鞋们说声:抱歉了!。自己这段时...

    JerryWangSAP 评论0 收藏0
  • 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    摘要:基于最新的,是你学习的最佳指南。驱动程序通过自动注册,手动加载类通常是不必要。由于加上了注解,如果转账中途出了意外和的钱都不会改变。三的方式项目结构相比于注解的方式主要有以下几点改变,非常容易实现。公众号多篇文章被各大技术社区转载。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(Sprin...

    gghyoo 评论0 收藏0

发表评论

0条评论

gotham

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<