zoukankan      html  css  js  c++  java
  • springboot+mybatis

    controller内容如下

    UserController

    package com.fengzi.controller;
    import com.fengzi.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    import java.util.Map;
    
    @Controller
    @ResponseBody
    public class UserController {
    
        @Autowired
        private UserService userService;
        @RequestMapping(value = {"/find","/find/{id}"},method = RequestMethod.GET)
        public List<Map<String,Object>> findById(@PathVariable(value = "id", required = false) String id){
            System.out.println(id);
            if (id != null) {
                return userService.findById(id);
            }
            return userService.findAll();
        }
    }

    dao层内容如下

    UserDao

    package com.fengzi.dao;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Mapper
    @Repository
    public interface UserDao {
        @Select("select id,name from user where id=#{id}")
        List<Map<String,Object>> findById(@Param(value = "id") String id);
    
        @Select("select * from user")
        List<Map<String,Object>> findAll();
    }

    service内容如下

    UserService

    package com.fengzi.service;
    import java.util.List;
    import java.util.Map;
    
    public interface UserService {
        List<Map<String,Object>> findById(String id);
        List<Map<String,Object>> findAll();
    }

    service实现类内容如下

    UserServiceImp

    package com.fengzi.service.ServiceImp;
    
    import com.fengzi.dao.UserDao;
    import com.fengzi.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class UserServiceImp implements UserService {
        @Autowired
        private UserDao userDao;
    
        public List<Map<String,Object>> findById(String id) {
            return userDao.findById(id);
        }
    
        public List<Map<String,Object>> findAll() {
            return userDao.findAll();
        }
    }

    application.yml内容如下

    spring:
      datasource:
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://134.175.85.103:3306/testdb
    server:
      port: 7001
    logging:
      level:
        com.fengzi.dao: debug //可以显示sql语句

    最后文件结构如下

  • 相关阅读:
    python-----删除文件到回收站
    python-----列表生成式和列表生成器表达
    html5 标准文档结构
    java基础之 类型转换
    java基础之 数据类型
    java基础之 变量
    java基础之 开发环境配置
    通用登录界面1.1
    mysql获取字段信息
    喵喵喵?
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/15487851.html
Copyright © 2011-2022 走看看