栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot——MyBatis整合数据库连接、配置文件方式、注解方式、配置文件+注解方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot——MyBatis整合数据库连接、配置文件方式、注解方式、配置文件+注解方式

1.pom.xml
        
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        





        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.17
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
2.bean
@Data
public class GasTest {
    String isLeak;
    int id;
    String time;
    float pressure;
    //需要开启驼峰命名规则
    float flowRate;
    float velocity;
}

@Data:lombok的jar包,封装get、set方法

3.resources目录截图

建议先学SpringBoot的静态资源和动态资源目录

4.application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/graduationproject
    username: root
    password: 113846
    driver-class-name: com.mysql.jdbc.Driver            #5.x版本mysql

#配置mybatis规则
mybatis:
#  config-location: classpath:mybatis/mybatis-config.xml    #全局只允许一个配置文件,这是开启驼峰命名规则的配置文件
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:   #指定mybatis全局配置文件中的配置项目,那就不需要mybatis-config.xml了
    map-underscore-to-camel-case: true          #开启驼峰命名规则
5.整合mybatis 5.1使用配置文件方式整合mybatis,查询数据库 (1).mapper
@Mapper
public interface GasTestMapper {
    public GasTest getIdInfo(int id);
}

@Mapper:声明这是一个bean。在连接数据库中加上@Mapper注解,使用mybatis配置文件mapper.xml,mapper.xml中的namespace指向mapper,会动态生成bean。在服务实现层serviceImpl中能够@Autowired注解动态注入该bean。

(2).mybatismapperGasTestMapper.xml



    
        select * from gastest where id=#{id}
    

namespace:命名空间,mapper接口类的路径

id:mapper中的接口方法

resultType:返回数据集的数据类型

(3).service和serviceimpl
@Service
public interface GasTestService {
    public GasTest getIdInfo(int id);
}


@Service
public class GasTestServiceImpl implements GasTestService {
    @Autowired
    GasTestMapper gasTestMapper;
    @Override
    public GasTest getIdInfo(int id) {
        return gasTestMapper.getIdInfo(id);
    }
}
(4)controller
@Slf4j
@RestController
public class GasTestController {
    @Autowired
    GasTestService gasTestService;

    //使用配置文件方式整合mybatis,查询数据库,方式1
    //注解RequestParam,路径传参,参为id,方式1
    @RequestMapping("/gas")
    public GasTest getIdInfo(@RequestParam("id") int id){
        return gasTestService.getIdInfo(id);
    }
}
(5)接口测试结果展示

5.2使用纯注解方式整合mybatis,查询数据库 (1).mapper
@Mapper
public interface GasTest2Mapper {
    //方式2
    @Select("select * from gastest where id=#{id}")
    public GasTest2 getIdInfo(int id);
}
(2).service和serviceimpl
@Service
public interface GasTest2Service {
    public GasTest2 getIdInfo(int id);
}

@Service
public class GasTest2ServiceImpl implements GasTest2Service {
    @Autowired
    GasTest2Mapper gasTest2Mapper;

    public GasTest2 getIdInfo(int id){
        return gasTest2Mapper.getIdInfo(id);
    }
}
(3).controller
    @Autowired
    GasTest2Service gasTest2Service;
    //使用纯注解整合mybatis,查询数据库,方式2
    //注解RequestParam,路径传参,参为id,方式2
    @RequestMapping("/gas2")
    public GasTest2 getIdInfo2(@RequestParam("id") int id){
        return gasTest2Service.getIdInfo(id);
    }
(4).接口测试展示结果

5.3使用注解+配置文件方式整合mybatis,查询数据库 (1).mapper或mapper
@Mapper
public interface GasTest2Mapper {
    //方式3
    //也可以用配置文件方式mybatismapperGasTest2Mapper.xml
    @Insert("insert into gastest(`isleak`,`time`,`pressure`,`flow_rate`,`velocity`) values(#{isLeak},#{time},#{pressure},#{flowRate},#{velocity})")
    //开启将没有表单传参的自增id返回,Options注解方式,
    @Options(useGeneratedKeys = true,keyProperty = "id")
    public void insert(GasTest2 gasTest2);
}    

(2).mybatismapperGasTest2Mapper.xml或mybatismapperGasTest2Mapper.xml



    



(3).service和serviceImpl
@Service
public interface GasTest2Service {
    public void insert(GasTest2 gasTest2);
}

@Service
public class GasTest2ServiceImpl implements GasTest2Service {
    @Autowired
    GasTest2Mapper gasTest2Mapper;

    public void insert(GasTest2 gasTest2){
        gasTest2Mapper.insert(gasTest2);
    }
}
(4).controller
    //方式3
    //也可以用配置文件方式mybatismapperGasTest2Mapper.xml
    @Insert("insert into gastest(`isleak`,`time`,`pressure`,`flow_rate`,`velocity`) values(#{isLeak},#{time},#{pressure},#{flowRate},#{velocity})")
    //post请求。insert这个方法既可以1.使用配置方式 2.也可以使用注解方式 3.或配置+注解方式一块用
    @PostMapping("/gas3")
    public GasTest2 insertGasTestInfo(GasTest2 gasTest2){
        log.info("gasTest2={}",gasTest2);
        gasTest2Service.insert(gasTest2);
        return gasTest2;
    }
(5).接口测试展示结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918797.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号