使用IDEA完成一个SpringBoot的demo

打算开始做毕业设计了,写一些博客记录一下做毕业设计的过程。

  前两天从老师那里拿了学长学姐做的非常简陋的代码,配置环境跑了一下,老师找我的时候说还剩下50%的工作,但感觉至少还有70%。

  废话不多说,今天先学习用IDEA做个小demo。

  开发环境的话是windows10、IntelliJ IDEA Community Edition 2020.2.3 x64、JDK11。

  

  一.创建项目

  首先在初始界面New Project。

  使用IDEA完成一个SpringBoot的demo

  

  使用IDEA完成一个SpringBoot的demo

 

  按照网上的教程需要选择spring Initializr,可是我这个社区版并没有,所以需要一个插件。

  返回到开始界面,右下角配置处点击Plugins。

  使用IDEA完成一个SpringBoot的demo

 

 

  下载Spring Assiant。

  使用IDEA完成一个SpringBoot的demo

 

  这样子完成之后新建项目时就会有一个Spring Assistant,默认选项点击next就可以了。

  使用IDEA完成一个SpringBoot的demo

 

  修改一下项目的配置,next。

  使用IDEA完成一个SpringBoot的demo

 

  然后勾选需要的服务,按照网上的说法需要Web下勾选Spring Web ,Template Englines勾选Thymeleaf;SQL勾选:MySQL Driver,JDBC API 和 MyBatis Framework三项;点击next;

  我这个Spring Boot的版本是2.3.5。

  使用IDEA完成一个SpringBoot的demo

 

  最后设置存储路径点击Finish。

  使用IDEA完成一个SpringBoot的demo

 

 

  二.配置项目

  首先看一下创建好项目的结构。

  使用IDEA完成一个SpringBoot的demo

 

 

  先要进行maven仓库的配置,配置maven位置和仓库位置,然后下载更新所需要的jar包。

  使用IDEA完成一个SpringBoot的demo

  使用IDEA完成一个SpringBoot的demo

 

 

  三.正式编写

  在templates文件下新建index.html页面,作为启动的初始页面

  使用IDEA完成一个SpringBoot的demo

使用IDEA完成一个SpringBoot的demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    这里是一个简单的启动页面
</body>
</html>

使用IDEA完成一个SpringBoot的demo

 

 

  在com.wangyu.test下新建controller文件夹,在controller文件夹下建一个简单的HelloController类;(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)

  使用IDEA完成一个SpringBoot的demo

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
    @RequestMapping("/index")
    public String sayHello(){
        return "index";
    }
}

使用IDEA完成一个SpringBoot的demo

 

 

  在resources文件夹下application中先配置DataSource基本信息,application文件有两种文件格式,一种是以.properties为后缀,一种是以.yml为后缀的,两种配置方式略有差别,详情可参考这个网址:https://blog.csdn.net/qq_29648651/article/details/78503853;在这我是用.yml后缀的文件格式。右键application文件选择Refact,选择Rename,将后缀改为yml。

  使用IDEA完成一个SpringBoot的demo

使用IDEA完成一个SpringBoot的demo

spring:
  datasource:
    name: springboottest
    url: jdbc:mysql://localhost:3306/springboottest
    username: root
    password: xxxxxx
    driver-class-name: com.mysql.cj.jdbc.Driver

使用IDEA完成一个SpringBoot的demo

 

  pom.xml文件配置信息。

使用IDEA完成一个SpringBoot的demo

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wangyu</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--thymeleaf模板引擎配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--MyBatis配置-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <!--MySQL数据库配置-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--单元测试配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

使用IDEA完成一个SpringBoot的demo

 

  Bean实体类,依据数据库表,生成set和get方法:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.bean;

public class UserBean {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int 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;
    }
}

使用IDEA完成一个SpringBoot的demo

 

  Mapper层访问数据库接口文件:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.mapper;

import com.wangyu.test.bean.UserBean;

public interface UserMapper {
    UserBean getInfo(String name, String password);
}

使用IDEA完成一个SpringBoot的demo

 

  Mapper层访问数据库实现文件(需在resource包下创建mapper文件夹,然后再创建一个UserMapper.xml.在application配置文件中mybatis:mapper-locations:对应的就是该文件地址),注意<mapper>标签的namespace属性要填写 访问数据库接口类文件路径:

https://www.mcmod.cn/center/180323/#/task/
https://www.mcmod.cn/center/180323/#/home/
https://www.mcmod.cn/center/132811/#/rank/
https://www.mcmod.cn/center/132811/
https://www.mcmod.cn/center/132811/#/comment/
https://www.mcmod.cn/center/132811/#/task/
https://www.mcmod.cn/center/132811/#/home/
https://www.dianyuan.com/people/855523
https://www.dianyuan.com/people/855524
https://www.dianyuan.com/people/855526
https://www.dianyuan.com/people/855527
https://www.dianyuan.com/people/855528
https://www.dianyuan.com/people/855529
https://www.dianyuan.com/people/855530
https://www.dianyuan.com/people/855531

使用IDEA完成一个SpringBoot的demo

<?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.wangyu.test.mapper.UserMapper">
    <select id="getInfo" parameterType="String" resultType="com.wangyu.test.bean.UserBean">
        SELECT * FROM user WHERE name = #{name} AND password = #{password}
    </select>
</mapper>

使用IDEA完成一个SpringBoot的demo

 

  Service层业务接口类编写:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.service;

import com.wangyu.test.bean.UserBean;

public interface UserService {
    UserBean loginIn(String name, String password);
}

使用IDEA完成一个SpringBoot的demo

  

  Service层业务实现类编写,注意要注解@Service,注入Mapper:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.serviceImpl;


import com.wangyu.test.bean.UserBean;
import com.wangyu.test.mapper.UserMapper;
import com.wangyu.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    //将DAO注入Service层
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserBean loginIn(String name, String password) {
        return userMapper.getInfo(name,password);
    }
}

使用IDEA完成一个SpringBoot的demo

 

  项目启动类要添加注解@MapperScan项目启动时扫描mapper接口,否则会报错找不到mapper文件:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test;

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

@SpringBootApplication
@MapperScan("com.wangyu.test.mapper")
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

使用IDEA完成一个SpringBoot的demo

 

  编写测试类,看是否能成功 访问数据库,获取数据库信息:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test;

import com.wangyu.test.bean.UserBean;
import com.wangyu.test.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestApplicationTests {

    @Autowired
    UserService userService;

    @Test
    public void contextLoads() {
        UserBean userBean = userService.loginIn("a","a");
        System.out.println("该用户ID为:");
        System.out.println(userBean.getId());
    }

}

使用IDEA完成一个SpringBoot的demo

 

 

  controller层,注意添加@controller注解,注入Service服务:

使用IDEA完成一个SpringBoot的demo

package com.wangyu.test.controller;


import com.wangyu.test.bean.UserBean;
import com.wangyu.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    //将Service注入Web层
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String show(){
        return "login";
    }

    @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
    public String login(String name,String password){
        UserBean userBean = userService.loginIn(name,password);
        if(userBean!=null){
            return "success";
        }else {
            return "error";
        }
    }
}

使用IDEA完成一个SpringBoot的demo

 

  login.html

使用IDEA完成一个SpringBoot的demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form role="form" action = "/loginIn" method="post">
    账号:<input type="text" id="name" name = "name"> <br>
    密码:<input type="password" id = "password" name = "password"> <br>
    <input type="submit" id = "login" value = "login">
</form>

</body>
</html>

使用IDEA完成一个SpringBoot的demo

 

  success.html 

使用IDEA完成一个SpringBoot的demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
    <h1>登录成功!</h1>
</body>
</html>

使用IDEA完成一个SpringBoot的demo

 

  error.html

使用IDEA完成一个SpringBoot的demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>error</title>
</head>
<body>
    <h1>登录失败!</h1>
</body>
</html>

使用IDEA完成一个SpringBoot的demo

 

  先运行测试类,看是否成功获取数据库信息:

  使用IDEA完成一个SpringBoot的demo

  使用IDEA完成一个SpringBoot的demo

 

  运行TestApplication.java文件,启动项目,进入浏览器输入localhost:8080/login

  使用IDEA完成一个SpringBoot的demo

  使用IDEA完成一个SpringBoot的demo

  

  

  输入账号密码测试网页跳转是否正常。

  使用IDEA完成一个SpringBoot的demo

 

  使用IDEA完成一个SpringBoot的demo

  到这里为止,整个demo就大功告成了。

 

 

 

  参考: