一. 知识储备
前往免费下载源码
1.1 使用MyBatis查询数据库步骤
- 搭建环境:创建Java Project,导入jar包(mybatis jar包、mysql-connector-java jar包、log4j jar包)
- 准备测试表、javaBean
- 创建全局的配置文件:mybatis-config.xml
- 获取SqlSessionFactory对象
- 创建SQL映射文件:xxxMapper.xml(一般情况你想要操作的类名是什么,xxx就写什么,比如我想要操作Employee,映射文件就写EmployeeMapper.xml)
- 测试查询
二. 例子
- 首先创建好名为Mybatis的Java Project,new好名为conf的source folder,new好名为lib的folder。导入jar包,项目结构图如下:
- 创建名为bookstore_0416数据库,创建名为tbl_employee的表,如下:
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(50),
email VARCHAR(50),
gender CHAR(1)
);
INSERT INTO tbl_employee VALUES (1001, '刚哥', 'gg@sina.com', '1');
SELECT * FROM tbl_employee;
结果如下:
- 创建名为Employee的JavaBean,如下:
package com.atguigu.mybatis.beans;
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
}
}
- 创建全局配置文件:位于conf源文件夹下,名为mybatis-config.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="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/bookstore_0416" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 引入SQL映射文件 :即xxxMapper.xml文件
resource: 资源所在的位置,因为EmployeeMapper.xml在类路径下(源文件夹conf下),
所以只写EmployeeMapper.xml即可
-->
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
- 获取SqlSessionFactory对象,如下:
TestMyBatis.java
package com.atguigu.mybatis.test;
import java.io.InputStream;
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 org.junit.Test;
import com.atguigu.mybatis.beans.Employee;
public class TestMyBatis {
@Test
public void testHelloWorld() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream =
Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
/**
* Parameters:
* statement Unique identifier matching the statement to use.即SQL语句的唯一标识
* parameter A parameter object to pass to the statement.即执行sql语句的参数
* Returns:
* Mapped object
*/
Employee employee =
session.selectOne("suibian.selectBlog", 1001);
System.out.println("employee: " + employee);
} finally {
session.close();
}
}
}
- 创建SQL映射文件,如下:
EmployeeMapper.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">
<!-- SQL映射文件/apper映射文件
namespace: 命名空间
1. 随便写。不使用mapper接口开发,随便写
2. 不随便写。 使用mapper接口开发,不能随便写
-->
<mapper namespace="suibian">
<!--
<select>: 定义查询的SQL语句
id: 唯一标识,namespace+id
resultType: 结果集的封装类型
#{}: 获取参数值
-->
<select id="selectBlog" resultType="com.atguigu.mybatis.beans.Employee">
select id, last_name lastName, email, gender from tbl_employee where id = #{id}
</select>
</mapper>
- 测试结果: