Springboot mvc + mybatis + mysql backend project
Foreword : Due to the school curriculum arrangement, we are about to learn such a course, so I want to write a blog to facilitate everyone to create their own springboot backend project!
1. I created using idea.
Click file-> new-> project- > Spring initalizr to
choose my own jdk, then click next
2.
Next is done
3.
Select sql, select Mysql driver, select mybatis famework and next !
4. Just initialize in your own directory!
**二、初始化架构**
1、现在你可以在你的main.java.com.example.demo里创建一个controller包,在包里创建一个class,在类名的上面写上一个@RestController注解,一般情况下是自动导入包的,如果没有的话鼠标点到注解,按alt+enter会给一个提示,然后Import就完事了!
2、在类里写一个方法
E.g:
@RequestMapping("/")
public String test(){
return "Hello World!"
}
Note that the @RequestMapping annotation here is very important. The "/" in the quotation marks behind him indicates the address of this interface. After you run up, if you report no errors, enter it in the browser.
http://localhost:8080/
If Hello World! Appears, it means success!
Third, the configuration file and mybatis use
1, let us first configure the basic database and port number
under your java, there is a resources file under the application file, right-click refactor, rename, change the suffix to yml, as for why Well, it may be because yml is more convenient. . . .
(1) Configure the port
server:
port: 8080
(2) Configure the database
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: root
(3) Configure mybatis
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: cn.boot.domain
The first port represents the port of your entire back-end project, you can not be the same as me, the second database configuration:
here indicates the name of your database, basically the same elsewhere, then your database name and password!
Next is mybatis. The first mapperlocations indicates the storage address of your mybatis file. If you choose to be the same as me, you can create a new mapper file under the resources file, and then your mybatis file can be put here to identify it yourself.
Create a new mybatis file
If you are a newbie, you need to create a new template for mybatis file, click File-> new-> edit new templates,
click the plus sign, enter mybatis in the name, enter xml in the extensionsion,
then you can use the template mine:
<?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="">
</mapper>
Choose ok!
There is a new mybatis file in your mapper file,
Fourth, create the entire mvc framework
Now, create a few packages in your com.example.demo, entity, controller, service, dao
prompt, here your entity represents the entity, service represents the service layer, used for application processing, dao The layer has only an interface and is mapped with the mybatis file!
My structure is like this. In
this case, we first use it to test without using entities. Create an interface in dao and type
@Mapper
@Component
These two notes.
Let's write a method.
public String getNameById();
Create an interface in service, such as userservice
write
public interface userService {
public String getNameById();
}
Create another serviceimp package in the service package and create userServiceImp file
@Service
public class userServiceImp implements com.example.demo.service.userService {
@Autowired
private com.example.demo.dao.userDao userDao;
@Override
public String getNameById() {
return userDao.getNameById();
}
}
Note that the @Service annotation is added before the class here, and automatic injection is written in the class. Then it is enough when implementing the interface! Then logically we can directly call the dao layer method!
Then in our controller layer, create a new userController file
@RestController
public class userController {
@Autowired
private userService userService;
@RequestMapping("/")
public void test() {
System.out.println(userService.getNameById());
}
}
Old rules,
before we write @RestController method, we write
@Autowired
private userService userService;
with
@RequestMapping("/")
Then, the highlight is coming, we got it in the newly created mybatis file. Note that in the mapper tag we need to write the absolute path to the namespace attribute, pointing to your dao layer, if you follow my steps step by step, you can write
namespace="com.example.demo.dao.userDao"
Here is how to use mybatis file. Mybatis is like a label. One label per sentence, there are 4 corresponding ones, add, delete, change, insert, delete, select, update.
Here we write
<select id="getNameById" resultType="String">
SELECT name FROM test WHERE id = 1
</select>
Since there are only two fields in my database, so test directly! Note that the id here corresponds to the name of the method in your dao layer code.
Here, we have basically completed the entire set, and then ran up and entered
my database in the browser :
http://localhost:8080/
Then click on your idea, you can see the test data!
Mine is like this! !
Then the construction of the springboot backend management is here, and then I will build a front-end construction of the vue family bucket, if you need it, please continue to pay attention to the blogger! !
If you think my blog helps you, please give a little encouragement in the comment area. Your encouragement is the source of my motivation. Hahahaha.