1 Star 0 Fork 0

_85024610 / mybatis_jpa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

mybatis_jpa

介绍

本插件实现类似Spring data jpa功能,通过方法名动态生成SQL进行查询,提升开发效率

安装教程

  • 拦截器配置(整合Spring)

    <bean id="myBatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ods-osp-stateDataSource" />        
        <property name="mapperLocations" >
            <list>
                <value>classpath*:mapper/*Mapper.xml</value>
            </list> 
        </property>
    
        <!-- 配置Mybatis的插件plugin-->
        <property name="plugins">
            <array>
                <bean id="jpaInterceptor" class="com.mybatis.jpa.interceptor.JpaInterceptor"></bean>
            </array> 
        </property>
    </bean>

使用说明

  • Java Bean 注解配置
//没有使用@Table,类名转化成表名,由驼峰转成下划线写法,例如:JobStateIndex --> job_state_index
//使用@Table注解就以name的值对应数据库的表名
@Table(name="job_state_index")
public class JobStateIndex {

	@Id //主键,必须定义,否则报异常
	private Long id;
	
    @Column(name="index_num") //字段名,功能和@Table方法一样
	private String orderSn;
	
	// 没有使用@Column,属性名转成数据库字段名
	private Integer indexNum;
	
	private Date createTime;
}
  • 使用方法
public interface CrudRepository<T> {

    // 提供批量insert方法,保存后主键回写对象有@Id注解的属性上
    @Insert("")
    @Options(useGeneratedKeys = true)
    void saveAll(List<T> list);
}


// 必须继承CrudRepository接口
public interface SelectRepository extends CrudRepository<JobStateIndex> {


	@Select("")  // 必填
	@ResultMap("AutoMapperResultMap") // 对应定义resultMap,通常在XML已经定义
	@Options( flushCache = true) // 关闭一级缓存,解决在同一个session里多次调用只返回第一次查询的结果
	List<JobStateIndex> findByOrderSn(String orderSn);

	@Select("")
	@ResultMap("AutoMapperResultMap")
	List<JobStateIndex> findByIdIn(List<Integer> ids);

	@Select("")
	@ResultMap("AutoMapperResultMap")
	List<JobStateIndex> findByIdInAndIndexNum(List<Integer> ids, int indexNum);
}

关键字说明

keyword Sample 说明
findBy findByOrderSn 声明使用select sql
And findByIdInAndIndexNum where x.id = ? and x.index_num = ?
Equals findById, findByIdEquals where x.id = ?
LessThan findByLessThan where x.id < ?
LessThanEqual findByIdLessThanEqual where x.id <= ?
GreaterThan findByIdGreaterThan where x.id > ?
GreaterThanEqual findByIdGreaterThanEqual where x.id >= ?
In findByIdIn(List ids) where x.id in (?)

空文件

简介

暂无描述 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/85024610/mybatis_jpa.git
git@gitee.com:85024610/mybatis_jpa.git
85024610
mybatis_jpa
mybatis_jpa
master

搜索帮助