基于IDEA的SpringBoot项目创建(二)

一、SpringBoot的相关依赖

在上一篇文章中我们说过,SpringBoot框架其开发目的主要是简化搭建Spring相关开发环境,其实SpringBoot还有一个另一个功能,使得一直认为搭建SpringBoot框架的代码十分繁琐的小伙伴们直呼真香。那么接下来我们就来看一看究竟是什么功能让大家逃过不掉“真香定律”呢?

(一)搭建SpringBoot项目的依赖配置以及配置文件

                      1、依赖配置:

                               上一篇文章中已经带领大家搭建好了一个最简单的SpringBoot项目,接下来我们将延续上一篇文章的项目继续搭建一个SpringBoot项目标准的目录结构:

                         首先请各位先回忆一下上次构建的项目目录结构。闲话少说,上图!

【注】:resources目录下的  static 和 templates 文件夹中前者放入静态效果,后者放入动态页面。

                              接下来我们要进行的是一站式开发—— 开启Spring全家桶的急速搭建环境模式:

                              首先,打开 porm.xml  文件中,我们看到之前已经继承过SpringBoot父项目的配置,那么我们要想整合SSM框架要怎么做呢?

                              其实很简单,在普通Maven工程中,我们构建SSM框架的支持,肯定要引入好多相关依赖,许多小伙伴背了好久的 依赖引入文件。现在不用了,SpringBoot已经帮我们把各层所需要的相关依赖整合完毕。比如:曾经我们要引入MyBatis作为项目的持久层框架,我们需要引入许多Maven配置,那么现在,我们只需要 引入  mybatis-spring-boot-start  这个依赖就可以将MyBatis引入,并且和Spring框架整合在一起。好了,话不多说,注释都在图里,上图!

<dependencies>

    <!--引入MyBatis和SpringBoot整合的配置依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.1</version>
    </dependency>
    <!--引入alibaba数据库连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>
    <!--引入MySQL数据库连接驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>
    <!--引入SpringBoot中的Web层相关依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--引入thymeleaf依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--配置小辣椒依赖-->
    <!--可以用注解将实体注入为JavaBean类-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>

                              【注】:thymeleaf  为前端模板引擎,其使用方法和JSP相差不大,且更加轻量级,后续文章中会有讲解,在此不做赘述

                                            小辣椒是第三方工具,主要作用是解决实体类的属性问题,特别方便,一会讲解,大家稍安勿躁

                              现在大家可以看到,我们只需要这几个依赖配置就可以解决SSM框架整合问题,是不是很方便?所以作者已经完全抛弃普通的Maven工程,投入SpringBoot的怀抱了。

                              那么好,既然JAR包配置完成了,我们要去解决配置文件的问题,从前的  .xml 文件是不是特别折磨人?很快就不会了,进行下一话题!

 

                       2、配置文件:

                             我们现在知道了入口类的正确生成位置,今天来给大家解释一下其他目录结构得含义:首先我们都知道,src/main/resource 目录下一般都是存放我们的.xml配置文件,然而SpringBoot项目给我们提供了一个更加方便的配置文件的方法:

                               1、基于 .yml  文件的配置

                               2、基于 .properties  文件的配置

                             这两种配置文件的区别主要是基于 .yml 文件的配置中,对语法的要求更加严格,在需要很多配置代码的时候容易人为出现错误,而  .properties  配置文件大家都比较熟悉,语法结构相对简单,不容易出错。而且在IDEA中,SpringBoot的  .properties  文件有自动提示,更加方便我们避免配置文件的错误。所以,在本片文章我会以  .properties  文件的方式对SpringBoot项目进行配置。

                            现在来说一下,在SpringBoot项目中 .properties 文件都可以帮我们进行哪些配置呢?我们依旧上图说话!

                         【注】:代码中注释已经解释清楚,鉴于作者一向很懒,小伙伴们自己看代码,在此不做赘述……嘿嘿

#修改端口号
server.port=8089
#设置访问路径
server.servlet.context-path=/springboot-mybatis
#设置thymeleaf文件的前缀
spring.thymeleaf.prefix=classpath:/templates/
#设置thymeleaf文件的后缀
spring.thymeleaf.suffix=.html

#设置thymeleaf文件前缀后缀的原因是thymeleaf文件无法像Jsp页面一样可以自动找到。

#设置视图转换器
#spring.mvc.view.prefix=/
#spring.mvc.view.suffix=.html
#设置thymeleaf文件的字符编码集
spring.thymeleaf.encoding=UTF-8
#设置页面返回值类型
spring.thymeleaf.servlet.content-type=text/html
#设置静态资源路径
spring.resources.static-locations=classpath:/templates/,classpath:/static/

#配置SpringBoot数据源
#SpringBoot 的优点是  自动配置  自动注入   如果测试架构时,没有配置数据源  则可能会报错
#因为  SpringBoot自动注入的时候回扫描注入的数据  如果为空  则不允许运行

#配置数据源类型   alibaba的数据缓冲池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#配置数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/class1
spring.datasource.username=root
spring.datasource.password=123123
#spring.datasource.max-idle=10
#spring.datasource.max-wait=10000
#spring.datasource.min-idle=5
#spring.datasource.initial-size=5

#配置MyBatis的mapper配置文件
mybatis.mapper-locations=classpath:/com/lee/mapper/*.xml
mybatis.type-aliases-package=com.lee.entity

二、简单地MVC三层架构:

                             由于SpringBoot中的三层架构只有相应的注解以及SpringBoot推荐的thymelea前端模板引擎有少许区别,所以这里将代码和工程目录放在上面,thymeleaf模板引擎会在下一篇文章讲 解,大家可以自行搭建测试(才不是作者懒呢,就不是!!!),闲话少叙,上图!上代码!

                      【注】:大家在service 中注入  mapper  的对象时会有报错,那个可以不需要管。因为IDEA对Spring的检查机制表示并没有找到接口                    的实例化对象,实际上我们已经通过MyBatis对其动态代理出了对象,所以程序可以正常运行。如果看着爆红碍眼的话可以尝试将                                @Autowired注解更改为@Resource注解。

                                 原理:  @Autowired注解是从BeanFactory中通过类型来寻找要被注入的对象,如果没有这个类型的对象的话就会报错;;而@Resource注解则是跳过类型,默认通过对象名)进行注入的,如果没有找到,则会回推上一级,通过类型来注入。而在爆红的地方,我们已经默认将其注入BeanFactory中,所以在BeanFactory中可以找到。


以下为代码:

package com.lee;

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

/**
 * SpringBootApplication注解是
 *  * 该注解为  @EnableAutoConfiguration  和 @ConmponentScan的组合
 *  前者作用是  :配置SpringBoot入口类
 *  后者的作用是:默认扫描当前包以及其子包下的所有类  并根据注解进行自动配置
 *  MapperScan注解的作用是将 持久层的接口扫描并自动配置
 *
 */
@SpringBootApplication
@MapperScan("com.lee.mapper")
public class SpringBoot_MyBatisApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringBoot_MyBatisApplication.class, args);

    }

}

 

 

package com.lee.controller;

import com.lee.entity.Student;
import com.lee.service.StudentService;
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;




//@RestController   该注解的作用是注入  控制器的类   同时  将当前控制器类的所有Handler的返回值都以Json的格式返回到页面上
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/getAllStudent")
    public String getAllStudent(Model model){

        List<Student> students = studentService.findAllStudnent();
        model.addAttribute("students", students);
        return "index";

    }

    @RequestMapping("/getStudentById")
    public String getStudentById(Student student, Model model){

        model.addAttribute("student",studentService.findStudentById(student));
        return "index";
    }


}
package com.lee.service;

import com.lee.entity.Student;

import java.util.List;

public interface StudentService {

    List<Student> findAllStudnent();

    Student findStudentById(Student student);

}
package com.lee.service.impl;

import com.lee.entity.Student;
import com.lee.mapper.StudentMapper;
import com.lee.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
//  该注解  为开启事务的注解
@Transactional
public class StudentServiceImpl implements StudentService {

    //@Resource : Resource默认按照id找  注入的Bean  如果没找到  则回退为按照类型找Bean  如果还找不到则抛出异常
    @Autowired // Autowired   按照类型来寻找   找不到则抛出异常
    private StudentMapper studentMapper;

    @Override
    //@Transactional   该注解也可以在方法名上打注解
    public List<Student> findAllStudnent() {
        return studentMapper.selectAllStudent();
    }

    @Override
    public Student findStudentById(Student student) {
        return studentMapper.selectStudentById(student);
    }
}
package com.lee.mapper;

import com.lee.entity.Student;

import java.util.List;

public interface StudentMapper {

    List<Student> selectAllStudent();

    Student selectStudentById(Student student);

}

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.lee.mapper.StudentMapper">
    
    <select id="selectAllStudent" resultType="com.lee.entity.Student">

        select * from student

    </select>
    <select id="selectStudentById" resultType="com.lee.entity.Student">

        select * from student
        where
        sid = #{sid}

    </select>

</mapper>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <ul th:each="student:${students}">

        <li><span th:text="${student.sid}"></span></li>
        <li><span th:text="${student.sname}"></span></li>
        <li><span th:text="${student.sage}"></span></li>
        <li><span th:text="${student.sgender}"></span></li>

    </ul>

    <a th:href="@{/student/getStudentById(sid=12)}">点击查询一个学生</a>

    <ul th:if="${student}"> 
        <li><span th:text="${student.sid}"></span></li>
        <li><span th:text="${student.sname}"></span></li>
        <li><span th:text="${student.sage}"></span></li>
        <li><span th:text="${student.sgender}"></span></li>
    </ul>

</body>
</html>

 


                       其实在IDEA中我们有另一种更加便捷的方式来搭建SpringBoot项目。接下来,请看图!!!

                               

                                这个方法有别于上一篇文章构建项目的方式。在IDEA中我们可以用一个十分快捷的工具来构建SpringBoot项目,方便到不需要一行代码就可以构建出标准的工程目录。闲话少叙,上图!

                          步骤一:我们依旧是创建新建一个Module,不再赘述。

                        步骤二:在这里我们要找到图示的位置。

                                      要主要说明的是:

                                                           Spring Initializr 是帮我们快捷的生成一个SpingBoot项目的一种便捷工具。

                                                           标号 3 的位置代表我们要使用Spring官网的镜像来引入相关依赖。

                        上一步一直卡在加载界面的小伙伴们不要着急,因为Spring是国外产品,所以使用官网镜像会比较慢。在这里推荐大家使用另一个网址,也就是Spring在国内的代理镜像:http://maven.aliyun.com/  这样可以快很多,而且阿里云代理的Spring国内镜像为我们提供了相关的中文注释,对于英语不太擅长的小伙伴是一个福音。

                        步骤三:这里就是单纯的设置项目名以及Maven项目的三坐标。需要注意的是,上一篇文章我们说过SpringBoot 项目是基于JAR包来启动的,所以  标注 1 号  要选择为Jar包; SpringBoot对JDK的最低版本要求是Java8(JDK1.8)版本,我在这里使用的 JDK1.8_131,有需要的小伙伴可以来我这里下载,目测这篇文章更新结束半小时后就会有JDK安装的博客,后续也会有各种编译软件的破解或者开源资源。

                        咳咳,跑题了,上图!

 

                       步骤四:到了这个页面,发现好多选项(头痛),不过仔细一看全都是各种依赖嘛。这下一目了然,我们可以直接通过鼠标点击的方式,轻轻松松地将我们所需要的依赖引入,简直不要太轻松。

               【注】:1、需要注意的是图示 标注 1 号  :这个是我们的SpringBoot版本,在这里我使用的是 2.3.5版本,小伙伴们可以根据自己的需求去选择。但是建议大家不要选择2.2.7以下的版 本,有些功能优化的还不是很好,有兴趣的小伙伴可以去了解一下。

                              2、在这里我们需要引入的依赖如图所示,这几个依赖我们都可以通过搜索栏中搜索找到

                      好了,点击finish 后我们就可以抽根烟等待IDEA为我们搭建项目了(最近作者抽烟有点多,经常被女盆友打,哭唧唧)

                      步骤五:不知道有没有小伙伴注意到,我们在刚刚搭建项目的时候是不是少了几个J依赖,其实剩下的两个依赖并不能通过快捷工具为我们引入(作者才不会承认忘说了呢),其实 剩下两个依赖,一个是 mybatis-spring-boot-start(Mybatis整合Spring依赖) 以及 druid (阿里巴巴数据库连接池),代码自己去上面copy,作者懒……。

                      现在让我们看一下工程目录,所有的目录都有了,妈妈再也不用担心我忘记创建文件夹了,开不开心?

(一)SpringBoot项目测试

                      接下来我们依旧来测试一下我们的SpringBoot项目是否搭建成功,测试方法从前一样,在全局启动入口类 Application ,等待出现logo,我们就可以确认是否启动成功了。这次我们来访问一下路径,看看能否成功跳转页面在浏览器地址栏中输入   localhost:8089/springboot-mybatis/student/getAllStudent  看看页面是否有显示?没有显示的小伙伴们不要着急,请检查路径是否错误、代码有没有敲错、数据库密码是不是忘了改了?最主要的是

 

 

 

 

 

 

 

 

 

 

 

 

 

               你数据库创建了吗?

        

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值