# simple-netty-mybatis
**Repository Path**: simple-netty/simple-netty-mybatis
## Basic Information
- **Project Name**: simple-netty-mybatis
- **Description**: simple-netty-core的mybatis插件
- **Primary Language**: Java
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-11-15
- **Last Updated**: 2025-09-09
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## Simple Socket Project
### simple netty mybatis
#### 一、基本使用
##### 1、包引入
因为 `jar` 包并未上传至 `maven` 的中央仓库,所以要想使用 `simple-netty-mybatis` ,只能通过拉取源代码,编译安装(`mnv install`)至本地 `maven` 仓库的方法来使用。
如果已经编译并且安装到本地 `maven` 仓库了,那么仅需:
```xml
org.simplesocket
simple-netty-core
版本
org.simplesocket
simple-netty-mybatis
版本
```
引入两个包即可,一个是 `simple-netty-core` 基本依赖,所有的 `simple-netty` 库都依赖于它。另一个是 `simple-netty-mybatis`依赖,它在 `simple-netty-core` 的基础上封装了 `mybatis` 的基本操作,自带了 `mybatis` 的依赖,所以我们不用再引入 `mybatis` 依赖
##### 2、简单使用
`simple-netty-mybatis` 是基于 `mybatis` 和 `simple-netty-core` 开发的,它是一个 `simple-netty-core` 插件,所以不能离开 `simple-netty-core` 单独使用。其作用是为 `simple-netty-core` 提供数据库访问能力。
使用时,我们必需先启动 `simple-netty-core` 服务器,再通过配置的方式将 `simple-netty-mybatis` 配置进 `simple-netty-core` 。
示例(基于`mysql`):
```java
public class TestServer {
public static void main(String[] args) {
SimpleNettyContext.init(SimpleNettyConfig.builder()
.configLoader(SimpleNettyMybatisConfig.builder()
.configObject(MybatisConfig.builder()
.url("jdbc:mysql://192.168.1.1:3306/test")
.username("root")
.password("123456")
.build())
.build())
.build()).run();
}
}
```
即可在 `127.0.0.1` 的 `8088` 端口上启动一个 `simple-netty` 服务器,并配置其使用 `192.168.1.1` 的 `3306` 端口上的 `mysql` 数据库。
#### 二、自定义配置
##### 1、配置加载
配置支持两种方式的加载,一种就是之前示例过的配置对象的方式,另一种是配置文件的方式,配置对象的方式已经演示过了,我们演示一下配置文件的方式。
首先得在 `resources` 资源目录中定义一个 `mybatis` 的 `xml` 格式配置文件,例如,我们配置一个 `resources/mybatis-config.xml` :
```xml
```
这个配置文件指定了数据源为 `Druid` (默认)数据源,并且指定了它的包路径,`simple-netty-mybatis 2.0.5` 版本目前还未支持使用其它数据源,所以数据源这里必需这么写。
配置文件还指定了连接的细节,账号密码等。最后是 `mapper文件` 的目录地址,它在项目中的位置是 `resources/mapper/TestMapper.xml` 。
编辑好配置文件后,我们需要在服务器初始化时指定配置文件的路径:
```java
SimpleNettyMybatisConfig.builder()
.configFile("mybatis-config.xml")
.build()
```
##### 2、修改驱动
`simple-netty-mybatis` 自带 `mysql` 的驱动,默认也是使用 `mysql` 作为数据库,我们也可以修改并且自定义。
首先排除 `mysql` 的依赖:
```xml
org.simplesocket
simple-netty-mybatis
版本
com.mysql
mysql-connector-j
```
再引入所需的依赖,在配置时指定驱动:
```java
MybatisConfig.builder()
.driver(com.mysql.cj.jdbc.Driver.class) // 替换为自指定的驱动
.build();
```
##### 3、自定义mapper接口包
当然,我们的 `mapper` 也能正常使用,如果在 `MybatisConfig` 中未显式定义 `mapper接口` 的包的话,默认是 `com.example.test.mapper` 包,当然,我们也能显式的定义它,例如:
```java
MybatisConfig.builder()
.url("jdbc:mysql://192.168.1.1:3306/test")
.username("root")
.password("123456")
.mapperInterfacePackage("mymapper") // 指定mappe接口的包
```
配置了 `mapperInterfacePackage` 后,会在 `com.example.test.mymapper` 包中寻找 `mapper接口`
##### 4、自定义mapper.xml文件目录
同样的,与mapper接口相关的`mapper.xml` 文件,默认目录是在 `resources/mapper` 目录下,如果你的 `mapper.xml` 文件的位置就在这里,那么不需要做任何改动,`simple-netty-mybatis` 默认就会加载 `resources/mapper` 目录下的 `mapper.xml` 文件,如果需要修改,那么可以:
```java
MybatisConfig.builder()
.url("jdbc:mysql://192.168.1.1:3306/test")
.username("root")
.password("123456")
.mapperInterfacePackage("mymapper")
.mapperXMLPackage("mapperxml") // 指定xml的目录
```
> 其他配置请参考类 `SimpleNettyMybatisConfig` 和 `MybatisConfig`
#### 三、mapper接口注入
在mapper的使用上,`simple-netty-mybatis` 与 `spring-boot` 非常像,你可以使用 `@InjectMapper` 注解来注入mapper接口,例如:
```java
@RouterEndpoint
public class TestRouter {
@InjectMapper
private TestMapper testMapper;
public String test(){
return testMapper.get();
}
}
```
> `***` `simple-netty-mybatis` 会自动寻找 `@RouterEndpoint` 注解类中被 `@InjectMapper` 注解的接口,并注入它的值。
所以必需得注意的是,`@InjectMapper` 注解只能在 `@RouterEndpoint` 注解的类中使用(`simple-netty-mybatis 2.0.5`版本)。后续更新会支持在不同的地方使用。