内容简介:MyBatis JPA Extra对MyBatis进行了JPA扩展,旨在基于JPA 2.1的注释简化对单表CUID操作,根据JPA注释动态生成SQL语句;使用Interceptor实现数据库SELECT分页查询,适配多种数据库;另外提供mybatis-jpa-extra-spr...
MyBatis JPA Extra对MyBatis进行了JPA扩展,旨在基于JPA 2.1的注释简化对单表CUID操作,根据JPA注释动态生成 SQL 语句;使用Interceptor实现数据库SELECT分页查询,适配多种数据库;另外提供mybatis-jpa-extra-spring-boot-starter简化SpringBoot集成。
MyBatis JPA Extra对MyBatis扩展JPA功能
1.基于JPA 2.1的注释简化CUID操作;
2.用Interceptor实现数据库SELECT分页查询;
3.提供mybatis-jpa-extra-spring-boot-starter,简化SpringBoot集成;
1、JavaBean注释简单
仅有6个注释
@GeneratedValue有4中策略
-
AUTO
snowflakeid
uuid
uuid.hex
serial
-
SEQUENCE
generator值为数据库序列名
-
IDENTITY
generator无,根据数据库自动生成方式
package org.apache.mybatis.jpa.test.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.apache.mybatis.jpa.persistence.JpaBaseEntity; @Entity @Table(name = "STUDENTS") public class Students extends JpaBaseEntity implements Serializable{ private static final long serialVersionUID = -6928570405840778151L; @Id @Column @GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid") //@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_MYBATIS_STUD") //@GeneratedValue(strategy=GenerationType.IDENTITY,generator="SEQ_MYBATIS_STUD") private String id; @Column private String stdNo; @Column private String stdName; @Column private String stdGender; @Column private int stdAge; @Column private String stdMajor; @Column private String stdClass; @Column private byte[] images; public Students() { super(); } public get(){}; public void set(){}; //... }
2、单表新增、修改、删除、查询、分页查询
package org.apache.mybatis.jpa.test; import java.sql.Types; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.mybatis.jpa.test.dao.service.StudentsService; import org.apache.mybatis.jpa.test.entity.Students; import org.apache.mybatis.jpa.util.WebContext; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyBatisTestRunner { private static final Logger _logger = LoggerFactory.getLogger(MyBatisTestRunner.class); public static ApplicationContext context; public static StudentsService service; @Test public void insert() throws Exception{ _logger.info("insert..."); Students student=new Students(); //student.setId("10024"); student.setStdNo("10024"); student.setStdGender("M"); student.setStdName("司马昭"); student.setStdAge(20); student.setStdMajor("政治"); student.setStdClass("4"); service.insert(student); Thread.sleep(1000); _logger.info("insert id " + student.getId()); //service.remove(student.getId()); } @Test public void merge() throws Exception{ _logger.info("merge..."); Students student=new Students(); //student.setId("10024"); student.setStdNo("10024"); student.setStdGender("M"); student.setStdName("司马昭"); student.setStdAge(20); student.setStdMajor("政治"); student.setStdClass("4"); service.merge(student); Thread.sleep(1000); _logger.info("insert id " + student.getId()); } @Test public void find() throws Exception{ _logger.info("find..."); _logger.info("find by filter " + service.find(" StdNo = '10024' or StdNo = '10004'") ); _logger.info("find by filter with args " + service.find( " StdNo = ? or StdNo = ? ", new Object[]{"10024","10004"}, new int[]{Types.VARCHAR,Types.INTEGER} ) ); } @Test public void get() throws Exception{ _logger.info("get..."); Students student=service.get("317d5eda-927c-4871-a916-472a8062df23"); System.out.println("Students "+student); _logger.info("Students "+student); } @Test public void update() throws Exception{ _logger.info("get..."); Students student=service.get("317d5eda-927c-4871-a916-472a8062df23"); System.out.println("Students "+student); _logger.info("Students "+student); _logger.info("update..."); student.setImages(null); service.update(student); _logger.info("updateed."); student.setImages("ssss".getBytes()); service.update(student); _logger.info("updateed2."); } @Test public void remove() throws Exception{ _logger.info("remove..."); Students student=new Students(); student.setId("921d3377-937a-4578-b1e2-92fb23b5e512"); service.remove(student.getId()); } @Test public void batchDelete() throws Exception{ _logger.info("batchDelete..."); List<String> idList=new ArrayList<String>(); idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7"); idList.add("ab7422e9-a91a-4840-9e59-9d911257c918"); idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c"); idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe"); service.deleteBatch(idList); } @Test public void logicDelete() throws Exception{ _logger.info("logicDelete..."); List<String> idList=new ArrayList<String>(); idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7"); idList.add("ab7422e9-a91a-4840-9e59-9d911257c918"); idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c"); idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe"); service.logicDelete(idList); } @Test public void batchDeleteByIds() throws Exception{ _logger.info("batchDeleteByIds..."); service.deleteBatch("2"); service.deleteBatch("2,639178432667713536"); } @Test public void queryPageResults() throws Exception{ _logger.info("queryPageResults..."); Students student=new Students(); //student.setId("af04d610-6092-481e-9558-30bd63ef783c"); //student.setStdGender("M"); //student.setStdMajor(政治"); student.setPageSize(10); //student.setPageNumber(2); student.calculate(21); List<Students> allListStudents = service.queryPageResults(student).getRows(); for (Students s : allListStudents) { _logger.info("Students "+s); } } @Test public void queryPageResultsByMapperId() throws Exception{ _logger.info("queryPageResults by mapperId..."); Students student=new Students(); student.setStdGender("M"); //student.setStdMajor(政治"); student.setPageSize(10); student.setPageNumber(2); List<Students> allListStudents = service.queryPageResults("queryPageResults1",student).getRows(); for (Students s : allListStudents) { _logger.info("Students "+s); } } @Test public void query() throws Exception{ _logger.info("findAll..."); List<Students> allListStudents =service.query(null); for (Students s : allListStudents) { _logger.info("Students "+s); } } @Test public void findAll() throws Exception{ _logger.info("findAll..."); List<Students> allListStudents =service.findAll(); for (Students s : allListStudents) { _logger.info("Students "+s); } } @Before public void initSpringContext(){ if(context!=null) return; _logger.info("init Spring Context..."); SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH????????ss"); String startTime=sdf_ymdhms.format(new Date()); try{ MyBatisTestRunner runner=new MyBatisTestRunner(); runner.init(); }catch(Exception e){ e.printStackTrace(); } _logger.info("-- --Init Start at " + startTime+" , End at "+sdf_ymdhms.format(new Date())); } //Initialization ApplicationContext for Project public void init(){ _logger.info("Application dir "+System.getProperty("user.dir")); context = new ClassPathXmlApplicationContext(new String[] {"spring/applicationContext.xml"}); WebContext.applicationContext=context; service =(StudentsService)WebContext.getBean("studentsService"); } }
版本更新
代码优化
新增find方法,根据过滤器查询数据
新增insertBatch方法
新增deleteBatch方法
删除batchInsert方法
删除batchDelete方法
日志功能优化
缓存调整为caffeine
升级log4j2 2.17.0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- NPM包(模块)发布、更新、撤销发布
- 有赞灰度发布与蓝绿发布实践
- 【重磅发布】Linkis 0.10.0 版本发布
- BeetlSQL 3.0.9 发布,Idea 插件发布
- 贝密游戏 0.7.0 发布,发布斗地主
- 【重磅发布】DataSphere Studio 0.9.0 版本发布
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First Rails
David Griffiths / O'Reilly Media / 2008-12-30 / USD 49.99
Figure its about time that you hop on the Ruby on Rails bandwagon? You've heard that it'll increase your productivity exponentially, and allow you to created full fledged web applications with minimal......一起来看看 《Head First Rails》 这本书的介绍吧!