Mini框架使用演示

本文档展示了如何创建一个名为Mini的微型Mybatis框架。从导入依赖、配置文件设置、加载配置到编写SQL映射接口和执行查询,详细解析了Mini架构的每个步骤。Mini虽小,但包含了Mybatis的核心功能,是作者自我验证Mybatis理解程度的实践作品。
摘要由CSDN通过智能技术生成

代码Gitee路径:Mini

1. 导入pom

这里已将源码中的com包放入了maven本地仓库的根路径下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lwl</groupId>
    <artifactId>testMini</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.lwl</groupId>
            <artifactId>Mini</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
    </dependencies>
</project>

2. 导入配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration[

        <!ELEMENT configuration (properties?,environment?,settings?,mappers?)>

        <!ELEMENT properties (property*)>
        <!ATTLIST properties
                resource CDATA #IMPLIED
                >

        <!ELEMENT property EMPTY>
        <!ATTLIST property
                name CDATA #REQUIRED
                value CDATA #REQUIRED
                >
        <!ELEMENT environment (dataSource)>
        <!ELEMENT dataSource (property*)>

        <!ELEMENT settings (setting*)>
        <!ELEMENT setting EMPTY>
        <!ATTLIST setting
                name CDATA #REQUIRED
                value CDATA #REQUIRED
                >
        <!ELEMENT mappers (package*)>
        <!ELEMENT package EMPTY>
        <!ATTLIST package
                name CDATA #REQUIRED
                >
        ]>
<configuration>
    <properties resource="db.properties">
    </properties>

    <settings>
        <setting name="userDruid" value="false"/>
        <setting name="cacheEnable" value="true"/>
        <setting name="loggedEnable" value="true"/>
    </settings>
    <!--<environment>
        <dataSource>
            <property name="jdbc.url" value="jdbc:mysql://localhost:3306/mybatis01"/>
            <property name="jdbc.username" value="root"/>
            <property name="jdbc.password" value="root"/>
            <property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </dataSource>
    </environment>-->
    <mappers>
        <package name="com.lwl.testMapper"/>
    </mappers>
</configuration>

3. 加载配置文件

package com.lwl.test;

import com.lwl.mini.session.SqlSession;
import com.lwl.mini.session.SqlSessionFactory;
import com.lwl.mini.session.SqlSessionFactoryBuilder;
import com.lwl.testMapper.UserMapper;

/**
 * @author lwl
 * @create 2022/4/9 12:33
 */
public class test {
    public static void main(String[] args)  {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build("miniConfig.xml");
        SqlSession sqlSession = build.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

    }
}

4.编写sql语句以及参数规则

public interface UserMapper {

    @Select("select * from user where id = #{testId} and username = #{name}")
    User selectOne(@Param("testId") int id, @Param("name") String name);

}

5. 执行

package com.lwl.test;

import com.lwl.mini.session.SqlSession;
import com.lwl.mini.session.SqlSessionFactory;
import com.lwl.mini.session.SqlSessionFactoryBuilder;
import com.lwl.mini.test.User;
import com.lwl.testMapper.UserMapper;

/**
 * @author lwl
 * @create 2022/4/9 12:33
 */
public class test {
    public static void main(String[] args)  {
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build("miniConfig.xml");
        SqlSession sqlSession = build.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectOne(41, "小刘");
        System.out.println("user = " + user);
    }
}

6. 后言

Mini架构参考Mybatis,代码自己diy,可以看作是个小型Mybatis。目前初步阶段,仍有许多限制不足。博主创作它也是想验证自己是否真的学会了Mybatis,因为我觉得学会一个东西,不仅局限于如何使用它,更应该扩展到如何制造它,整个过程就像是把电视机拆开之后,自己还能否重新拼凑出一个电视,这是一个有趣的过程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值