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

springboot分层项目(controller,mapper,service)

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

springboot分层项目(controller,mapper,service)

pom.xml


    4.0.0
    com.example
    Untitled01
    0.0.1-SNAPSHOT
    Untitled01
    Untitled01

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
        
        
            mysql
            mysql-connector-java
        
        
        
            org.projectlombok
            lombok
        
        
        
            org.slf4j
            slf4j-api
            1.7.21
        
        
            org.slf4j
            slf4j-log4j12
            1.7.21
        
    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.example.Application
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    



配置 application.yml
server:
  port: 8086 # 启动端口

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动
    username: root 
    password: 123456
    url: jdbc:mysql://localhost:3306/sell?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  application:
    name: Untitled01
mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml #自动扫描mapper下的xml
  global-config:
    db-config:
      table-prefix: t_ #表浅醉
用户表
CREATE TABLE IF NOT Exists tb_user
(
    id       INT(20)     NOT NULL COMMENT '主键id',
    user_name VARCHAr(30) NULL DEFAULT NULL COMMENT '姓名',
    age      INT(11)     NULL DEFAULT NULL COMMENT '年龄',
    email    VARCHAr(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);
replace into tb_user(id, user_name, age, email)
values (1, 'Jone', 18, 'test1@qq.com'),
       (2, 'Jone', 22, 'test2@qq.com'),
       (3, 'Jone', 23, 'test3@qq.com'),
       (4, 'Jone', 12, 'test4@qq.com'),
       (5, 'Jone', 34, 'test5@qq.com');
主启动类

Application.java

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

首先dao层接口

UserMapper.java

package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.bean.User;
import java.util.List;

public interface UserMapper extends BaseMapper {
    List findUser();
}

然后Service层接口

UserImpl.java

package com.example.impl;

import com.example.bean.User;

import java.util.List;

public interface UserImpl{
    List findUser();
}

接着Service层实现

UserService.java

package com.example.service;

import com.example.bean.User;
import com.example.impl.UserImpl;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService implements UserImpl {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List findUser() {
        return userMapper.findUser();
    }
}

最后Controller层实现

UserController.java

package com.example.controller;

import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public List findUser(){
        return userService.findUser();
    }
}

另外还有

Usermapper.xml




    

完成了,访问接口 运行Application.java里面main方法

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

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

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