Set up Mybatis development environment and entry project in detail (must be)

Create a maven project

Create the mybatis01 project, the project information is as follows:

Groupid:com.itheima
ArtifactId:mybatis01
Packing:jar

Add the coordinates of Mybatis3.4.5

Add the coordinates of Mybatis3.4.5 in the pom.xml file, as follows:

<dependencies>
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.4.5</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.10</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.6</version>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.12</version>
 </dependency>
 </dependencies>

Write User entity class

/**
* 
* <p>Title: User</p>
* <p>Description: 用户的实体类</p>
* <p>Company: http://www.itheima.com/ </p>
* 
*/
public class User implements Serializable {
    
    
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
    
    
return id;
}
public void setId(Integer id) {
    
    
this.id = id;
}
public String getUsername() {
    
    
return username;
}
public void setUsername(String username) {
    
    
this.username = username;
}
public Date getBirthday() {
    
    
return birthday;
}
public void setBirthday(Date birthday) {
    
    
this.birthday = birthday;
}
public String getSex() {
    
    
return sex;
}
public void setSex(String sex) {
    
    
this.sex = sex;
}
public String getAddress() {
    
    
return address;
}
public void setAddress(String address) {
    
    
this.address = address;
}

@Override
public String toString() {
    
    
return "User [id=" + id + ", username=" + username + ", birthday=" + birthday+ ", sex=" + sex + ", address="+ address + "]";
}
}

Write the persistence layer interface IUserDao

The IUserDao interface is our persistence layer interface (also can be written as UserDao or UserMapper), the specific code is as follows:

/**
* 
* <p>Title: IUserDao</p>
* <p>Description: 用户的持久层操作</p>
* <p>Company: http://www.itheima.com/ </p>
*/
public interface IUserDao {
    
    
/**
* 查询所有用户
* @return
*/
List<User> findAll();
}

Write the mapping file IUserDao.xml of the persistence layer interface

Insert picture description here

<?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.itheima.dao.IUserDao">
<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.itheima.domain.User">
select * from user
</select>
</mapper>

Write the SqlMapConfig.xml configuration file

<?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>
<!-- 配置 mybatis 的环境 -->
<environments default="mysql">
<!-- 配置 mysql 的环境 -->
<environment id="mysql">
<!-- 配置事务的类型 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置连接数据库的信息:用的是数据源(连接池) -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ee50"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<!-- 告知 mybatis 映射配置的位置 -->
<mappers>
<mapper resource="com/itheima/dao/IUserDao.xml"/>
</mappers>
</configuration>

Write test class

/**
* 
* <p>Title: MybatisTest</p>
* <p>Description: 测试 mybatis 的环境</p>
* <p>Company: http://www.itheima.com/ </p>
* 
*/
public class MybatisTest {
    
    
public static void main(String[] args)throws Exception {
    
    
//1.读取配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建 SqlSessionFactory 的构建者对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.使用构建者创建工厂对象 SqlSessionFactory
SqlSessionFactory factory = builder.build(in);
//4.使用 SqlSessionFactory 生产 SqlSession 对象
SqlSession session = factory.openSession();
//5.使用 SqlSession 创建 dao 接口的代理对象
IUserDao userDao = session.getMapper(IUserDao.class);
//6.使用代理对象执行查询所有方法
List<User> users = userDao.findAll();
for(User user : users) {
    
    
System.out.println(user);
}
//7.释放资源
session.close();
in.close();
}
}

summary

Through the quick start example, we found that using mybatis is very easy,
because only need to write the Dao interface and write two configuration files according to the requirements of mybatis to achieve the function.
It is much more convenient than our previous jdbc.

(After we use annotations, it will become easier, just write a mybatis configuration file is enough.)

However, it contains many details, such as why there is a factory object (SqlSessionFactory),
why there is a builder object (SqlSessionFactoryBuilder) after a factory,
why IUserDao.xml has location and file name requirements when it is created, etc. .
Please note: We explain the custom Mybatis framework, not for everyone to go back and write a mybatis,
but for us to better understand how mybatis is executed internally,
and to better use the mybatis framework in future development.
At the same time Have an understanding of its design concept (design pattern)

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/111997899