一、MyBatis获取数据表的步骤:
- 配置xml全局配置文件mybatis-config.xml
存储数据库连接的4个基本信息和一些基本设置,以及 mapper注册信息 - 配置sql映射文件EmployeeMapper.xml
配置了每一个sql语句和sql的映射信息:唯一标识id,返回值类型resultType/resultMap - 将sql映射文件EmployeeMapper.xml注册到全局配置文件mybatis-config.xml中
- 主函数逻辑
(1)根据全局配置文件,创建一个SqlSessionFactory对象
(2)使用sqlSessionFactory.openSession()
获取SqlSession对象,能够直接执行已经注册(映射)的sql语句
一个sqlSeesion就代表和数据库的一次会话,用完要关闭
(3)使用sql的唯一标识id,告诉MyBatis要执行哪个sql
改进:接口式编程
- 一个Mapper接口就对应一个xxxMapper.xml配置文件
- Mapper接口中定义CRUD方法,但是不需要定义其实现类,因为MyBatis提供了一个接口和xml配置文件动态绑定的功能,会为接口生成一个Proxy代理对象
openSession.getMapper(Mapper接口.class)
具体代码
Employee对象(对应于数据表中的各个字段值)
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 + '\'' +
'}';
}
}
- 全局配置文件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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="自己设置的数据库密码"/>
</dataSource>
</environment>
</environments>
<!-- 将写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="mybatis_1_HelloWorld/conf/EmployeeMapper.xml"/>
</mappers>
</configuration>
- 定义接口EmployeeMapper.java
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
- 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">
<mapper namespace="mybatis_1_HelloWorld.com.Lemon.mybatis.dao.EmployeeMapper">
<!--
1. namespace:名称空间,TODO 指定为接口的全类名
——从而将接口文件和sql映射文件绑定
2. 接口中的getEmpById()方法的功能和select语句功能是相同的
所以可以将select的id改成接口方法名
——从而将接口方法和select标签绑定
-->
<select id="getEmpById" resultType="mybatis_1_HelloWorld.com.Lemon.mybatis.bean.Employee">
select id,last_name lastName,email,gender from tbl_employee where id = #{id}
</select>
</mapper>
- 主函数逻辑
// 根据xml配置文件(全局配置文件mybatis-config.xml),创建sqlSessionFactory对象
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis_1_HelloWorld/conf/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
@Test
public void test() throws IOException {
// 1. 获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// 2. 获取SqlSession实例
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3. 获取接口的实现类对象
// TODO 会为接口自动创建一个Proxy代理对象,通过代理对象去执行CRUD方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
// 从这里可以看出mapper是一个代理类
System.out.println(mapper.getClass());
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
} finally {
openSession.close();
}
}
注意!!!
(1)在全局配置文件中,使用<mapper>
标签将sql映射文件注册进来
(2)在sql映射文件中,namespace
名称空间要写对应的Mapper接口的全类名
(3)在sql映射文件中,使用<select>
标签定义sql语句,id
是对应Mapper接口的方法名,resultType
是方法(sql)的返回值类型
(4)select语句中使用#{ }
的方式,从外部传进参数到这里
(5)一个SqlSession就代表和数据库的一次会话,所以用完必须关闭
二、基于注解的sql注册
我们可以使用@Select
注解,标注在Mapper接口的方法上,从而可以省去xml映射文件,直接在全局配置文件中使用class
进行注册
具体代码
- 基于注解的Mapper接口
public interface EmployeeMapperAnnotation {
@Select("select * from tbl_employee where id = #{id} ")
public Employee getEmpById(Integer id);
}
- 全局配置文件
<mappers>
<mapper class="mybatis_2_config.com.Lemon.mybatis.dao.EmployeeMapperAnnotation"/>
</mappers>
三、 全局配置文件中的setting标签
mapUnderscoreToCamelCase
:是否开启驼峰命名自动映射,即从经典数据库列名 “last_name” 映射到经典 Java 属性名 “lastName”
例如:
数据表中字段是last_name,而在idea中定义的对象属性名是lastName
如果不开启的话,使用`select last_name from tbl_employee where id = #{id}`是查不到lastName信息的,必须为其取别名。
而如果开启的话,就不需要取别名,可以直接查询到该字段信息
全局配置文件
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
sql映射文件
<select id="getEmpById" resultType="mybatis_2_config.com.Lemon.mybatis.bean.Employee">
<!-- TODO 通过开启mapUnderscoreToCamelCase,可以自动识别 -->
select * from tbl_employee where id = #{id}
</select>