update docs

This commit is contained in:
开源海哥 2023-04-21 16:29:57 +08:00
parent 3af6c82c67
commit fa47c7cca3
4 changed files with 68 additions and 1 deletions

View File

@ -35,6 +35,7 @@ export default defineConfig({
{text: '和同类框架「性能」对比', link: '/zh/benchmark'},
{text: '和 Spring 整合 常见问题', link: '/zh/mybatis-flex-with-spring'},
{text: '支持的数据库类型', link: '/zh/support-database'},
{text: '使用 Mybatis 原生功能', link: '/zh/use-mybatis-native'},
{text: 'QQ 交流群', link: '/zh/qq-group'},
]
},

View File

@ -0,0 +1,56 @@
# 使用 Mybatis 原生功能
我们使用 Mybatis-Flex 作为 Mybatis 的增强框架进行代码开发,并不会影响原有的 Mybatis 的任何功能。
## 使用 `@Select` 等 Mybatis 原生注解
Mybatis 提供了 `@Insert``@Delete``@Update``@Select` 4 个注解,用于对 Mapper 的方法进行配置,用于原生编写原生 SQL 进行增删改查,
在 Mybatis-Flex 我们一样可以使用这些注解。例如:
```java
public interface MyAccountMapper extends BaseMapper<Account> {
@Select("select * from tb_account where id = #{id}")
Account selectById(@Param("id") Object id);
}
```
`@Insert``@Delete``@Update` 等注解也是一样的,也就是说,原有的 Mybatis 功能如何使用,在 Mybatis-Flex 就如何使用。
`@InsertProvider``@DeleteProvider``@UpdateProvider``@SelectProvider` 等还是和原生 Mybatis 一样的用法。
## 使用 xml 的方式
在开始使用 xml 之前,我们需要添加如下配置,告知 mybatis 的 xml 存放路径。
```yaml
mybatis-flex:
mapper-locations:
- classpath*:/mapper/*.xml
```
配置完成后,我们就可以编写自己的 xml 和 mapper 代码了,如下所示:
mapper:
```java
public interface MyAccountMapper extends BaseMapper<Account> {
Account selectByName(@Param("name") String name);
}
```
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.test.mapper.MyAccountMapper">
<!-- selectByName -->
<select id="selectByName" resultType="com.test.model.Account">
select * from `tb_account` where `user_name` = #{name}
</select>
</mapper>
```

View File

@ -52,12 +52,18 @@ public class AccountController {
@GetMapping("/account/select/{name}")
@GetMapping("/account/byName/{name}")
Account selectName(@PathVariable("name") String name){
return myAccountMapper.selectByName(name);
}
@GetMapping("/account/byId/{id}")
Account selectId(@PathVariable("id") Object id){
return myAccountMapper.selectById(id);
}
@GetMapping("/account/{id}")
@Transactional
public Account selectOne(@PathVariable("id") Long id) {

View File

@ -2,9 +2,13 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.test.model.Account;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface MyAccountMapper extends AccountMapper {
Account selectByName(@Param("name") String name);
@Select("select * from tb_account where id = #{id}")
Account selectById(@Param("id") Object id);
}