Prerequisite preparation
- Install MySQL database
- Create a table in advance
Create a Student database:
// create database 数据库名;
create database student;
Check whether the creation is successful:
show databases;
If the picture below appears, it means the creation is successful.
Then create a student table:
use student;
CREATE TABLE IF NOT EXISTS student(
id INT,
name VARCHAR(25),
age INT,
);
Insert data:
insert into student (id, name, age) values (1, 'tom', 23);
Check whether the data is inserted successfully:
select * from student;
The preparations are done.
Use Maven to create a project
The introduced Mybatis and mysql-connector:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependencies>
File Directory:
Mybatis's main configuration file SqlMapConfig.xml
<?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="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/student"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/radish/dao/IStudentDao.xml"/>
</mappers>
</configuration>
Mapper configuration file of Mybatis: IStudentDao.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">
<mapper namespace="com.radish.dao.IStudentDao" >
<!--查询所有-->
<select id="findAll" resultType="com.radish.domain.Student">
select * from student;
</select>
</mapper>
The IStudentDao code is as follows:
package com.radish.dao;
import com.radish.domain.Student;
import java.util.List;
public interface IStudentDao {
/**
* 查询所有
* @return
*/
List<Student> findAll();
}
Student's code is as follows:
package com.radish.domain;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
test program:
package com.radish;
import com.radish.dao.IStudentDao;
import com.radish.domain.Student;
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.InputStream;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
// 3.使用工厂生成SqlSession对象
SqlSession session = factory.openSession();
// 4.使用session创建Dao的代理对象
IStudentDao dao = session.getMapper(IStudentDao.class);
// 5.执行findAll方法
List<Student> students = dao.findAll();
for(Student student: students) {
System.out.println(student);
}
// 6.释放资源
session.close();
in.close();
}
}
Results of the:
Precautions
- The operation interface name and mapping file of the persistence layer in Mybatis are also called: Mapper. So IUserDao and IUserMapper are the same
- When creating a directory in idea, it is different from a package. When the package is created: com.radish.dao is a three-level structure, and the directory is created as a first-level directory.
- The location of the mapping configuration file of mybatis must be the same as the package structure of the dao interface
- The value of the namespace attribute of the mapper tag of the mapping configuration file must be the fully qualified class name of the dao interface
- The operation configuration of the mapping configuration file, the value of id must be the method name of the dao interface