idea中spring-boot整合mybatis,使用application.yaml文件配置mysql

总体的包结构如下

idea中spring-boot整合mybatis,使用application.yaml文件配置mysql_第1张图片

pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.yixuan
    spring-boot-05-mybaits
    0.0.1-SNAPSHOT
    spring-boot-05-mybaits
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


pojo下的User.java文件

package com.yixuan.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private  int id;
    private String name;
}

datasource:

idea中spring-boot整合mybatis,使用application.yaml文件配置mysql_第2张图片

数据文件:

idea中spring-boot整合mybatis,使用application.yaml文件配置mysql_第3张图片

application.yaml文件

spring:
  datasource:
    username: root
    password:
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

    #整合mybatis
mybatis:
  type-aliases-package: com.yixuan.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml       # mapper映射文件

mapper下的UserMapper.java文件

package com.yixuan.mapper;

import com.yixuan.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    List queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);
}

controller下的UserController.java文件

package com.yixuan.controller;

import com.yixuan.mapper.UserMapper;
import com.yixuan.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    //查询用户列表
    @GetMapping("/queryUserList")
    public List queryUserList(){
        List userList = userMapper.queryUserList();
        for(User user:userList){
            System.out.println(user);
        }
        return userList;
    }
    //增加一个用户
    @GetMapping("/addUser")
    public String addUser(){
        userMapper.addUser(new User(6,"小三"));
        return  "ok";
    }
    //修改一个用户
    @GetMapping("/updateUser")
    public String updateUser(){
        userMapper.updateUser(new User(2,"小王"));
        return "ok";
    }
    //删除一个用户
    @GetMapping("deleteUser")
    public String deleteUser(){
        userMapper.deleteUser(5);
        return "ok";
    }
    @GetMapping("queryUserById")
    public User queryUserById(){
        User user = userMapper.queryUserById(2);
        System.out.println(user);
        return user;
    }

}

resource下的mybatis->mapper->UserMapper.xml文件




    

    

    
      insert into User (id ,name) value  (#{id},#{name})
   

    
      update user set name =#{name} where id =#{id}
   

    
      delete from user where id = #{id}
   

测试界面:

idea中spring-boot整合mybatis,使用application.yaml文件配置mysql_第4张图片

你可能感兴趣的:(idea中spring-boot整合mybatis,使用application.yaml文件配置mysql)