• SpringBoot整合Mybatis总结


    导入依赖,看清楚了,别导错

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.2.0</version>
    </dependency>
    

    配置application.yml

    spring:
      profiles:
        active: dev
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.feige.domin
    

    配置application-dev.yml

    server:
      port: 8080
     
     #mybatis setting
    spring:
      datasource:
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.cj.jdbc.Driv
    

    项目有好多环境,开发环境,测试环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到不同的yml文件中。
    application-dev.yml:开发环境
    application-test.yml:测试环境
    application-prod.yml:生产环境
    至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置。

    SpringBoot启动类

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    // 扫描包中的类,加载入spring内。
    @ComponentScan(basePackages = {"com.feige.*",})
    @SpringBootApplication
    //这个必须要有,不然找不到mapper
    @MapperScan("com.feige.*")
    public class MySpringBoootApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MySpringBoootApplication.class, args);
    	}
    }
    

    Mapper.java

    import org.springframework.stereotype.Repository;
    
    import com.feige.domin.User;
    
    @Repository
    public interface UserMapper {
    	User selectById(String id);
    }
    

    Mapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.feige.dao.UserMapper">
    
        <select id="selectById" resultType="com.feige.domin.User" parameterType="java.lang.String">
            select * from user where id = #{0}
        </select>
    
    </mapper>
    

    Controller.java

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.feige.dao.UserMapper;
    import com.feige.domin.User;
    
    @RestController
    public class UserController {
    	
    	@Autowired
    	private UserMapper mapper;
    	
    	@PostMapping("/person/select")
    	public User select(@RequestParam String id) {
    		User user = mapper.selectById(id);
    		System.out.println(user);
    		return user;
    	}
    }
    

    项目结构

    Postman测试

  • 相关阅读:
    【转贴】Cookie + Session + OAuth + SSO
    zz淘宝商品库MySQL优化实践
    HIVE 数据倾斜调优总结zz
    数据挖掘笔记(一)
    hive函数参考手册
    hive QL(HQL)简明指南zz
    数据挖掘笔记(二)
    python format string (转)
    hive 中转义符使用问题
    关于文档管理
  • 原文地址:https://www.cnblogs.com/feiqiangsheng/p/11007637.html
Copyright © 2020-2023  润新知