Integrated persistence layer framework Mybatis
- Integrated Mybatis
Introduce dependency<!-- integrate mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!-- integrate mysql connect --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency>
Configure data source# Add database connection spring.datasource.url=jdbc:mysql://localhost:3306/xxx?serverTimezone=GMT%2B8 spring.datasource.username=xxx spring.datasource.password=xxx spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- Using Mybatis
Configure all mappers of mybatis XML path
mybatis.mapper-locations=classpath:/mapper/**/*.xml
Use @ MapperScan to scan all Mapper interfaces
@MapperScan("com.xxx.xxx.mapper")
Naming of interfaces and XML -
Use persistence layer
Create a service package and inject mapper into
Use the @ Service or @ RestController annotation to hand over the Service class or Controller class to Spring for management
Inject one class into another using @ Resource (JDK) or @ Autowire (spring)
Integrate Mybatis official code generator
Add plug-in dependency
<!-- mybatis generator Automatic code generation plug-in --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <configurationFile>src/main/resources/generator/generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> </plugin>
Add the configuration file generator config xml
Add execute command
The back-end interface returns parameters uniformly to facilitate the front-end unified processing
CommonResp
Encapsulate request parameters and return parameters
Encapsulate all query parameters into an EbookReq class, which is similar to the Ebook table
Encapsulate all return parameters into a class EbookResp, which is similar to Ebook table
Now the return parameter is commonresp < list < ebook > > ebook, but sometimes the return value does not correspond one-to-one with eBook (for example, the password is generally not returned directly to the front end, so it does not correspond one-to-one with the database). Under normal circumstances, there is no special field that corresponds one-to-one.
The list < ebook > returned by the persistence layer needs to be converted to list < ebooresp > and then returned to the controller
Summary of common interview questions
1. What are the common persistence layer frameworks?
Mainly Mybatis,Hibernate Two persistence layer frameworks, the former is semi-automatic and the latter is fully automatic
2. What is semi-automatic? What is fully automatic?
Semi automatic persistence layer framework sometimes requires programmers to write some SQL statements with high flexibility, such as Mybatis.
The fully automatic persistence layer framework does not require programmers to write SQL statements. Almost everything can be done by the framework, which is very worry-free and easy
3. What's the difference between Mybatis and Hibernate?
Mybatis: programmers need to write SQL statements, which can strictly control the performance of SQL execution and has high flexibility. But the data independence is poor. If it is a variety of databases, each database needs to write special SQL statements, which is very troublesome.
Hibernate: there is no need for programmers to write SQL statements. It has good data independence. It can adapt to projects with multiple database types, but its execution performance is worse than that of Mybatis.
4. What is the difference between generics and objects?
There is little difference between generics and Object in use, but generics do not need forced type conversion when used, which is safer at compile time. If you use the Object class, you can't guarantee that the returned type must be the required type, maybe other types. At this time, you will get a type conversion exception (ClassCastException) at runtime
5. Have you ever used generics in your projects and how?
CommonResp<List> resp = new CommonResp<>();
Generics can specify specific data types only when used, which is very convenient