1.导入jar包
根据全局配置文件,创建一个SQLSessionFactory对象
String resource="mybatis-conf.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
/获取SQLSession实例
//能直接执行已经映射的sql语句
SqlSession sqlSession=sqlSessionFactory.openSession();
//执行封装在Mapper中的映射语句
Employee emp=sqlSession.selectOne("com.hjk.EmployeeMapper.selectEmp", 1);
//回收资源
sqlSession.close();
mybatis.jar 和 相对应的数据库驱动jar包
2.config配置文件
mybatis-config.xml:
配置链接参数和开发环境,注册映射sql的mapper.xml文件
mapper.xml:
mapper标签属性:
<!-- namespace:名称空间 -->
select标签属性:
<!-- id:标签的唯一标识 -->
<!-- resultType:返回值类型 -->
<!-- #{id}:从传来的参数中取出id值 -->
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="kangkang94bang" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 将写好的sql注册到全局的配置文件中 -->
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
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.hjk.EmployeeMapper">
<!-- namespace:名称空间 -->
<!-- id:标签的唯一标识 -->
<!-- resultType:返回值类型 -->
<!-- #{id}:从传来的参数中取出id值 -->
<select id="selectEmp" resultType="com.hjk.bean.Employee">
select * from tbl_employee where id=#{id}
</select>
</mapper>
3.操作方法:
根据全局配置文件,创建一个SQLSessionFactory对象
String resource="mybatis-conf.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
/获取SQLSession实例
//能直接执行已经映射的sql语句
SqlSession sqlSession=sqlSessionFactory.openSession();
//执行封装在Mapper中的映射语句
Employee emp=sqlSession.selectOne("com.hjk.EmployeeMapper.selectEmp", 1);
//回收资源
sqlSession.close();
Test.java
package com.hjk.test;
import java.io.IOException;
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 com.hjk.bean.Employee;
public class MyBatisTest {
/*
* 根据全局配置文件,创建一个sql session factory 对象
*/
public static void main(String[] args) throws IOException {
String resource="mybatis-conf.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//获取sql session实例
//能直接执行已经映射的sql语句
SqlSession sqlSession=sqlSessionFactory.openSession();
/*
* 参数一:sql语句的唯一标识符:namespace+id
* 参数二:执行sql使用的参数
*/
Employee emp=sqlSession.selectOne("com.hjk.EmployeeMapper.selectEmp", 1);
System.out.println(emp);
sqlSession.close();
}
}
注意:
配置文件的格式和实体类的名字千万不用弄错,很麻烦的,注意引用路径一般采用的是全名,不然会报错找不到文件。实体类需要自己编写,成员变量需要和数据库中的字段名相匹配对应,不然无法获得操作结果。或者,在Mapper配置文件中,sql语句采用起别名的方式进行校正匹配字段。