Introduction to mybatis - idea

01? 2022-02-13 07:30:10 阅读数:814

introduction mybatis idea

1、 establish maven engineering

 Wechat screenshot _20211216222007.png

 Wechat screenshot _20211216222117.png

 2、 To configure pom.xml file , Remember to refresh after configuration maven engineering

 <dependencies>
<!-- mysql drive -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!--mybatis what is needed jar package -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<!-- Solve the problem of resource export -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
Copy code 

3、 stay resoources Create mybatis-config.xml File and configure

 Wechat screenshot _20211216222804.png

<?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"/>
<!-- Connect to database , The database USES mysql8.0-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ Database name ?useSSL=false&amp;allowPublicKeyRetrieval=true&amp;serverTimezone=UTC"/>
<property name="username" value=" Database user name "/>
<property name="password" value=" Database password "/>
</dataSource>
</environment>
</environments>
<!-- register mapper, Each one needs to be registered -->
<mappers>
<!-- dao/UserMapper.xml by UserMapper.xml Class name -->
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>
Copy code 

4、 Write tool class MyBatisUntil, stay java Next create a until package , Create in package MyBatisUntil class

package until;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
// Tool class
public class MyBatisUntil {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// obtain sqlSessionFactory object
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
// establish sqlSession object
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
Copy code 

5、 Start test case , establish mybatis database , Value to create the database user surface 6、java Create pojo package ,pojo Create User class

package pojo;
public class User {
private int id;
private String name;
private String pwd;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + ''' +
", pwd='" + pwd + ''' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
}
Copy code 

7、 establish dao package , Create an interface UserDao

package dao;
import pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
Copy code 

8、 stay dao Create UserDao Interface correspondence xml The configuration file UserMapper.xml

<?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">
<!-- Binding correspondence mapper Interface , Bind here UserDao Interface -->
<mapper namespace="dao.UserDao">
<!-- Query statement ,id Corresponding UserDao Method name ,resultType Is the full class name -->
<select id="getUserList" resultType="pojo.User">
select * from mybatis.user
</select>
</mapper>
Copy code 

Be careful UserMapper.xml Need to MyBatis-config.xml To register

 Wechat screenshot _20211216224251.png

 9、 Test case , stay test Down to java Create dao package , Package created under UserDaoTest class

 Wechat screenshot _20211216224329.png

package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import until.MyBatisUntil;
import java.util.List;
public class UserDaoTest {
@Test
public void test1(){
// obtain sqlSession object
SqlSession sqlSession = MyBatisUntil.getSqlSession();
// perform
UserDao mapper = sqlSession.getMapper(UserDao.class);
// Loop print find data
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
// close
sqlSession.close();
}
}
Copy code 

Test success

 Wechat screenshot _20211216224540.png

copyright:author[01?],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130730082658.html