目录
1.添加驱动包 mysql.jar 和 mybatis.jar
2.添加配置文件(resources.mybatis.xml)
4.添加 mapper 文件 (resources.StudentMapper.xml)
5.mybatis(resources.mybatis.xml) 配置⽂件中添加 mapper
6.获得 SqlSession,通过该对象进⾏数据的操作 (com.yuyu.test.Test.java)
1.添加驱动包 mysql.jar 和 mybatis.jar
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
2.添加配置文件(resources.mybatis.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>
<!--连接数据库的环境,default="环境的id"-->
<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://localhost:3306/guangdong"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 指定maper文件的路径(maven项目从resources源文件夹下找资源)-->
<mappers>
<!--<mapper resource="包名/mapper⽂件名"/>-->
<mapper resource="StudentMapper.xml"/>
</mappers>
</configuration>
3.创建实体类和接口
实体类 bean (com.yuyu.bean.Student.java)
public class Student{
//属性名=列名
private int studentId;
private String studentNo;
private String stuName;
public int getStudentId() {return studentId;}
public void setStudentId(int studentId) {this.studentId = studentId;}
public String getStudentNo() {return studentNo;}
public void setStudentNo(String studentNo) {this.studentNo = studentNo;}
public String getStuName() {return stuName;}
public void setStuName(String stuName) {this.stuName = stuName;}
@Override
public String toString() {
return "Student{" +
"studentId=" + studentId +
", studentNo='" + studentNo + '\'' +
", stuName='" + stuName + '\'' +
'}';
}
}
接口 dao (com.yuyu.dao.StudentDao.java[interface])
public interface StudentDao {
//增删改查的方法
public List<Student> getall();
}
4.添加 mapper 文件 (resources.StudentMapper.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">
<!--namespace="接口的完整路径"-->
<mapper namespace="com.yuyu.dao.StudentDao">
<!--id="方法名"-->
<select id="getall" resultType="com.yuyu.bean.Student">
select * from student
</select>
</mapper>
5.mybatis(resources.mybatis.xml) 配置⽂件中添加 mapper
目的是让框架读取该配置⽂件知道 StudentMapper.xml ⽂件的存在
(mybatis.xml) 中 <environment> 标签后面
<!-- 指定maper文件的路径(maven项目从resources源文件夹下找资源)-->
<mappers>
<!--<mapper resource="包名/mapper⽂件名"/>-->
<mapper resource="StudentMapper.xml"/>
</mappers>
6.获得 SqlSession,通过该对象进⾏数据的操作 (com.yuyu.test.Test.java)
public class Test {
public static void main(String[] args) {
try {
//1.加载配置文件
Reader reader = Resources.getResourceAsReader("mybatis.xml");
//2.得到 sqlSessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = builder.build(reader);
//3.得到SqlSession
SqlSession session = build.openSession();
//4.操作sql
//方法参数是被调取的 sql 的完整路径:namespace + id
// (StudentMapper.xml 中的 <mapper namespace="com.yhp.dao.StudentDao">
// + <select id="getall" resultType="com.yhp.bean.Student">)
List<Student> list = session.selectList("com.yuyu.dao.StudentDao.getall");
//5.遍历
for (Student student : list) {
System.out.println(student);
}
//6.关闭资源
session.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
7.MyBatis 执行过程
①读取 .xml 配置文件 Reader reader = Resources.getResourceAsReader("mybatis.xml");
mybatis.xml 中指定了数据库的连接信息 和相应的数据库操作文件 StudentMapper.xml
② StudentMapper.xml 中映射 sql语句 和方法 getall() 的对应关系
即通过:mapper namespace="com.yhp.dao.StudentDao" 对应 select id="getall"
(namespace + id 找到唯一的sql 语句):select * from student
③通过配置文件的输入流得到一个工厂对象 SqlSessionFactory build = builder.build(reader);
④通过工厂对象打开一个 session: SqlSession session = build.openSession();
⑤通过 session 对象操作数据库 :
List<Student> list = session.selectList("com.yuyu.dao.StudentDao.getall");
⑥关闭资源