mapperdb - Mybatis mapper 单表处理工具


Apache
跨平台
Java

软件简介

mapperdb

作用于 mybatis 的 mapper 的单表增删改查(mybatis v3.2.4+,无xml配置配置哦)。

1、entity 配置

package com.demo.domain;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.type.JdbcType;

@Resource
public class Blog {
    private Integer id;

    private String title;

    private Date publishtime;

    private String content;

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

    public void setTitle(String title) {
        this.title = title;
    }

    public void setPublishtime(Date publishtime) {
        this.publishtime = publishtime;
    }

    public void setContent(String content) {
        this.content = content;
    }

    // call identity()
    // SELECT LAST_INSERT_ID()
    @Result(id = true)
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", before = false, keyProperty = "id", keyColumn = "id", resultType = Integer.class)
    public Integer getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    @Result(jdbcType=JdbcType.TIMESTAMP)
    public Date getPublishTime() {
        return publishtime;
    }

    public String getContent() {
        return content;
    }
}

Result可以配置 TypeHandler

2、mybatis 配置

    public void t() {
        Object b = null;
        // Mapper 添加
        MapperRegistryProxy proxy = MapperRegistryProxy.use(sqlSessionFactory);
        proxy.addMapper(BlogMapper.class);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    }

MapperRegistryProxy 注册 mapper 模板.

3、spring 整合

    <bean class="com.mapperdb.spring.MapperScannerConfigurerSupprot">
        <property name="basePackage" value="com.demo.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

其中 MapperScannerConfigurerSupprot 是 mapper 模板的扫描器 .

4、mapper 使用方式

package com.demo.mapper;

import com.demo.domain.Blog;
import com.mapperdb.mapper.Mapper;

public interface BlogMapper extends Mapper<Blog> {

}

其中 BlogMapper 继承已经定义好的 mapper 模板 . 还可以实现自己的 mapper 模板.

5、注意事项

text 建议和 mapper 模板和 mybatis 的 mapper 不放在同一个包中,方便扫描。