Springboot用idea写查询数据库程序(详细讲解)

先把代码给大家详细贴以下
首先是连接数据库的application.properties

root和15235068202 是我的账号和密码    8888是访问的端口号    school是我的数据库的 库名字

spring.datasource.url = jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username = root
spring.datasource.password =  15235068202
spring.datasource.driverClassName = com.mysql.jdbc.Driver

server.port=8888
mybatis.mapper-locations=classpath:mapper/*.xml
#spring.mvc.static-path-pattern=/static/**
#logging.level.com.example.thecloudsystem.dao=debug
#logging.level.org.springframework.security=info
logging.level.mapper=DEBUG
#logging.level.com.example.syscloud.dao=DEBUG
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.thymeleaf.enabled=true

需要引入的依赖pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.0.BUILD-SNAPSHOT
         
    
    com.example
    schools
    0.0.1-SNAPSHOT
    schools
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            javax.servlet
            javax.servlet-api
            provided
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        


        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
        
        
        
        
        
            mysql
            mysql-connector-java
            5.1.38
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.github.fge
            json-schema-core
            1.2.5
        
        
            com.alibaba
            fastjson
            1.2.47
        
        
            com.google.guava
            guava
            27.0-jre
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.7
        
        
            org.projectlombok
            lombok
            1.16.20
            provided
        
        
        
            com.github.penggle
            kaptcha
            2.3.2
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            com.auth0
            java-jwt
            3.3.0
        
        
        
            org.quartz-scheduler
            quartz
            ${quartz.version}
        
        
            org.quartz-scheduler
            quartz-jobs
            ${quartz.version}
        

        
        
        
        
        
        
        
        
        
        
        
            org.testng
            testng
            RELEASE
            compile
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    
    
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
    



再简单的贴下我的数据库 从数据直接导出的数据

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50703
Source Host           : 127.0.0.1:3306
Source Database       : school

Target Server Type    : MYSQL
Target Server Version : 50703
File Encoding         : 65001

Date: 2019-06-19 17:45:01
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'root', '123456');
INSERT INTO `user` VALUES ('2', 'httl', '554952');
INSERT INTO `user` VALUES ('3', '刘俊', '3');

Springboot用idea写查询数据库程序(详细讲解)_第1张图片
接下来给大家看下 都需要那些类

Springboot用idea写查询数据库程序(详细讲解)_第2张图片
实体类userBean
get/set 方法

package com.example.demo4.userBean;
import javax.persistence.Entity;
import javax.persistence.Id;

//     @Entity   对实体注释。任何Hibernate映射对象都要有这个注释
//     @Id   声明此属性为主键。该属性值可以通过应该自身创建
//实体类
@Entity
public class userBean {

    @Id
    private Integer id;
    private String name;
    private String password;


    public userBean(Integer id, String name,String password) {
      this.id=id;
        this.name = name;
        this.password=password;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

mapper的数据库语句





    

        
        
        

//man 中的id name password 只是优化数据库的作用
    
    id,name,password


userDao 数据源文件

package com.example.demo4.userDao;


import com.example.demo4.userBean.userBean;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;


//数据源文件
@Repository
@Mapper
public interface UserDao {

    List select(Map userMap);


}

service 业务层

package com.example.demo4.service;


import com.example.demo4.userBean.userBean;

import java.util.List;
import java.util.Map;

public interface service {
    List select(Map userMap);

}

serviceimpl

业务实现层

package com.example.demo4.service;

import com.example.demo4.userBean.userBean;

import com.example.demo4.userDao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class servielimpl implements service {

@Autowired
private UserDao userDao;


    @Override
    public List select(Map userMap) {
        return userDao.select(userMap);
    }
}

testController 直接访问返回

package com.example.demo4.Controller;
import com.example.demo4.service.service;
import com.example.demo4.userBean.userBean;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Controller
@RestController
public class testController {
    @Autowired
    private service service;
    @RequestMapping("/aaa")
    public String sayHello() {
        return "helloword";

    }


Springboot用idea写查询数据库程序(详细讲解)_第3张图片
testController 直接查询数据库数据

package com.example.demo4.Controller;
import com.example.demo4.service.service;
import com.example.demo4.userBean.userBean;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Controller
@RestController
public class testController {
    @Autowired
    private service service;
    @RequestMapping("/aaa")
    public String sayHello() {
        return "helloword";

    }


    @RequestMapping(value = "/fu")
    @ResponseBody
    public List fuzzyCUser(HttpServletRequest request) {
       Map result = new HashMap<>();
        Listlist =service.select(result);
      //  result.put("list",list);
        request.getSession().setAttribute("LIST",list);
      //  return result;
           return list;


    }

Springboot用idea写查询数据库程序(详细讲解)_第4张图片

testController 访问list输出的数据

package com.example.demo4.Controller;
import com.example.demo4.service.service;
import com.example.demo4.userBean.userBean;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Controller
@RestController
public class testController {
    @Autowired
    private service service;
    @RequestMapping("/aaa")
    public String sayHello() {
        return "helloword";

    }


//    @RequestMapping(value = "/fu")
//    @ResponseBody
//    public List fuzzyCUser(HttpServletRequest request) {
//       Map result = new HashMap<>();
//        Listlist =service.select(result);
//      //  result.put("list",list);
//        request.getSession().setAttribute("LIST",list);
//      //  return result;
//           return list;
//
//
//    }

    @RequestMapping(value = "/fuss")
    @ResponseBody
    public Map fuzzyCUser(HttpServletRequest request) {
        Map result = new HashMap<>();
        List list = service.select(result);
        result.put("list", list);
        request.getSession().setAttribute("LIST", list);
        return result;
         
    }


}

Springboot用idea写查询数据库程序(详细讲解)_第5张图片

这样查询就出来了 大家有意见和问题的可以提出来 欢迎讨论。

一个新手、没背景的野鸡大学挣脱出来的野小子,主要以不断
学习,不断记录,用思维认知去看这个世界。写作、阅读、分享,用独自的思考和感悟分享给互联网里的每一位技术人。
不爱学习的我们,无力的挣扎吧!

你可能感兴趣的:(笔记,java,web)