SpringBoot 整合Mybatis

SpringBoot 整合Mybatis,第1张

SpringBoot 整合Mybatis

目录结构:

 1.依赖引入



    4.0.0

    SpringBoot1
    SpringBoot1
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    


    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
    
        org.springframework.boot
        spring-boot-starter-web
        2.4.5
    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.3
    
    
    
        mysql
        mysql-connector-java
        5.1.47
    
    
    
        org.projectlombok
        lombok
        1.18.12
        provided
    



2.TestApplication

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

3.UesrContrellor

package com.msb.controller;

import com.msb.pojo.User;
import com.msb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;



@Controller
@RequestMapping("/user")
public class UesrContrellor {
        @Autowired
        UserService  userService;

      @RequestMapping("findAll")
      @ResponseBody
      public List  findAll(){
          return    userService.findAll();
      }

}

4.UserMapper

package com.msb.mapper;

import com.msb.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface UserMapper {
    List selectAll();
}

5.User

package com.msb.pojo;

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

import java.io.Serializable;


@AllArgsConstructor
@NoArgsConstructor
@Data
public class User  implements Serializable {
    private  Integer id;
    private  String userName;
    private  String  sex;
    private  String   stills;
}

6.UserService

package com.msb.service;

import com.msb.pojo.User;
import org.springframework.stereotype.Service;

import java.util.List;




public interface UserService {
    List findAll();
}

7.UserServiceImpl

package com.msb.service.impl;

import com.msb.mapper.UserMapper;
import com.msb.pojo.User;
import com.msb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
     @Autowired
     private UserMapper userMapper;

     public List findAll() {
        return  userMapper.selectAll();
    }
}

8.UserMapper.xml





     

9.application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
#    driver-class-name: com.mysql.cj.jdbc.Driver
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123

mybatis:
  type-aliases-package: com.msb.pojo
  mapper-locations: classpath:com.msb.mapper/*.xml

10.结果

 报错;mapper 接口已经加了@Mapper注解,UserServiceImpl仍然报错

解决办法:

idea 按快捷键 Ctrl+shift+Alt ,将代码校验严格程度跳到中间即可:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5137918.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存