好得很程序员自学网

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

Mybatis映射文件规则实例详解

在说明映射文件规则之前,先来回顾一下ORM相关概念。

1.ORM概念

ORM(Object Relationship Mapping)对象关系映射

对象:Java的实体类对象

关系:关系型数据库

映射:二者之间的对应关系

字段名和属性名要一一对应才可以,它们的名字要相同,底层调用的是反射机制

Java概念 数据库概念
属性 列,字段
对象 记录

2.映射文件命名规则

表对应的实体类的类名+Mapper.xml

举例:假如数据库的表的名字是t_user,它对应的实体类是User,那么对应的映射文件为UserMapper.xml

一个映射文件对应一个实体类,对应一张表的操作,调用Mapper中的方法就是来执行SQL

Mybatis映射文件用来写SQL语句,访问和操作表的数据

Mybatis映射文件存放位置是src/main/resources/mappers目录下面

3.Mybatis的两个一致

Mybatis可以面向接口操作数据,如果我们以包为单位引入映射文件,需要有两个一致

① 映射文件的namespace要和mapper接口的全类名一致

当调用Mapper接口中的方法,它会先根据Mapper接口的全类名去找到映射文件,然后根据方法名去找到对应的SQL语句

②映射文件中SQL语句的id要和mapper接口中的方法名一致

4.总结创建mybatis的步骤

创建maven工程 在pom.xml中引入相关依赖,比如数据库驱动,mybatis,junit单元测试,log4j日志 在src/main/java建包 3.1 在pojo包下面创建对应的实体类
注:实体类对应数据库表的记录
也就是说数据库查询出来的结果要以什么方式返回 3.2 在mapper包下面创建mapper接口,里面定义操作数据库中表的相关方法 在resources目录下面建mybatis的核心配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

<? 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代表核心配置文件-->

< configuration >

<!--    The content of element type "configuration" must match "

(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,

objectWrapperFactory?,reflectorFactory?,plugins?,environments?,

   databaseIdProvider?,mappers?)".-->

<!--    <typeAliases></typeAliases>-->

<!--    因为后面要查询的语句很多,每一个查询语句要对应一个实体类,那么我们在指定返回类型的时候-->

<!--    要写类的全路径,路径可能很长,这样就很麻烦,我们就可以使用标签-->

    < properties resource = "jdbc.properties" />

<!--    设置类型别名-->

    < typeAliases >

<!--        typeAlias:设置某个类型的别名

            属性:

                type:设置需要设置别名的类型

                alias:设置某个类型的别名,如果不设置该属性,则这个类型有默认的别名,而且类名不区分大小写

-->

        < typeAlias type = "com.atguigu.mybatis.mybatis.pojo.User" alias = "User" ></ typeAlias >

<!--       以包为单位,把包下面的所有类型设置成为默认的类型别名,——类名不区分大小写-->

        < package name = "com.atguigu.mybatis.pojo" />

    </ typeAliases >

    <!--    环境可以有多个,我们用默认的环境-->

    <!--    enviments:配置连接数据库的环境

               id:表示连接数据库环境的唯一标识,不能重复-->

    < environments default = "development" >

        < environment id = "development" >

            <!--            transactionManager设置事务管理方式

                         type=DBC|MANAGED

                         JDBC:表示在当前环境中,执行SQL时,使用的是JDBC中原生的事务管理方式,事务的提交或回滚需要手动处理

                         MANAGED:表示被谁管理,例如Spring-->

            < transactionManager type = "JDBC" />

<!--            type用来设置数据源的类型

type=POOLED|UNPOOLED|JNDI

POOLED:表示使用的是数据库连接池缓存数据库连接

UNPOOLED:表示不使用数据库连接池

JNDI:表示使用的是上下文中的数据源-->

            < dataSource type = "POOLED" >

                < property name = "driver" value = "${jdbc.driver}" />

                < property name = "url" value = "${jdbc.url}" />

                < property name = "username" value = "${jdbc.username}" />

                < property name = "password" value = "${jdbc.password}" />

                <!--                数据源就是连接是连接数据库的信息-->

            </ dataSource >

        </ environment >

        < environment id = "test" >

            < transactionManager type = "JDBC" />

            <!--            type=POOLED表示使用数据库连接池-->

            < dataSource type = "POOLED" >

                < property name = "driver" value = "${driver}" />

                < property name = "url" value = "${url}" />

                < property name = "username" value = "${username}" />

                < property name = "password" value = "${password}" />

            </ dataSource >

        </ environment >

    </ environments >

    <!--    引入映射文件-->

    <!--    每一个Mapper.xml都需要在Mybatis的核心配置文件中注册-->

<!--    mapper文件的位置是为了找到要执行的sql语句

resources属性指定的是mapper文件的路径

这个路径是从target/classes路径开始的

用/作为分隔符-->

    < mappers >

<!--        <mapper resource="mappers/UserMapper.xml"/>-->

<!--        这个时候,这个包里面的所有配置文件都会被引入-->

<!--        以包为单位引入映射文件

             要求:

             1.mapper接口所在的包要和映射文件所在的包一致

             2.mapper接口要和映射文件的名字一致

                   -->

        < package name = "com.atguigu.mybatis.mapper" />

    </ mappers >

</ configuration >

4.在resources目录下面建立mapper映射文件

5.测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

    //加载核心配置文件

         InputStream is = Resources.getResourceAsStream( "mybatis-config.xml" );

         //获取SqlSessionFactoryBuilder

         SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

         //获取SqlSessionFactory 工厂模式

         SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);

         //获取mybatis操作的会话对象

         //sqlSession默认是不自动提交事务的,如果我们写上参数true,就代表自动提交

         SqlSession sqlSession = sqlSessionFactory.openSession( true );

         //获取mapper接口的对象

         UserMapper mapper = sqlSession.getMapper(UserMapper. class );

         int i = mapper.insertUser();

         //提交事务

//        sqlSession测试数据mit();

         System.out.println( "结果:" +i);

补充:MyBatis_自定义结果映射规则

自定义resultMap,实现高级结果集映射

在EmployeeMapperPlus.xml中有:

?

1

2

3

4

5

6

package com.atguigu.mybatis.dao;

import java.util.List;

import com.atguigu.mybatis.bean.Employee;

public interface EmployeeMapperPlus {

    public Employee getEmpById(Integer id);

}

在EmployeeMapperPlus.xml中实现方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

    <!--自定义某个javaBean的封装规则

    type:自定义规则的Java类型

    id:唯一id方便引用

      -->

    < resultMap type = "com.atguigu.mybatis.bean.Employee" id = "MySimpleEmp" >

        <!--指定主键列的封装规则

        id用来定义主键,会底层有优化;

        column:指定哪一列

        property:指定对应的javaBean属性

          -->

        < id column = "id" property = "id" />

        <!-- result定义普通列封装规则 -->

        < result column = "last_name" property = "lastName" />

        <!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->

        < result column = "email" property = "email" />

        < result column = "gender" property = "gender" />

    </ resultMap >

 

    <!-- resultMap:自定义结果集映射规则;  -->

    <!-- resultMap与resultType只能二选一  -->

    <!-- public Employee getEmpById(Integer id); -->

    < select id = "getEmpById"   resultMap = "MySimpleEmp" >

        select * from tbl_employee where id=#{id}

    </ select >

测试:

先把驼峰命名法关了<setting name="mapUnderscoreToCamelCase" value="false"/>,或者注释掉也可以

?

1

2

3

4

5

6

7

8

9

10

11

12

@Test

     public void test05() throws IOException{

         SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

         SqlSession openSession = sqlSessionFactory.openSession();

         try {

             EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus. class );

             Employee empById = mapper.getEmpById( 1 );

             System.out.println(empById);

         } finally {

             openSession.close();

         }

     }

即使把驼峰命名关了还是可以按照我们自定义的规则封装成功的

总结

到此这篇关于Mybatis映射文件规则的文章就介绍到这了,更多相关Mybatis映射文件规则内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_52797170/article/details/123829957

查看更多关于Mybatis映射文件规则实例详解的详细内容...

  阅读:22次