好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

初次体验MyBatis的注意事项

一、MyBatis的HelloWord

1.根据xml配置文件(全局配置文件mybatis-config.xml)创建一个SqlSessionFactory对象 有数据源一些运行环境信息

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?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= "123456" />

          </dataSource>

       </environment>

    </environments>

    <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 -->

    <mappers>

       <mapper resource= "EmployeeMapper.xml" />

    </mappers>

</configuration>

2.sql映射文件EmployeeMapper.xml;配置了每一个sql,以及sql的封装规则等。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?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.atguigu.mybatis.dao.EmployeeMapper" >

<!--

namespace:名称空间;指定为接口的全类名

id:唯一标识

resultType:返回值类型

#{id}:从传递过来的参数中取出id值

 

public Employee getEmpById(Integer id);

分离实现与接口

  -->

    <select id= "getEmpById" resultType= "com.atguigu.mybatis.bean.Employee" >

       select id,last_name lastName,email,gender from tbl_employee where id = #{id}

    </select>

</mapper>

3.将sql映射文件注册在全局配置文件mybatis-config.xml中

?

1

2

3

<mappers>

    <mapper resource= "EmployeeMapper.xml" />

</mappers>

4.写代码:

1).根据全局配置文件得到SqlSessionFactory;

?

1

2

3

String resource = "mybatis-config.xml" ;

InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

2).使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查,一个sqlSession就是代表和数据库的一次会话,用完关闭

?

1

SqlSession openSession = sqlSessionFactory.openSession();

3).使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的

?

1

2

3

4

5

6

7

try {

    Employee employee = openSession.selectOne(

          "com.atguigu.mybatis.dao.EmployeeMapper.getEmpById" , 1 ); // spacename + sqlId

    System.out.println(employee);

} finally {

    openSession.close();

}

二、MyBatis接口式编程

?

1

mybatis:    Mapper.java(接口) ====>  xxMapper.xml(实现)

接口式编程的好处在于,能够将功能与实现相分离

1、SqlSession代表和数据库的一次会话;用完必须关闭;
2、SqlSession和connection一样它都是非线程安全。每次使用都应该去获取新的对象。
3、mapper.java接口没有实现类,但是mybatis会为这个接口生成一个代理对象。(将接口和xml进行绑定)
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
4、两个重要的配置文件:

mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等…系统运行环境信息 sql映射文件:保存了每一个sql语句的映射信息:将sql抽取出来。

到此这篇关于初次体验MyBatis的注意事项的文章就介绍到这了,更多相关MyBatis的用法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/Wang_Pro/article/details/118110119

查看更多关于初次体验MyBatis的注意事项的详细内容...

  阅读:12次