Mybatis的Hello Word
2020.02.04 10:56
2020.02.04 10:56
1. 第一个Mybatis测试
1.1. 新建Mybatis配置文件
src/main/resources/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_test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
</configuration>
以上是最基本的配置,以后还会添加其他的东西。
1.2. 新建Mybatis的mapper文件
直白一点,该xml文件就是写SQL语句的。
src/main/resources/mapper/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="com.misiai.mapper">
<!--namespace:命名空间
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中获取一个值,可理解为以前的占位符。
-->
<select id="selectEmployee" resultType="com.misiai.bean.Employee">
select * from employee where id=#{id};
</select>
</mapper>
注意:我们在mybatis-config.xml配置文件中,已经关联了此xml文件,在哪里?
<mappers>
<mapper resource="mapper/EmployeeMapper.xml"/>
</mappers>
1.3. 新建测试类
com.misiai.test.Test01.java
package com.misiai.test;
import com.misiai.bean.Employee;
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 java.io.IOException;
import java.io.InputStream;
public class Test01 {
@Test
public void test01() throws IOException {
String resource = "mybatis-config.xml";
//由于该mybatis-config.xml文件,在类路径(源码目录)的根下,所以我们这里就没写更多路径,
// 如果你不是类路径下,就需要补全路径。
InputStream inputStream = Resources.getResourceAsStream(resource);
//1.根据xml文件,新建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//2.获取SqlSession,直接执行已经映射的SQL语句。
SqlSession sqlSession = sqlSessionFactory.openSession();
Employee employee = (Employee) sqlSession.selectOne("com.misiai.mapper.selectEmployee", 1);
System.out.println(employee);
sqlSession.close();
}
}
解释:
- 首页根据xml文件(mybatis-config.xml),新建sqlSessionFactory
- 获取SqlSession,直接执行已经映射的SQL语句。
- 那么SQL语句哪里来的?employeeMapper.xml文件里面写的
- 那么我们怎么找到employeeMapper.xml文件,因为我们只传入了mybatis-config.xml文件呀?通过mybatis-config.xml的配置呀!
- sqlSession.selectOne("com.misiai.mapper.selectEmployee", 1);的第一个参数是什么意思?
- 命名空间+id
- 即employeeMapper.xml文件中的namespace和对应的SQL语句的id
结果:
2. 接口式编程
上面的代码我们是调用的SqlSession.selectOne
方法,传入mapper.xml文件里面的命名空间+ID,然后执行对应的SQL语句。
不是说这样不好,但是这样传递参数的时候很复杂,而且不能很好的规范参数的类型。
那么我们直接通过接口使用,不是更好么?
2.1. 新建接口类
com.misiai.dao.EmployeeMapper
package com.misiai.dao;
import com.misiai.bean.Employee;
public interface EmployeeMapper {
public Employee findById(Integer id);
}
与此对应的mapper映射文件也要修改:
<?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.misiai.dao.EmployeeMapper">
<!--namespace:命名空间
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中获取一个值,可理解为以前的占位符。
-->
<select id="findById" resultType="com.misiai.bean.Employee">
select * from employee where id=#{id};
</select>
</mapper>
修改的地方有两处:
- namespace:必须是接口类的全类名限定符,这样mybatis才能找到。
- id:必须是接口类的对应的方法名。
2.2. 测试类
com.misiai.test.Test01
package com.misiai.test;
import com.misiai.bean.Employee;
import com.misiai.dao.EmployeeMapper;
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 java.io.IOException;
import java.io.InputStream;
public class Test01 {
/**
* 封装了一个获取SqlSession的公共函数
*
* @return
* @throws IOException
*/
public SqlSession getSqlSession() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession();
}
/**
* 测试接口式编程
*/
@Test
public void test02() throws IOException {
//此处是获取SqlSession,和之前的步骤一样,我们只是把它封装成一个公共函数
SqlSession sqlSession = getSqlSession();
//这一步就不一样了,之前我们是调用的sqlSession.selectOne()等等方法,然后传入的是namespace+id和参数,
// 但是这一步我们是调用的getMapper方法,传入的是EmployeeMapper.class
//这样mybatis就会自动为我们创建代理类,然后我们就可以直接使用接口的方法了。
// 注意:我们这里并没有接口类的具体实现类,因为mybatis创建的代理类相当于就是实现类了。
//而要mybatis通过接口文件找到对应的mapper.xml配置文件,那么mapper.xml配置文件必须满足两个要求:
// - namespace:必须是接口类的全类名限定符,这样mybatis才能找到。
// - id:必须是接口类的对应的方法名。
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = employeeMapper.findById(1);
System.out.println("employee = " + employee);
sqlSession.close();
}
}
Tips:具体说明看注释!
本节阅读完毕!
(分享)