mybatis全局配置文件SqlMapConfig.xml的使用

SqlMapConfig.xml标签必须按照顺序配置,原因就是因为使用了dtd校验方式

properties(属性)

typeAliases(类型别名)

 

Mybatis默认支持的别名

别名

映射的类型

_byte

byte

_long

long

_short

short

_int

int

_integer

int

_double

double

_float

float

_boolean

boolean

string

String

byte

Byte

long

Long

short

Short

int

Integer

integer

Integer

double

Double

float

Float

boolean

Boolean

date

Date

decimal

BigDecimal

bigdecimal

BigDecimal

map

Map

 

自定义别名

 

mappers(映射器

mappers注册sql映射文件的

  1. resource属性加载sql映射文件,万能型选手(crud、原始dao、mapper动态代理)
  2. 针对Mapper动态代理进行一个增强(增强两种用法)
    1. 以上两种方式有规范要求
    2. package 批量扫描注册
    3. mapper class 单个注册

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis 是一款使用 XML 或注解配置的持久层框架,它可以自动化地将数据库的数据映射到 Java 对象。在 MyBatis ,mapper 与 mapper.xml 是配对使用的,其 mapper 是接口,而 mapper.xml映射配置文件。 mapper 接口定义了数据库操作的方法,而 mapper.xml 则定义了这些方法的 SQL 语句以及参数映射规则、结果集映射规则等。 下面是一个简单的例子: 1. 定义 mapper 接口 ```java public interface UserMapper { User selectUserById(Integer id); } ``` 2. 定义 mapper.xml 映射配置文件 ```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.example.UserMapper"> <resultMap id="userResultMap" type="com.example.User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="password" property="password"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 上述代码,namespace 属性指定了 mapper 接口的全限定名,resultMap 标签定义了一个结果集映射规则,select 标签定义了一个查询操作,其 id 属性指定了 mapper 接口的方法名,resultMap 属性指定了结果集映射规则的 id。 3. 在 MyBatis 配置文件引入 mapper.xml ```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> <mappers> <mapper resource="com/example/UserMapper.xml"/> </mappers> </configuration> ``` 上述代码,mapper 标签指定了映射配置文件的位置。 这样就完成了 mapper 与 mapper.xml配置。在代码调用 selectUserById 方法时,MyBatis 会根据 mapper.xml 配置自动生成 SQL 语句,并将查询结果映射到 User 对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_35670694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值