All Projects → hhyo → mybatis-mapper2sql

hhyo / mybatis-mapper2sql

Licence: Apache-2.0 License
Generate SQL Statements from the MyBatis3 Mapper XML file

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to mybatis-mapper2sql

lightbatis
Lightbatis 增强 MyBatis 版Java 数据库持久层,更简洁并易用
Stars: ✭ 52 (-14.75%)
Mutual labels:  mybatis, mybatis-generator
mybatis-generator-plus
轻度扩展mybatis-generator-core插件,与官方插件兼容。
Stars: ✭ 62 (+1.64%)
Mutual labels:  mybatis, mybatis-generator
mybatis-generator-gui-plus
基于MyBatis-Generator+SQLite+beautyeye_lnf开发的一款图形化代码生成器
Stars: ✭ 16 (-73.77%)
Mutual labels:  mybatis, mybatis-generator
Spring Boot Seed
SpringBoot骨架项目,集成SpringBoot、Mybatis、Druid、Mapper、PageHelper、Redis、Shiro、Swagger2、Log4j2等技术
Stars: ✭ 204 (+234.43%)
Mutual labels:  mapper, mybatis
kite-mybatis-builder
web 形式的mybatis 生成器
Stars: ✭ 58 (-4.92%)
Mutual labels:  mybatis, mybatis-generator
mall
SpringBoot + Layui 电子商城系统
Stars: ✭ 38 (-37.7%)
Mutual labels:  mybatis, mybatis-generator
AllInOneFX
All In One JavaFX Application with a curated list of awesome JavaFX libraries, frameworks
Stars: ✭ 26 (-57.38%)
Mutual labels:  mybatis, mybatis-generator
Spring Boot Api Project Seed
🌱🚀一个基于Spring Boot & MyBatis的种子项目,用于快速构建中小型API、RESTful API项目~
Stars: ✭ 8,979 (+14619.67%)
Mutual labels:  mapper, mybatis
mybatis-generator
MyBatis code generator
Stars: ✭ 25 (-59.02%)
Mutual labels:  mybatis, mybatis-generator
p mybatis
mybatis深入学习代码文档以及源码解析,包含通用mapper、分页等插件的详细介绍以及使用
Stars: ✭ 22 (-63.93%)
Mutual labels:  mapper, mybatis
Okhelper Service
OK帮 云进销存 (SpringBoot Restful 全家桶)
Stars: ✭ 146 (+139.34%)
Mutual labels:  mapper, mybatis
SpringBootMovie
基于Spring Boot的电影网站
Stars: ✭ 56 (-8.2%)
Mutual labels:  mybatis, mybatis-generator
Ourbatis
Enhancement tools that make the development of Mybatis easier.
Stars: ✭ 84 (+37.7%)
Mutual labels:  mapper, mybatis
IDEAPractice
Java练习 - Java基础知识,面试题,小demo,长期积累 | intellij idea + maven + tomcat
Stars: ✭ 45 (-26.23%)
Mutual labels:  mybatis, mybatis-generator
Genesis
Spring cloud Example
Stars: ✭ 83 (+36.07%)
Mutual labels:  mapper, mybatis
atguigu ssm crud
Atguigu-SSM-CRUD 一个最基本的CRUD系统,采用IDEA+Maven搭建,具备前后端交互功能,前端采用BootStrap+Ajax异步请求DOM渲染,后端采用SpringMVC+MyBatis+Mysql8.0+Servlet+Jsp,符合REST风格URL规范,并加入了Hibernate提供的数据校验功能,支持PageHelper的分页功能,很适合SSM阶段性练习。同时用到了很多前端操作以及BootStrap组件,也有利于学习JS和前端框架。
Stars: ✭ 52 (-14.75%)
Mutual labels:  mybatis, mybatis-generator
Ssm booksystem
ssm demo,ssm详细教程,SSM简明教程:简单的十步教你搭建人生第一个SSM框架[ SSM框架整合教程(spring+spring mvc+mybatis+redis+maven+idea+bootstrap) ]
Stars: ✭ 355 (+481.97%)
Mutual labels:  mapper, mybatis
Mapper
Mybatis Common Mapper - Easy to use
Stars: ✭ 6,680 (+10850.82%)
Mutual labels:  mapper, mybatis
gd-generator
A code generator that elegantly generates mybatis ORM (mapper and xml config) and intelligently retains user-defined mapper and xml, automatically creates/synchronizes database tables, and records and prints exactly after each domain model change Database repair sql, support for the production of a variety of complex VO (View Object) objects, al…
Stars: ✭ 82 (+34.43%)
Mutual labels:  mybatis, mybatis-generator
demo springboot with mybatis
No description or website provided.
Stars: ✭ 17 (-72.13%)
Mutual labels:  mybatis, mybatis-generator

mybatis-mapper2sql

Build Status codecov image image image

Generate SQL Statements from the MyBatis3 Mapper XML file
Just for SQL Review https://github.com/hhyo/archery/issues/3

Installation

pip install mybatis-mapper2sql

Usage

import mybatis_mapper2sql
# Parse Mybatis Mapper XML files
mapper, xml_raw_text = mybatis_mapper2sql.create_mapper(xml='mybatis_mapper.xml')
# Get All SQL Statements from Mapper
statement = mybatis_mapper2sql.get_statement(mapper)
# Get SQL Statement By SQLId
statement = mybatis_mapper2sql.get_child_statement(mapper, sql_id)

Examples

https://github.com/OldBlackJoe/mybatis-mapper

test.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="Test">
    <sql id="sometable">
        fruits
    </sql>
    <sql id="somewhere">
        WHERE
        category = #{category}
    </sql>
    <sql id="someinclude">
        FROM
        <include refid="${include_target}"/>
        <include refid="somewhere"/>
    </sql>
    <select id="testParameters">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        WHERE
        category = #{category}
        AND price > ${price}
    </select>
    <select id="testInclude">
        SELECT
        name,
        category,
        price
        <include refid="someinclude">
            <property name="prefix" value="Some"/>
            <property name="include_target" value="sometable"/>
        </include>
    </select>
    <select id="testIf">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        WHERE
        1=1
        <if test="category != null and category !=''">
            AND category = #{category}
        </if>
        <if test="price != null and price !=''">
            AND price = ${price}
            <if test="price >= 400">
                AND name = 'Fuji'
            </if>
        </if>
    </select>
    <select id="testTrim">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        <trim prefix="WHERE" prefixOverrides="AND|OR">
            OR category = 'apple'
            OR price = 200
        </trim>
    </select>
    <select id="testWhere">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        <where>
            AND category = 'apple'
            <if test="price != null and price !=''">
                AND price = ${price}
            </if>
        </where>
    </select>
    <update id="testSet">
        UPDATE
        fruits
        <set>
            <if test="category != null and category !=''">
                category = #{category},
            </if>
            <if test="price != null and price !=''">
                price = ${price},
            </if>
        </set>
        WHERE
        name = #{name}
    </update>
    <select id="testChoose">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        <where>
            <choose>
                <when test="name != null">
                    AND name = #{name}
                </when>
                <when test="category == 'banana'">
                    AND category = #{category}
                    <if test="price != null and price !=''">
                        AND price = ${price}
                    </if>
                </when>
                <otherwise>
                    AND category = 'apple'
                </otherwise>
            </choose>
        </where>
    </select>
    <select id="testForeach">
        SELECT
        name,
        category,
        price
        FROM
        fruits
        <where>
            category = 'apple' AND
            <foreach collection="apples" item="name" open="(" close=")" separator="OR">
                <if test="name == 'Jonathan' or name == 'Fuji'">
                    name = #{name}
                </if>
            </foreach>
        </where>
    </select>
    <insert id="testInsertMulti">
        INSERT INTO
        fruits
        (
        name,
        category,
        price
        )
        VALUES
        <foreach collection="fruits" item="fruit" separator=",">
            (
            #{fruit.name},
            #{fruit.category},
            ${fruit.price}
            )
        </foreach>
    </insert>
    <select id="testBind">
        <bind name="likeName" value="'%' + name + '%'"/>
        SELECT
        name,
        category,
        price
        FROM
        fruits
        WHERE
        name like #{likeName}
    </select>
</mapper>

test.py

Get All SQL Statements from Mapper

import mybatis_mapper2sql
mapper, xml_raw_text = mybatis_mapper2sql.create_mapper(xml='test.xml')
statement = mybatis_mapper2sql.get_statement(mapper, result_type='raw', reindent=True, strip_comments=True)
print(statement)
SELECT name,
       category,
       price
FROM fruits
WHERE category = ?
  AND price > ?;


SELECT name,
       category,
       price
FROM fruits
WHERE category = ?;


SELECT name,
       category,
       price
FROM fruits
WHERE 1=1
  AND category = ?
  AND price = ?
  AND name = 'Fuji';


SELECT name,
       category,
       price
FROM fruits
WHERE category = 'apple'
  OR price = 200;


SELECT name,
       category,
       price
FROM fruits
WHERE category = 'apple'
  AND price = ?;


UPDATE fruits
SET category = ?,
    price = ?
WHERE name = ?;


SELECT name,
       category,
       price
FROM fruits
WHERE name = ?
  AND category = ?
  AND price = ?
  AND category = 'apple';


SELECT name,
       category,
       price
FROM fruits
WHERE categy = 'apple'
  AND (name = ?
       OR name = ?);


INSERT INTO fruits (name, category, price)
VALUES (?,
        ?,
        ?) , (?,
              ?,
              ?);


SELECT name,
       category,
       price
FROM fruits
WHERE name like ?;

Get SQL Statement By SQLId

import mybatis_mapper2sql
mapper, xml_raw_text = mybatis_mapper2sql.create_mapper(xml='test.xml')
statement = mybatis_mapper2sql.get_child_statement(mapper,'testForeach', reindent=True, strip_comments=False)
print(statement)
SELECT name,
       category,
       price
FROM fruits
WHERE categy = 'apple'
  AND ( name = ? -- if(name == 'Jonathan' or name == 'Fuji')
OR name = ? -- if(name == 'Jonathan' or name == 'Fuji')
)

Running the tests

python setup.py test

Known Limitations

  • Doesn't support custom parameters
  • All sql parameters will be replace to '?'
  • All of the conditionals to apply in <if> <choose> <when> <otherwise> element

Acknowledgments

This project was inspired by the following projects and websites:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].