Mini框架使用演示

Mini框架使用演示,第1张

代码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,因为我觉得学会一个东西,不仅局限于如何使用它,更应该扩展到如何制造它,整个过程就像是把电视机拆开之后,自己还能否重新拼凑出一个电视,这是一个有趣的过程。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存