“This is the fifth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. What is Mybatis?

MyBatis is an excellent persistence layer framework/semi-automatic ORM that supports custom SQL, stored procedures, and advanced mapping. MyBatis eliminates almost all of the JDBC code and the work of setting parameters and fetching result sets. MyBatis can configure and map primitive types, interfaces, and Java POJOs (Plain Old Java Objects) to records in the database via simple XML or annotations.

Advantages:

1. Less code compared to JDBC

2, the simplest persistence framework, easy to learn

3, SQL code from the program code completely separated, can be reused

4, provide XML tags, support the preparation of dynamic SQL

5. Provide mapping labels to support ORM field relational mapping between objects and databases; Supports caching, connection pooling, and database migration

Disadvantages:

1, SQL statement writing workload, high proficiency

2, the database portability is poor, if you need to switch the database, SQL statements will have a great difference

2. Set up Mybatis project

2.1 Main Process

2.2 Import mybatis dependency packages and database drivers

Mybatis </groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> < the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.47 < / version > </dependency>Copy the code

Table 2.3 built

2.4 Creating an entity-class object: userinfo.java

public class UserInfo { private Integer uid; private String username; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }}Copy the code

2.5 Creating a Mapper interface: userInfomapper.java

Public interface UserInfoMapper {// Query UserInfo entity by ID // @select (" Select * from user_info where ID =#{id}") UserInfo selectUser(Integer id); // Insert Integer insertEmp(UserInfo emp); }Copy the code

2.6 Compiling the configuration file: mybatis-config.xml

<? The 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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mysql"/> <property name="username" value="root"/> <property name="password" value="haitaiinc"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.zhl.mapper.UserInfoMapper"></mapper> </mappers> </configuration>Copy the code

2.7 Compiling the XML file corresponding to the Mapper Interface: userinfomapper.xml

<? The 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.zhl.mapper.UserInfoMapper"> <! <select id="selectUser" resultType="com.zhl.entity.UserInfo"> select * from user_info where uid = #{id}  </select> <insert id="insertUserInfo"> INSERT INTO `mysql`.`user_info` ( `username`) VALUES (#{username}); </insert> </mapper>Copy the code

2.8 the test class

public class MybatisTest { SqlSessionFactory sqlSessionFactory; @before public void Before (){// Build SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test(){ try (SqlSession session = sqlSessionFactory.openSession()) { UserInfoMapper mapper = session.getMapper(UserInfoMapper.class); UserInfo userInfo = mapper.selectUser(1); System.out.println(userInfo); }}}Copy the code