Related articles
-
The series begins to update! At least two a week! Welcome to like + follow!
-
MyBatis series summary: MyBatis series
preface
-
MyBatis is an excellent persistence layer framework
-
MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieving result sets.
-
Official documentation: Documentation
-
Making: making
-
Persistence:
- That is, data is permanently saved to a storage device, such as a disk.
- JDBC is also a persistence mechanism. The same goes for file IO.
-
Why is persistence needed?
- Because memory breakpoints are lost, some data must not be lost, such as bank accounts. But there is no guarantee that the power will never go out.
- Memory is expensive and maintenance costs are high because it needs to be powered consistently.
- So we usually persist data in external storage.
First, start the pre-preparation of MyBatis
1.1. Establishment of experimental database
- Simple experiment, just a few simple fields.
1.2. Import MyBatis related JAR packages
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
Copy the code
1.3. Write the core configuration file of MyBatis
<! DOCTYPEconfiguration
PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="JDBC: mysql: / / IP address: 3306 / mybatis? useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dbright/mapper/xml/userMapper.xml"/>
</mappers>
</configuration>
Copy the code
- There’s nothing to explain. They’re all fixed templates, similar to SpringBoot’s configuration, but not as simple as SpringBoot’s
- The only thing to watch out for might be,
mappers
In theresource
This must be consistent with the XML path of the project to map to!
The first MyBatis program
- With the above preconditions complete, you are ready to start development
2.1. Create an entity class
import lombok.Data;
@Data
public class User {
private Integer id;
private String name;
private Integer age;
private String like;
}
Copy the code
2.2, mapper
- mapper
public interface UserMapper {
List<User> getUserInfo(a);
}
Copy the code
- mapper.xml
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dbright.mapper.UserMapper">
<select id="getUserInfo" resultType="com.dbright.pojo.User">
select * from user
</select>
</mapper>
Copy the code
2.3 MyBatis tool class
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "ContextAplication.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch(IOException e) { e.printStackTrace(); }}// Obtain the SqlSession connection
public static SqlSession getSession(a){
returnsqlSessionFactory.openSession(); }}Copy the code
2.4 Junit test class
- pom
<! -- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
Copy the code
- The test class
public class MyTest {
@Test
public void selectUser(a) {
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.getUserInfo();
for(User map: users){ System.out.println(map); } session.close(); }}Copy the code
2.5. Execution result
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah