Idea中springboot项目初搭建

前言:

  最近想学习一下springboot,所以通过网上各种找前人铺的路,虽然有的博客写得不是很完整,比如在新建类文件的路径啊,配置文件中的配置项的介绍上,有点不完整,但是通过各种找,最终也算是完成了第一步,从springboot项目创建,到工程连接数据库,到展示到前端界面,初步完成,因此做一个简答的记录。

1:创建项目

创建项目过程其实比较简单,就是按部就班走,这里我就按照博客推荐的进行新建了。建议参考

博客地址:通过IDEA搭建springboot项目(maven方式)_智Min的博客-CSDN博客 中的【IDEA 快速搭建Spring Boot】这一步。不在进行详述。

(博主的项目建设步骤和配置讲解相对还是比较全,不足点是最后新建文件的路径未明确和整体文件内容不能复制。在此还是感谢博主贡献)

到此呢,一个springboot项目就新建成功了。

2:连接数据库

2.1:首先需要配置数据库依赖依赖,需要在pom文件中配置mysql和mybatis依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
</dependency>

放在<dependency></dependency>标签内

2.2:配置数据库连接

在.properties文件中配置数据的连接地址,用户名,密码,连接数等

#连接地址:端口号/实例名
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/integrated_core
spring.datasource.username=root
spring.datasource.password=111111
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

2.3:编写数据库表模型类,mepper数据层访问类,controller数据控制类。

CifClientController 类内容:

package com.test.springbootbest.controller;

import com.test.springbootbest.mapper.CifClientMapperImp;
import com.test.springbootbest.model.CifClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CifClientController {

    @Autowired
    private CifClientMapperImp cifClientMapper;

    @RequestMapping("getCifClient")
    public String listCifClient(Model model){
        List<CifClient> all = cifClientMapper.findAll();

        //jsp文件中可通过cicClients获取查询结果
        model.addAttribute("cicClients",all);
        //listCifClients 为跳转jsp界面的文件名
        return "listCifClients";
    }
}

CifClientMapperImp类:

package com.test.springbootbest.mapper;

import com.test.springbootbest.model.CifClient;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CifClientMapperImp {
//查询cif_client表全部数据
    @Select("select * from cif_client")
    List<CifClient> findAll();
}

CifClient类:

package com.test.springbootbest.model;


import java.io.Serializable;
/**
 * 数据库表模型类
 * 属性名称与表中字段名称要一致
 */
public class CifClient implements Serializable {
    private static final long serialVersionUID = 4780975752032126427L;

    private String CLIENT_NO;
    private String CLIENT_NAME;

    public String getCLIENT_NO() {
        return CLIENT_NO;
    }

    public void setCLIENT_NO(String CLIENT_NO) {
        this.CLIENT_NO = CLIENT_NO;
    }

    public String getCLIENT_NAME() {
        return CLIENT_NAME;
    }

    public void setCLIENT_NAME(String CLIENT_NAME) {
        this.CLIENT_NAME = CLIENT_NAME;
    }
}

3:访问界面并查询数据库返回查询数据展示在界面

3.1:配置依赖

pom.xml文件添加tomcat依赖,servlet依赖

<!-- servlet依赖. -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
 
<!-- tomcat的支持.-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

3.2:编写前端界面

listCifClients.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<table align='center' border='1' cellspacing='0'>
    <tr>
        <td>client_no</td>
        <td>client_name</td>
    </tr>
    <c:forEach items="${cicClients}" var="s" varStatus="st">
        <tr>
            <td>${s.CLIENT_NO}</td>
            <td>${s.CLIENT_NAME}</td>
        </tr>
    </c:forEach>
</table>

需要主要的点:jsp文件中数据依赖controller类model类中塞值的key。获取属性的名称需和模型类中属性一致。模型类中属性名需和表中字段名称相同

至此,启动工程访问,访问:127.0.0.1:8080/getClinets,出现查询到表数据展示。

数据库数据:

 成功界面:

 初学阶段,本博客作为一个记录。有错误的地方希望各位予以指点。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值