MySQL > create spring Boot project

Start Idea.

Click New Project to create a New Project. Select Spring Initializr to create a Spring Boot Project, which needs to select the JDK (Java Development ToolKit) version, and the rest of the options default. Click next.

Clicking Next brings up the screen with Group for the project domain and Artifact for the project name.

For example, com.example is the domain name of example, and demo is the name of the demonstration project. Since my JDK version is 1.8, Java Version (Java version) is 8. The other defaults are ok. Click next

Choose dependencies.

  • Select 2.4.4 for Springboot
  • Select Spring Boot DevTools under Developer Tools
  • Select Spring Web under Web
  • Select MyBatis Framework and MySQL Driver under SQL
  • Click next

Select the project storage directory and click Finish.

Simple Springboot project (based on SSM framework: Spring+SpringMVC+MyBatis)

Configure the application. The properties

/ / MySQL Driver spring. The datasource. DriverClassName = com. MySQL.. JDBC Driver / / MySQL address, which need to fill in the IP address, if be local, fill out the localhost, if for the server, Enter the IP address of the server. // The MySQL port is 3306 // Enter the database name. Spring.datasource. Url = JDBC :mysql:// IP address :3306/ dbdatasource? CharacterEncoding = utf8 & serverTimezone = UTC&rewriteBatchedStatements = true. / / database login user name spring datasource. The username = root / login/password spring. The datasource. Password = root / / springboot port operation for 8888, the default shall generally be 8080, can choose. server.port=8888Copy the code

Create a package name

Create four new packages under the project name.

  • Bean puts entity classes that correspond to key fields in the database
  • Dao puts mapper files, data access layer, and interacts with database.
  • Service Business layer to realize business logic functions. There is also an Impl package that implements the concrete implementation class of the Service.
  • Controller Service control layer. Interfaces required by the front end are implemented at this layer. (For ease of understanding, we will create a new package controller under New Web package)

(3) Create the corresponding code

In practice, the controller interacts with the front and back ends, and the back end provides corresponding interfaces to the front end. The Controller layer calls the Service layer, and the Service calls the DAO layer to query the database and store the data in the User object under the bean package. For example, UserController — >UserService — >UserServiceImpl Implements the method in UserService — >UserMapper to query the data, and new a User to store the data.

The corresponding code is as follows:

bean

package com.example.demo.bean; /** * @author TTH * @date 2021/4/4 */ / Id, name, age, gender in the corresponding database. public class User { private Long id; private String name; private Integer age; private String gender; 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; }}Copy the code

dao

package com.example.demo.dao; import com.example.demo.bean.User; import org.apache.ibatis.annotations.Select; import java.util.List; /** * @author TTH * @date 2021/4/4 */ Look at whether the introduction of org. Apache. Ibatis. Annotations. Select package public interface UserMapper {@ the Select (" Select * from test_user ") List < User >  query(); }Copy the code

service

package com.example.demo.service; import com.example.demo.bean.User; import java.util.List; /** * @author TTH * @date 2021/4/4 */ public interface UserService {// List<User> query(); }Copy the code

serviceImpl

package com.example.demo.service.Impl; import com.example.demo.bean.User; import com.example.demo.dao.UserMapper; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author TTH * @date 2021/4/4 */ @service public class UserServiceImpl implements UserService {// UserMapper implements UserService; // @resource or @autoWired implements UserService @Autowired private UserMapper userMapper; @override public List<User> query() {// Call the query method in UserMapper and return a List of type User. For simplicity, no intermediate variables need to be defined. List<User> User = usermapper.query (); // return user; return userMapper.query(); }}Copy the code

controller

package com.example.demo.web.controller; import com.example.demo.bean.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @author tth * @date 2021/4/4 */ @RestController @RequestMapping("/User") public class UserController { @Autowired private UserService userService; // Indicates that this is a get request, followed by post, delete, etc. @GetMapping("query") public List<User> query(){ return userService.query(); }}Copy the code

Attention! Attention! Attention! We also need to add an annotation @mapperscan to DemoApplication, otherwise the project will still get an error. Or use a MyBatis annotation class.

package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; // This comment is very important, @mapperscan ("com.example.demo.dao") @SpringBootApplication Public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code

Project running

Right click on the DemoApplication file and click Run “DemoApplication” to Run the project.

After the command is successfully executed, open the browser and enterhttp://localhost:8888/User/queryHTTP is the protocol, localhost is the local IP address, 8888 is the running port, and User is the string after the @requestMapping parentheses of the Controller layer, which is the same as the root directory. Query is the string after the @getMapping parentheses of the Controller layer, which is equivalent to the query method in the root directory

The last

At the end of the article the author sorted out a lot of information for you! Including Java core knowledge + a full set of architect learning materials and video + first-line factory interview treasure dictionary + resume template interview ali Meituannetease Tencent Xiaomi IQiyi Quick hand bilibili bilibili interview questions +Spring source code collection +Java architecture actual combat e-books and so on! Friends in need welcome to pay attention to the public number: bright future, receive