手写mybatis框架

框架代码简单梳理
注释 com.simple.ibatis.annotation
@Dao
标注在mapper类上。标志着该类是一个mapper类,在解析时会进行解析

@Dao
public interface App1 {
}
@Select

标注在mapper类中的方法上。标志着该方法是一个Select方法,并在Select方法内部写具体的sql语句。对于有参数注入的情况,参数使用{}进行代替

@Select(“SELECT name from sys_user where name = {user.name} and id = {id}”)
List test1(User user, int id);
@Param

标注在mapper类中的方法参数上。对参数名进行一次重命名。若不使用此注释,会默认按照参数名当做注入的元素

@Select(“SELECT name from sys_user where id = {userId}”)
List test2(@Param(“userId”) int id);
@Update

标注在mapper类中的方法上。标志着该方法是一个Update方法

@Update(“update sys_user set name = {user.name} where id = {user.id}”)
void update3(User user);
@Insert

标注在mapper类中的方法上。标注着该方法是一个Insert方法

@Insert(“insert into sys_user(id,name) values ({user.id},{user.name})”)
int insert4(@Param(“user”) User user);
@Delete

标注在mapper类中的方法上。标注着该方法是一个Delete方法

@Delete(“delete from sys_user where id = {user.id}”)
int delete5(@Param(“user”) User user);
数据库注册 com.simple.ibatis.driver
DriverRegister 提供数据库注册功能。未避免重复注册,内部使用了一个缓存

数据源 com.simple.ibatis.datasource
NormalDataSource 普通数据源,没有池化的功能,提供获取数据库连接的功能
PoolDataSource 池化数据源,存放着活跃连接列表和空闲连接列表。并对获取连接和释放连接做了一系列操作
PoolConnection 连接的包装类,除了存放真实连接外,还存放此连接被获取时间,用于判断连接是否超时

核心类 com.simple.ibatis.core
Config 全局核心类,存放数据源,mapper包地址,mapper类解析文件
MapperCore mapper类解析文件
SqlSource 具体的sql语句封装

代理类 com.simple.ibatis.mapping
MapperProxy mapper接口代理类。使用动态代理技术

执行器类 com.simple.ibatis.execute
Executor 执行器接口
SimpleExecutor 具体执行器,执行具体的sql方法。生成结果
ExecutorFactory 生成Executor的工厂类

反射类 com.simple.ibatis.reflect
ClassWrapper 类加强器,封装了Object的get和set方法。
ObjectWrapper 对象包装类。调用ObjectWrapper.setVal和getVal就可以设置和获得属性。不需要显示的调用对象的getxxx和setxxx方法。
ObjectWrapperFactory 对象包装类生成器

处理器类 com.simple.ibatis.statement
PreparedStatementHandle PreparedStatement生成器。将java属性转为jdbc属性并注入。
ResultSetHandle 对查询结构ResultSet进行解析,转换为Java类型

工具类 com.simple.ibatis.util
PackageUti 解析包的工具类
TypeUtil 类型判断的工具类

缓存类 com.simple.ibatis.cache
Cache 缓存接口类

SimpleCache 简单缓存类

具体代码实践

  1. 代码运行默认在java8上,因为用到了参数反射,所以在idea中记得开启parameters;

File->Settings->Build,Execution,Deployment->Compiler->Java Compiler
在 Additional command line parameters: 后面填上 -parameters

  1. 构建pojo文件(记得在数据库也建立一张sys_user表)

复制代码
public class User {
private int id;

private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
复制代码
2. 构建mapper文件

在mapper文件上加@Dao注解,并为每个方法编写Sql语句,如下所示。目前仅支持注解方式配置SQL,后续会继续完善使其支持.xml文件。

复制代码
@Dao
public interface App1 {

@Select(“SELECT * from sys_user”)
List select2();

@Select(“SELECT name from sys_user where name = {user.name} and id = {id}”)
List select3(User user, @Param(“id”) int id);

@Update(“update sys_user set name = {user.name} where id = {user.id}”)
void update4(User user);

@Insert("insert into sys_user(id,name) values ({user.id},{user.name}) ")
int insert5(@Param(“user”) User user);
}
复制代码
3. 编写测试类

复制代码
public class ExecutorTest {

@Test
public void shouldConnect(){
    /**构建数据源,使用时下面代码可设置为全局变量,只加载一次*/
    PoolDataSource poolDataSource = new PoolDataSource("com.mysql.jdbc.Driver","jdbc:mysql://101.132.150.75:3306/our-auth","root","root");
    Config config = new Config("com/simple/ibatis/mapper",poolDataSource);
  /**拿到具体的mapper代理类*/
    SimpleExecutor executor = config.getExecutor();
    App1 app1 = executor.getMapper(App1.class);

    /**构建查询条件*/
    User user = new User();
    user.setName("xiabing");
    user.setId(1);
    
    /**调用插入方法*/
    int count = app1.insert5(user);
    
    /**调用更新方法*/
    user.setName("root");
    app1.update4(user);
    
    /**查询用户名,返回字符*/
    List<String> users = app1. select3(user,3);
    System.out.println(users.get(0));
   
   /**查询用户,返回对象*/
   List<User> userLists = app1.select2();
   System.out.println(userLists.get(0).getName());

}

}
亚马逊测评 www.yisuping.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值