Preface to SpringBoot framework
SpringB is an open-source Java-based framework for creating microservices. It is developed by Pivotal Team for building standalone production-ready Spring applications. SpringBoot is designed to allow you to run Spring applications as fast as possible and reduce your configuration files as much as possible to simplify development.
1. What is the three-tier architecture in SpringBoot?
Controller layer: (as the name implies, the control layer) controls and processes http requests, transfers its different business types to the Service layer for processing, and returns the data processed by the Service layer to the front end. For starters, a Controller corresponds to a Service, and a Service corresponds to a Dao. Feel how the program progresses layer by layer.
Second, the use of basic annotations of the three-tier architecture
1. Basic annotations in the Controller layer
1.1@Controller
Marked on the class body, declaring that the class is a Controller (equivalent to telling you that I am a controller)
1.2@RequestMapping
Marked on the method body, used to specify the access path
1.3@ResponseBody
Marked on the method body, used to return data to the <body> tag
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/user/query")
@ResponseBody
public String queryUser(Integer id){
return "查询用户的id是"+ id + "用户:" + userService.queryUser(id).toString();
}
}
2. Basic annotations in the Service layer
@Service
Marked on the implementation class of the Service interface, the current class is automatically injected into the Spring container
@Service
public class UserServiceImp implements UserService {
@Resource
private UserDao userDao;
@Override
public User queryUser(Integer id) {
User user = userDao.selectById(id);
return user;
}
}
3. Basic annotations in Dao layer
@Mapper is marked above the interface, used by the framework to find the interface and the xml file of the corresponding interface
Corresponding xml file:
<?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.start.springbootmapper.dao.UserDao">
<select id="selectById"
resultType="com.start.springbootmapper.entities.User">
select uid,username,password from t_user where uid=#{uid}
</select>
</mapper>
3. The corresponding pom file and properties file
properties file:
server.servlet.context-path=/orm spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url= spring.datasource.username= spring.datasource.password= #implements separating management for java files and xml files mybatis.mapper-locations=classpath:mapper/*.xml #output logs to console mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
pom file:
<?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.7.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.start</groupId> <artifactId>springboot-mapper</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mapper</name> <description>springboot-mapper</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- Web dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Mybatis dependency --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- Mysql dependency --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.27</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Summarize
These annotations are the basic annotations in SpringBoot. Through the use of these annotations, you can write a small project to access the database and display the data to the front end.