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语句

    最后文件结构如下

  • 相关阅读:
    蓝鲸财经新闻记者实战培训
    雷军解密小米商业内核
    OpenGL6-纹理动画
    OpenGL5-纹理贴图
    OpenGL4-绘制旋转的立方体
    OpenGL3-绘制各种图元绘制
    OpenGL2-绘制三角形
    OpenGL1-创建窗口(建立Win32的OpenGL开发环境 )(代码上传)
    一步步做程序优化-讲一个用于OpenACC优化的程序(转载)
    常用编译选项介绍
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/15487851.html
Copyright © 2011-2022 走看看