Using idea to complete a demo of springboot

itread01 2020-11-07 16:46:45
using idea complete demo springboot


I plan to start my graduation project , Write some blogs to record the process of graduation project .

Two days ago, I took some simple code from my teacher , The configuration environment has run for a while , When the teacher came to me, he said there was still 50% The job of , But it feels like at least 70%.

Not much nonsense , Today, learn to use IDEA Make a little demo.

The development environment is windows10、IntelliJ IDEA Community Edition 2020.2.3 x64、JDK11.

One . Create a project

First, on the initial interface New Project.

 

According to the online tutorial needs to choose spring Initializr, But my community version doesn't have , So we need a plug-in .

Back to the start interface , In the lower right corner, click Plugins.

 

 

Download Spring Assiant.

 

This way, when a new project is created, there will be a Spring Assistant, Select the default option next That's all right. .

 

Change the configuration of the project ,next.

 

Then check the required service , According to the Internet, you need Web Next check Spring Web ,Template Englines Tick Thymeleaf;SQL Tick :MySQL Driver,JDBC API and MyBatis Framework Three items ; Click on next;

This is me Spring Boot The version is 2.3.5.

 

Finally, set the storage path and click Finish.

 

 

Two . Configure projects

First, take a look at the structure of the project .

 

 

First of all maven Warehouse configuration , To configure maven Location and warehouse location , Then download what you need for the update jar package .

 

 

3、 ... and . Officially written

stay templates Create a new file index.html Page , As the initial page to launch

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Here is a simple launch page
</body>
</html>

 

 

stay com.wangyu.test Under the new controller Folder , stay controller Create a simple HelloController Class ;(Controller Class to add @Controller Notes , When the project starts ,SpringBoot It will scan and load automatically Controller)

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";
}
}

 

 

stay resources Under folder application In the first configuration DataSource Basic information ,application There are two file formats , One is to .properties At the end of the word , One is to .yml It's the end of the word , There is a slight difference between the two configurations , Please refer to this website for details :https://blog.csdn.net/qq_29648651/article/details/78503853; Here I use .yml File format at the end of the word . Right click application File selection Refact, Choice Rename, Change the ending to yml.

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

 

pom.xml File configuration information .

<?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 Template engine configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Web Rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--MyBatis To configure -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--MySQL Database configuration -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Unit test configuration -->
<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>

 

Bean Entity class , According to the database table , Generate set and get Method :

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;
}
}

 

Mapper Layer access database interface files :

package com.wangyu.test.mapper;
import com.wangyu.test.bean.UserBean;
public interface UserMapper {
UserBean getInfo(String name, String password);
}

 

Mapper Layer access database implementation file ( Need to be in resource Create... Under the package mapper Folder , And then build another UserMapper.xml. stay application In the configuration file mybatis:mapper-locations: The corresponding file address is ), Be careful <mapper> Labeled namespace Attribute should be filled in   Access database interface class file path :

<?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>

 

Service Layer business interface class :

package com.wangyu.test.service;
import com.wangyu.test.bean.UserBean;
public interface UserService {
UserBean loginIn(String name, String password);
}

Service Layer business implementation class writing , Note that @Service, Inject Mapper:

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 {
// Will DAO Inject Service Layer
@Autowired
private UserMapper userMapper;
@Override
public UserBean loginIn(String name, String password) {
return userMapper.getInfo(name,password);
}
}

 

The project startup class should be annotated @MapperScan Scan when the project starts mapper Interface , Otherwise, it will report an error and cannot find mapper Archives :

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);
}
}

 

Write test class , See if you can succeed Access the database , Access to database information :

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(" The user ID For :");
System.out.println(userBean.getId());
}
}

 

 

controller Layer , Pay attention to the addition of @controller Notes , Inject Service Service :

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 {
// Will Service Inject Web Layer
@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";
}
}
}

 

login.html

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

 

success.html 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
<h1> Login successful !</h1>
</body>
</html>

 

error.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
<h1> Login failed !</h1>
</body>
</html>

 

Execute the test class first , See if the database information is successfully obtained :

 

Execute TestApplication.java Archives , Start the project , Enter the browser and type localhost:8080/login

Enter the account password to test whether the page Jump is normal .

 

So far , The whole demo And it's done .

 

 

 

Refer to :

https://blog.csdn.net/baidu_39298625/article/details/98102453

https://blog.csdn.net/crazilyer/article/details/80313323

https://blog.csdn.net/qq_29648651/article/details/78503853

https://www.cnblogs.com/kaikai-2018/p/13030719.html

https://blog.csdn.net/qq_40902709/article/details/105806376

&n

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云