MyBatis_HelloWorld
MyBatis操作数据库
- 创建MyBatis全局配置文件
MyBatis 的全局配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息、如数据库连接池信息等,指导着MyBatis进行工作。我们可以参照官方文件的配置示例。
<?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="EmployeeMapper.xml" />
</mappers>
</configuration>
- 创建SQL映射文件
映射文件的作用就相当于是定义Dao接口的实现类如何工作,这也是我们使用MyBatis时编写的最多的文件。
<?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.ginger.myabtis.dao.EmployeeMapper">
<!--
namespace:名称空间,指定为接口的全类名。
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Employee getEmpById(Integer id);
MyBatis 映射文件传入的参数可以不用指定,有默认的处理器可以分析出来出入的参数,还是比较智能的。
-->
<select id="getEmpById" resultType="com.ginger.myabtis.bean.Employee">
select id,lastname lastName,email,gender from employee where id = #{id}
</select>
</mapper>
1.HelloWorld简单版
- 创建一张测试表
- 创建对应的javaBean
- 创建mybatis配置文件,sql映射文件
- 测试
测试
/**
* 1、接口式编程
* 原生: Dao ====> DaoImpl
* mybatis: Mapper ====> xxMapper.xml
* 2、SqlSession 代表和数据库的一次会话;用完必须关闭;
* 3、SqlSession 和 Connection 一样她都是非线程安全,每次使用都应该去获取新的对象。
* 4、mapper 接口没有实现类,但是 mybatis 会为这个接口生成一个代理对象。
* (将接口和xml进行绑定)
* EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
* 5、两个重要的配置文件:
* mybatis 的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息
* sql 映射文件:保存了每一个 sql 语句的映射信息将:sql 抽取出来。
*/
public class MyBatisTest{
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1、根据xml配置文件(全局配置文件)创建一个 SqlSessionFactory 对象 有数据源一些运行环境信息。
* 2、sql 映射文件;配置了每一个 sql,以及 sql 的封装规则等。
* 3、将 sql 映射文件注册在全局配置文件中。
* 4、写代码:
* 1)、根据全局配置文件得到 SqlSessionFactory。
* 2)、使用 sqlSession 工厂,获取到 sqlSession 对象使用他来执行增删改查一个 sqlSession 就是代表和数据库的一次会话,用完关闭。
* 3)、使用 sql 的唯一标志来告诉 MyBatis 执行哪个 sql,sql 都是保存在 sql 映射文件中的。
*/
@Test
public void test() throws IOException {
// 2、获取sqlSession实例,能直接执行已经映射的sql语句。
// sql的唯一标识:statement Unique identifier matching the statement to use。
// 执行sql要用的参数:parameter A parameter object to pass to the statement。
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 注意这里 selectOne 方法的第一个参数时指定要指定的 sql 标识。那样才能找到具体要执行的 sql 语句。
Employee employee = openSession.selectOne("com.ginger.myabtis.dao.EmployeeMapper.getEmpById", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}
}
测试结果
2.HelloWorld-接口式编程
- 创建一个Dao接口
- 修改Mapper文件
- 测试
测试
/**
* 1、接口式编程
* 原生: Dao ====> DaoImpl
* mybatis: Mapper ====> xxMapper.xml
* 2、SqlSession 代表和数据库的一次会话;用完必须关闭;
* 3、SqlSession 和 Connection 一样她都是非线程安全,每次使用都应该去获取新的对象。
* 4、mapper 接口没有实现类,但是 mybatis 会为这个接口生成一个代理对象。
* (将接口和xml进行绑定)
* EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
* 5、两个重要的配置文件:
* mybatis 的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息。
* sql 映射文件:保存了每一个 sql 语句的映射信息将:sql 抽取出来。
*/
public class MyBatisTest{
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test01() throws IOException {
// 1、获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// 2、获取sqlSession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3、获取接口的实现类对象
//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(mapper.getClass());
System.out.println(employee);
} finally {
openSession.close();
}
}
}
测试结果