This article for the rapid construction of Java project, the novice for reference, if there is an error, please correct.

1. Install the software and configure the environment

Install JDK8, install Maven, and install the development tool IDEA

1.1 installation jdk8

Download it from the official website and install it. Click Next all the time during the installation.

1.2 Configuring JDK Environment Variables

Basically, set three environment variables, JAVA_HOME, Path, and CLASSPATH

At the end of this article, type Java -version in the command box. If you get the Java version information, you are done.

1.3 install maven

Download from the official website, download the compressed package and decompress it.

1.4 Configuring Maven Environment Variables

Basically, set two environment variables, MAVEN_HOME and Path

Refer to this article

Finally, type MVN –version in the command box to display maven version information, and you are done.

1.5 Modifying the Maven Local Repository and Setting the Maven domestic mirror

Modify maven local repository: Open conf\settings. XML in the unzipped Maven folder and add

D:\maven\MyRepository
to the < Settings > tag.

Set domestic mirrors: Add mirrors to the “mirrors>” section

<mirror>

<id>nexus-aliyun</id>

<mirrorOf>*</mirrorOf>

<name>Nexus aliyun</name>

The < url > maven.aliyun.com/nexus/conte…

</mirror>

1.6 IDEA Download and Install

Download and install from the official website, select the installation path, and then proceed to the next step. Do not open the software after the installation is complete. Crack: this crack is more troublesome, can search for tutorial (a lot of tutorial can’t crack), or some treasure to buy activation code (ten dollars of appearance). Idea without cracking can be used for free for 30 days. If you continue to use idea without cracking after 30 days, you will exit every 30 minutes.

Of course, it is best to support the legal version, buy the legal version.

Mysql > create an empty mysql database (mytest, root, 123456);

2. Generate a SpringBoot project

Open the idea

Since it is a new project, click on this to download the related dependencies as shown below:

Write the following configuration:

Access the root path

# app name

spring.application.name=springboot-demo

Access port number

server.port=6666

# Encoding format

server.tomcat.uri-encoding=utf-8

# Database configuration

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/mytest? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai

spring.datasource.username=root

spring.datasource.password=123456

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

DDL -auto=update; spring.jpa.hibernate. DDL -auto=update

spring.jpa.show-sql=true

#session lifecycle

server.servlet.session.timeout=30m

Then, create a new Java class as follows:

package com.mytest.test.demo.controller;

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

/**
 * @author Frank
 * @date 2020/5/10
 */
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {return "Hello World 66666666666666666"; }}Copy the code

Type http://localhost:6666/hello in the browser can access the interface, the following figure:

A very simple interface is created above, which is a string returned.

Now, for database tables, do a slightly more complicated one.

1. Create a Java class

package com.mytest.test.demo.entity;


import javax.persistence.*;

/**
 * @author Frank
 * @date 2020/5/10
 */
@Entity
@Table(name="my_test_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String sex;
    private Integer age;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) { this.age = age; }}Copy the code

2. Create a repository class as follows:

package com.mytest.test.demo.repository;

import com.mytest.test.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

/**
 * @author Frank
 * @date 2020/5/10
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long>,
        JpaSpecificationExecutor<User>{
}

Copy the code

3. Create a service class as follows:

package com.mytest.test.demo.service; import com.mytest.test.demo.entity.User; import com.mytest.test.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author Frank * @date 2020/5/10 */ @Service @Transactional(rollbackFor = Exception.class) public class UserService  { @Autowired private UserRepository repository; @Transactional(readOnly = true)
    public List<User> findAll() {returnrepository.findAll(); }}Copy the code

4. Create a Controller class as follows:

package com.mytest.test.demo.controller;

import com.mytest.test.demo.entity.User;
import com.mytest.test.demo.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.ResponseBody;

import java.util.List;

/**
 * @author Frank
 * @date 2020/5/10
 */
@Controller
public class UserController {
    @Autowired
    private UserService service;
    @ResponseBody
    @RequestMapping("/users")
    public List<User> hello() {returnservice.findAll(); }}Copy the code

Click on the green triangle and run the project. You can see that the database will automatically create tables. Then I manually wrote several pieces of data in the database so that the interface could access several pieces of data, as shown below:

http://localhost:6666/users

This interface completes one of the query operations in the add, delete, change, and query of the User object.