mybatis-helper-spring

mybatis-helper-spring module

License

License

Categories

Categories

MyBatis Data ORM
GroupId

GroupId

me.gaigeshen.mybatis
ArtifactId

ArtifactId

mybatis-helper-spring
Last Version

Last Version

1.6.0
Release Date

Release Date

Type

Type

jar
Description

Description

mybatis-helper-spring
mybatis-helper-spring module
Project URL

Project URL

https://github.com/gaigeshen/mybatis-helper

Download mybatis-helper-spring

How to add to project

<!-- https://jarcasting.com/artifacts/me.gaigeshen.mybatis/mybatis-helper-spring/ -->
<dependency>
    <groupId>me.gaigeshen.mybatis</groupId>
    <artifactId>mybatis-helper-spring</artifactId>
    <version>1.6.0</version>
</dependency>
// https://jarcasting.com/artifacts/me.gaigeshen.mybatis/mybatis-helper-spring/
implementation 'me.gaigeshen.mybatis:mybatis-helper-spring:1.6.0'
// https://jarcasting.com/artifacts/me.gaigeshen.mybatis/mybatis-helper-spring/
implementation ("me.gaigeshen.mybatis:mybatis-helper-spring:1.6.0")
'me.gaigeshen.mybatis:mybatis-helper-spring:jar:1.6.0'
<dependency org="me.gaigeshen.mybatis" name="mybatis-helper-spring" rev="1.6.0">
  <artifact name="mybatis-helper-spring" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.gaigeshen.mybatis', module='mybatis-helper-spring', version='1.6.0')
)
libraryDependencies += "me.gaigeshen.mybatis" % "mybatis-helper-spring" % "1.6.0"
[me.gaigeshen.mybatis/mybatis-helper-spring "1.6.0"]

Dependencies

compile (2)

Group / Artifact Type Version
me.gaigeshen.mybatis : mybatis-helper jar 1.6.0
org.mybatis : mybatis-spring jar 2.0.1

provided (2)

Group / Artifact Type Version
org.springframework : spring-context jar 5.1.6.RELEASE
org.springframework : spring-jdbc jar 5.1.6.RELEASE

test (2)

Group / Artifact Type Version
org.hsqldb : hsqldb jar 2.4.1
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Mybatis-Helper

License Build Status Maven Central Sonatype Nexus (Snapshots) GitHub last commit

For Chinese version of this file, please click Readme_zh_CN.md

We created intelliJ idea plugin to helps you generate mybatis files, please click Readme_idea_plugin.md

Introduction

Helper tools for mybatis, this tools TESTed with mybatis version 3.5.1 and mybatis-spring version 2.0.1, after extends BaseEntity and Dao interface, you don't have to write mapper xml file if JUST use basic CRUD methods.

About entity

Base entity

All entities should extends BaseEntity, or no any data access methods for you.

Annotations

If you want to customize table name or column name, you can use Table and Column annotations

  • Table

    You can define table name and table id column name by using this annotation

  • Column

    You can define column name by using this annotation

About Dao interface

Dao

All Dao interfaces MUST extends this interface directly

About mapper xml file

Write your custom methods to mapper xml file

Note: The resultMap id is "${entity type name}ResultMap"

<?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" >
<!-- Basic methods please don't write to this file -->
<!-- The namespace attribute of mapper is not required in some scenarios -->
<mapper>
    <!-- The resultMap id is ${entity type name}ResultMap -->
    <select id="selectByUsername" resultMap="UserResultMap" >
        <!-- Include all columns name -->
        select <include refid="fields" />
        <!-- Include table name -->
        from <include refid="table" />
        where username = #{username}
    </select>
</mapper>

Basic methods

Method Description
saveOne Save one entity
save Save many entities
saveOrUpdate Save or update entity, if the entity has id value, then update it
deleteOne Delete by entity id
delete Delete by conditions
findOne Find entity by id
findFirst Find first record by conditions
find Find by conditions
count Returns records count by conditions
paging Returns paged entities by conditions
exists Check exists of entity id or conditions
update Update entity by id
updateCondition Update entities by condition
updateNullable Update entity by id, and null value properties update to null
updateConditionNullable Update entities by condition, and null value properties update to null

Condition

Some "dao" methods requires condition object parameter, here are some examples for you:

  1. Find by page

    // select identity, username from user limit 0, 10;
    userDao.find(new Condition<>(User.class).page(1).size(10));
  2. Find by page and sort

    // select identity, username from user order by identity desc limit 0, 10;
    userDao.find(new Condition<>(User.class).page(1).size(10).desc("identity"));
  3. Find by conditions

    // select identity, username from user where username = 'jack';
    userDao.find(new Condition<>(User.class).where().equalTo("username","jack").end());
  4. Delete by conditions

    // delete from user where username like '%ja%';
    userDao.delete(new Condition<>(User.class).where().like("username","ja").end());
    // delete from user where username like 'ja%';
    userDao.delete(new Condition<>(User.class).where().llike("username","ja").end());
  5. Count by conditions

    // select count(1) from user where username = 'jack';
    userDao.count(new Condition<>(User.class).where().equalTo("username","jack").end());
  6. Exists by conditions

    // select count(1) from user where username = 'jack';
    // Translate to boolean value automatically
    userDao.exists(new Condition<>(User.class).where().equalTo("username","jack").end());

How to configure without spring support

  1. Configuration file mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <settings>
            <setting name="useGeneratedKeys" value="true"/>
        </settings>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="Your database url"/>
                    <property name="username" value="Your database username"/>
                    <property name="password" value="Your database password"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper class="me.gaigeshen.mybatis.helper.mapper.UserDao" />
            <!-- <mapper resource="me/gaigeshen/mybatis/helper/mapper/UserDao.xml" /> -->
            
            <!-- If use url attribute, the namespace of mapper is required -->
            <mapper url="" />
        </mappers>
    </configuration>
  2. Configure helper by using MybatisHelperConfigurer before mybatis SqlSessionFactory build

    MybatisHelperConfigurer configurer = MybatisHelperConfigurer.create().configure();
    InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
  3. Initialize result mappings by using mybatis Configuration object

    configurer.initializeResultMappings(sqlSessionFactory.getConfiguration());
  4. Now, you can use mybatis APIs

    SqlSession session = sqlSessionFactory.openSession();
    UserDao userDao = session.getMapper(UserDao.class);

How to configure with spring boot support

<!-- Enable mybatis helper -->
<dependency>
    <groupId>me.gaigeshen.mybatis</groupId>
    <artifactId>mybatis-helper-spring-boot-starter</artifactId>
    <version>${version}</version> <!-- version must be great than or equals 1.7.0 -->
</dependency>
<!-- Auto configure mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

Versions

Version
1.6.0
1.5.0
1.4.0
1.3.0
1.2.1
1.2.0
1.1.0
1.0.0