10 years ago to learn the Java foundation, now want to re-learn Java, choose to learn Spring Boot this more popular framework.

In the configuration after start to write the first interface: configuration reference forpastime.com/news/experi…

Possible words are not accurate, please Daniel more guidance!

1. Create “Controller Mapper Model Service” under “demo/ SRC /main/ Java/package name/”. There is no need to create a View directory, because main/resources/ has static resources and a templates directory.

If the database is added to the dependency, you need to configure the database in application.yml before Run. Otherwise, you can access the database in the browser. Localhost: port 8080, there is an error page, but it is not an error, indicating that the project is running properly

Add the @slf4j annotation directly above the DemoApplication class, and then use the log.info() or log.error methods in the main method to write the log to the console. This is easy to go online after viewing and debugging

2) @restController (interface) 3) @slf4j (log) 4) Create new method test() 5) Add annotation @getMapping ("/test") or @PostMapping("/test") above the method to print a log package in the method com.example.demo.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @slf44j @RestController public class TestControllers {@getMapping ("/test") public Object test(){log.info(" requested test method "); return null; }} 7) in a browser requests http://localhost:8080/test IntelliJ console display log: request the test method interface success!!!!!!Copy the code

TestModel class testModel class testModel class testModel class testModel class testModel

@tablename (value = "ha_user_order") The value of the table name above 2) private member variables in the class join @ TableId (type = IdType. AUTO) specified on the primary key import com. Baomidou. Mybatisplus. The annotation. IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName (value = "ha_user_order") public class TestModel { @TableId(type = IdType.AUTO) private Integer id; private Integer userid; private String unionid; private String orderid; TestMethod () public interface TestService {Object testMethod(); } 7. Add the impl package under service and add the TestServiceImpl class file. Select Implement Interface and select the method to override.)Copy the code

1) This class implements the TestService interface and overwrite the testMethod() method. Field tsi in com.example.demo.controller.TestControllers required a bean of type 'com. Example. Demo. Service. Impl. TestServiceImpl' that could not be found. 2) the method of using TestServiceImpl rewrite access database data, Create TestMapper under mapper; create TestMapper under mapper; create TestMapper under mapper; @mapperscan ("com.example.demo.mapper") otherwise error: Field testMapper in com.example.demo.service.impl.TestServiceImpl required a bean of type 'com.example.demo.mapper.TestMapper' that could not be found. import com.baomidou.mybatisplus.core.mapper.BaseMapper; Public interface TestMapper extends BaseMapper<TestModel> {} Then, TestServiceImpl declares the private variable TestMapper and automatically injects TestMapper; QueryWrapper > QueryWrapper = new QueryWrapper<>(); queryWrapper.eq("id",350); Object res = TestMapper.selectList (queryWrapper); Finally, return the query result in JSON format return res; The complete code: package com. Example. The demo. Service. Impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.demo.mapper.TestMapper; import com.example.demo.model.TestModel; import com.example.demo.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestServiceImpl implements TestService { @Autowired private TestMapper testMapper; @Override public Object testMethod() { QueryWrapper<TestModel> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id","350"); Object res =this.testMapper.selectList(queryWrapper); return res; }}, modify TestController eight classes through the service to use the model to obtain the data import com. Example. Demo. Service. Impl. TestServiceImpl; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class TestControllers { @Autowired private TestServiceImpl testService; @getMapping ("/test") public Object test(){log.info(" requested test method "); Object o = testService.testMethod(); return o; }}Copy the code

Job done!