springBoot+mybatis

目录

一、目录结构

二、依赖

三、代码及注释记录

1、controller

2、entity的User

3、mapper下的UserMapper

4、UserService

5、Impl

6、Usermapper.xml

7、application.properties

8、请求


一、目录结构

springBoot+mybatis_第1张图片

二、依赖,pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.11
         
    
    com.example
    ie
    0.0.1-SNAPSHOT
    ie
    ie
    
        8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.mysql
            mysql-connector-j
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            com.alibaba
            easyexcel
            2.1.6
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.7.11
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


三、代码及注释记录

1、controller

package com.example.ie.controller;

import com.example.ie.entity.User;
import com.example.ie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: $
 * @Date: $
 * @Param: $
 * @return: $
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUser/{userid}")
    @ResponseBody
    public String getUserData(@PathVariable Integer userid){

        User user = userService.queryUserById(userid);
        return user.toString();
    }
}

浏览器路径请求样例

http://localhost:8080/user/getUser/1

2、entity的User

package com.example.ie.entity;

import lombok.Data;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: $
 * @Date: $
 * @Param: $
 * @return: $
 */
@Configuration
@Data
public class User {
    private int userid;
    private String username;
    private String password;

    public User() {
    }

    public User(int userid, String username, String password) {
        this.userid = userid;
        this.username = username;
        this.password = password;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3、mapper下的UserMapper

package com.example.ie.mapper;

import com.example.ie.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Controller;

/**
 * @Description: $
 * @Date: $
 * @Param: $
 * @return: $
 */
//@Controller
@Mapper
public interface UserMapper {
    User selectUser(@Param("userid") Integer userid);
}

4、UserService

package com.example.ie.service;

import com.example.ie.entity.User;
import org.springframework.stereotype.Service;

/**
 * @Description: $
 * @Date: $
 * @Param: $
 * @return: $
 */

public interface UserService {
    User queryUserById(Integer userid);
}

5、Impl

package com.example.ie.service.impl;

import com.example.ie.entity.User;
import com.example.ie.mapper.UserMapper;
import com.example.ie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: $
 * @Date: $
 * @Param: $
 * @return: $
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User queryUserById(Integer userid) {
        User user = userMapper.selectUser(userid);
        return user;
    }
}

6、Usermapper.xml






    

id为UserMapper中的方法名,如下

//@Controller
@Mapper
public interface UserMapper {
    User selectUser(@Param("userid") Integer userid);
}

resultType为实体的全路径

7、application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/user?serverTimezone=UTC&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:com.example.ie.mapper/*.xml

此处的mapper为自己mapper文件的路径

mybatis.mapper-locations=classpath:mapper/*.xml,

8、请求

http://localhost:8080/user/getUser/1

你可能感兴趣的:(spring,boot,mybatis,java)