In the past, when I started learning the framework, I imported the relevant packages bit by bit on the pom.xml file, which was too cumbersome.
Let's talk about how to use idea to quickly build the SpringBoot+MyBatis project (clean and fast)
1, Create project
Open idea and select create project (take idea 2020.1.2 as an example)
Select Spring Initializr
Select the required module (the selected module will be displayed on the right)
Edit the project name and click finish
In this way, idea will automatically import dependent packages and make the basic directory
In this way, the project is created.
2, Environment configuration
2.1 modify configuration file
Although the configuration file automatically generated by idea is application.properties, I personally created YML (the bottom layer of SpringBoot will parse the application.yml file into application.properties)
When creating the project, we added mysql and mybatis components, so we need to configure the data source, otherwise an error will be reported when the project is started
be careful:
My database is above version 8.0, so the driver uses com.mysql.cj.jdbc.Driver instead of com.mysql.jdbc.Driver, and the url should also be set to useSSL=false
server: port : 8081 spring: datasource: name : test url : jdbc:mysql://127.0.0.1:3306/spring_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true username : root password: root driver-class-name: com.mysql.cj.jdbc.Driver
At this time, do you want to ask, if there are both properties and yml, which one will the project use?
The answer is: properties, because the priority of application.properties will be higher than that of application.yml, provided that it is under the same directory layer.
2.2 mybatis configuration file
Create mybatis-config.xml in the resource directory
The contents are as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- Globally enables or disables any caches configured in any mapper under this configuration --> <setting name="cacheEnabled" value="true"/> <!-- Sets the number of seconds the driver will wait for a response from the database --> <setting name="defaultStatementTimeout" value="3000"/> <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- Allows JDBC support for generated keys. A compatible driver is required. This setting forces generated keys to be used if set to true, as some drivers deny compatibility but still work --> <setting name="useGeneratedKeys" value="true"/> </settings> <!-- Continue going here --> </configuration>
At the same time, mybatis-config.xml is introduced into the configuration file
mybatis: config-location: classpath:mybatis-config.xml
3, Create mapper
3.1 create Bean
I create a user table (t_user) in the database
Now we need to create a user bean first
public class UserBean { private String id; private String name; private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserBean{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
3.2 define Dao interface
Note: use * * @ Mapper * * annotation to initialize in the container
I use the annotation method here, but xml should be used as much as possible in actual development. Although annotation is very convenient, it is difficult to maintain complex sql statements (see mybatis configuration xml)—— IDEA builds mybatis framework DEMO)
@Mapper public interface UserDao { @Select("select id,name,password from t_user where id = #{id}") UserBean getUserById(String id); }
3.3 Controller
Use * * @ Autowired to inject UserDao and @ RequestMapping * * to set the access path
@Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserDao userDao; @RequestMapping(value = "/getData") @ResponseBody public String getUser(){ UserBean userBean = userDao.getUserById("admin"); return userBean.toString(); } }
3.4 static data
Create index.html in the templates directory
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> try </body> </html>
Create a HelloController to jump to the index page when accessing
@Controller public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String Index(){ return "/index.html"; } }
4, Start project
Select DemoApplication and right-click ➡ run
Project start
5, Testing
http://localhost:8081/hello : the page display jumps to index.html
http://localhost:8081/user/getData : displays the queried user information