简单版
- 创建一张测试表
- 创建对应的JavaBean
- 创建mybatis配置文件,sql映射文件
- 测试
MyBatis操作数据库
- 创建MyBatis全局配置文件
MyBatis的全局配置文件包含了影响MyBatis行为甚深的设置(settings)和属性(properties)信息、如数据库连接池信息等。指导着MyBatis进行工作。我们可以参照官方文件的配置示例。
- 创建SQL映射文件
映射文件的作用就是定义Dao接口的实现类如何工作。这是我们使用MyBatis时编写的最多的文件。
- 根据全局配置文件,利用
SqlSessionFactoryBuilder
创建SqlSessionFactory
。
String resource="mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
- 使用
SqlSessionFactory
获取SqlSession
对象。一个SqlSession
对象代表和数据库的一次会话。
SqlSession sqlSession = sf.openSession();
使用SqlSession:根据Id查询
SqlSession sqlSession = sf.openSession();
try {
Employee employee = sqlSession.selectOne("helloworld.EmployeeMapper.getEmployeeById", 1);
System.out.println(employee);
} finally {
sqlSession.close();
}
完整代码
- 加入相关的maven依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.19</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
</dependencies>
- 编写数据库脚本
create table t_employee(
id int auto_increment,
last_name varchar(255),
email varchar(255),
gender varchar(255),
primary key(id)
);
insert into t_employee (last_name,email,gender)values('tom','tom@qq.com','male');
select * from t_employee;
- 编写实体类
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
// getter and setter
// toString
}
- 编写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="helloworld.EmployeeMapper">
<!--
namespace:名称空间。为接口的全类名。
id:唯一标识。
resultType:返回值类型。
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
-->
<select id="getEmployeeById" resultType="helloworld.Employee">
select
id,
last_name lastName,
email,gender
from t_employee
where id = #{id}
</select>
</mapper>
- 编写MyBatis的全局配置文件 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="root"/>
</dataSource>
</environment>
</environments>
<!-- 将写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<mapper resource="helloworld/EmployeeMapper.xml"/>
</mappers>
</configuration>
- 编写主程序
public class Test {
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sf.openSession();
try {
Employee employee = sqlSession.selectOne("helloworld.EmployeeMapper.getEmployeeById", 1);
System.out.println(employee);
} finally {
sqlSession.close();
}
}
}
接口式编程
- 创建一个Dao接口
public interface EmployeeMapper {
Employee getEmployeeById(Integer id);
}
- 测试
SqlSession sqlSession = sf.openSession();
try {
// 使用SqlSession获取映射器进行操作
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmployeeById(1);
System.out.println(employee);
} finally {
sqlSession.close();
}
SqlSession
- SqlSession的实例是非线程安全的,因此是不能被共享的。
- SqlSession每次使用完成后需要正确关闭,这个关闭操作是必须的。
- SqlSession可以直接调用方法的id进行数据库操作。
但推荐使用SqlSession获取到Dao接口的代理类。执行代理对象的方法,可以更安全的进行类型检查操作。
来源:oschina
链接:https://my.oschina.net/mondayer/blog/4314116