提交 7ac681ce 编写于 作者: M MaxKey

2.7 readme

上级 d3271d6d
# MyBatis JPA Extra # MyBatis JPA Extra
**MyBatis JPA Extra**对MyBatis扩展JPA功能 **MyBatis JPA Extra**对MyBatis扩展JPA功能
...@@ -42,7 +41,6 @@ ...@@ -42,7 +41,6 @@
```java ```java
package org.apache.mybatis.jpa.test.entity; package org.apache.mybatis.jpa.test.entity;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
...@@ -55,9 +53,6 @@ import org.apache.mybatis.jpa.persistence.JpaBaseEntity; ...@@ -55,9 +53,6 @@ import org.apache.mybatis.jpa.persistence.JpaBaseEntity;
@Entity @Entity
@Table(name = "STUDENTS") @Table(name = "STUDENTS")
public class Students extends JpaBaseEntity implements Serializable{ public class Students extends JpaBaseEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6928570405840778151L; private static final long serialVersionUID = -6928570405840778151L;
@Id @Id
...@@ -66,120 +61,43 @@ public class Students extends JpaBaseEntity implements Serializable{ ...@@ -66,120 +61,43 @@ public class Students extends JpaBaseEntity implements Serializable{
//@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_MYBATIS_STUD") //@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_MYBATIS_STUD")
//@GeneratedValue(strategy=GenerationType.IDENTITY,generator="SEQ_MYBATIS_STUD") //@GeneratedValue(strategy=GenerationType.IDENTITY,generator="SEQ_MYBATIS_STUD")
private String id; private String id;
@Column @Column
private String stdNo; private String stdNo;
@Column @Column
private String stdName; private String stdName;
@Column @Column
private String stdGender; private String stdGender;
@Column @Column
private int stdAge; private int stdAge;
@Column @Column
private String stdMajor; private String stdMajor;
@Column @Column
private String stdClass; private String stdClass;
@Column @Column
private byte[] images; private byte[] images;
public Students() { public Students() {
super(); super();
} }
public get(){};
public String getStdNo() { public void set(){};
return stdNo; //...
}
public void setStdNo(String stdNo) {
this.stdNo = stdNo;
}
public String getStdName() {
return stdName;
}
public void setStdName(String stdName) {
this.stdName = stdName;
}
public String getStdGender() {
return stdGender;
}
public void setStdGender(String stdGender) {
this.stdGender = stdGender;
}
public int getStdAge() {
return stdAge;
}
public void setStdAge(int stdAge) {
this.stdAge = stdAge;
}
public String getStdMajor() {
return stdMajor;
}
public void setStdMajor(String stdMajor) {
this.stdMajor = stdMajor;
}
public String getStdClass() {
return stdClass;
}
public void setStdClass(String stdClass) {
this.stdClass = stdClass;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public byte[] getImages() {
return images;
}
public void setImages(byte[] images) {
this.images = images;
}
} }
``` ```
## 2、单表新增、修改、删除、查询、分页查询 ## 2、单表新增、修改、删除、查询、分页查询
```java ```java
package org.apache.mybatis.jpa.test; package org.apache.mybatis.jpa.test;
import java.sql.Types;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
...@@ -195,13 +113,10 @@ import org.springframework.context.ApplicationContext; ...@@ -195,13 +113,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyBatisTestRunner { public class MyBatisTestRunner {
private static final Logger _logger = LoggerFactory.getLogger(MyBatisTestRunner.class); private static final Logger _logger = LoggerFactory.getLogger(MyBatisTestRunner.class);
public static ApplicationContext context; public static ApplicationContext context;
public static StudentsService service; public static StudentsService service;
@Test @Test
public void insert() throws Exception{ public void insert() throws Exception{
_logger.info("insert..."); _logger.info("insert...");
...@@ -218,10 +133,8 @@ public class MyBatisTestRunner { ...@@ -218,10 +133,8 @@ public class MyBatisTestRunner {
Thread.sleep(1000); Thread.sleep(1000);
_logger.info("insert id " + student.getId()); _logger.info("insert id " + student.getId());
//service.remove(student.getId()); //service.remove(student.getId());
} }
@Test @Test
public void merge() throws Exception{ public void merge() throws Exception{
_logger.info("merge..."); _logger.info("merge...");
...@@ -237,15 +150,28 @@ public class MyBatisTestRunner { ...@@ -237,15 +150,28 @@ public class MyBatisTestRunner {
Thread.sleep(1000); Thread.sleep(1000);
_logger.info("insert id " + student.getId()); _logger.info("insert id " + student.getId());
//service.remove(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 @Test
public void get() throws Exception{ public void get() throws Exception{
_logger.info("get..."); _logger.info("get...");
Students student=service.get("317d5eda-927c-4871-a916-472a8062df23"); Students student=service.get("317d5eda-927c-4871-a916-472a8062df23");
System.out.println("Students "+student); System.out.println("Students "+student);
_logger.info("Students "+student); _logger.info("Students "+student);
} }
...@@ -256,7 +182,6 @@ public class MyBatisTestRunner { ...@@ -256,7 +182,6 @@ public class MyBatisTestRunner {
Students student=service.get("317d5eda-927c-4871-a916-472a8062df23"); Students student=service.get("317d5eda-927c-4871-a916-472a8062df23");
System.out.println("Students "+student); System.out.println("Students "+student);
_logger.info("Students "+student); _logger.info("Students "+student);
_logger.info("update..."); _logger.info("update...");
student.setImages(null); student.setImages(null);
service.update(student); service.update(student);
...@@ -267,25 +192,12 @@ public class MyBatisTestRunner { ...@@ -267,25 +192,12 @@ public class MyBatisTestRunner {
_logger.info("updateed2."); _logger.info("updateed2.");
} }
@Test
public void find() throws Exception{
_logger.info("find...");
Students student=service.find(Students.class,"317d5eda-927c-4871-a916-472a8062df23");
System.out.println("Students "+student);
_logger.info("Students "+student);
}
@Test @Test
public void remove() throws Exception{ public void remove() throws Exception{
_logger.info("remove..."); _logger.info("remove...");
Students student=new Students(); Students student=new Students();
student.setId("921d3377-937a-4578-b1e2-92fb23b5e512"); student.setId("921d3377-937a-4578-b1e2-92fb23b5e512");
service.remove(student.getId()); service.remove(student.getId());
} }
@Test @Test
...@@ -296,19 +208,37 @@ public class MyBatisTestRunner { ...@@ -296,19 +208,37 @@ public class MyBatisTestRunner {
idList.add("ab7422e9-a91a-4840-9e59-9d911257c918"); idList.add("ab7422e9-a91a-4840-9e59-9d911257c918");
idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c"); idList.add("12b6ceb8-573b-4f01-ad85-cfb24cfa007c");
idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe"); idList.add("dafd5ba4-d2e3-4656-bd42-178841e610fe");
service.batchDelete(idList); 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 @Test
public void queryPageResults() throws Exception{ public void queryPageResults() throws Exception{
_logger.info("queryPageResults..."); _logger.info("queryPageResults...");
Students student=new Students(); Students student=new Students();
//student.setId("af04d610-6092-481e-9558-30bd63ef783c"); //student.setId("af04d610-6092-481e-9558-30bd63ef783c");
student.setStdGender("M"); //student.setStdGender("M");
//student.setStdMajor(政治"); //student.setStdMajor(政治");
student.setPageSize(10); student.setPageSize(10);
student.setPageNumber(1); //student.setPageNumber(2);
student.calculate(21);
List<Students> allListStudents = List<Students> allListStudents =
service.queryPageResults(student).getRows(); service.queryPageResults(student).getRows();
for (Students s : allListStudents) { for (Students s : allListStudents) {
...@@ -318,23 +248,27 @@ public class MyBatisTestRunner { ...@@ -318,23 +248,27 @@ public class MyBatisTestRunner {
@Test @Test
public void queryPageResultsByMapperId() throws Exception{ public void queryPageResultsByMapperId() throws Exception{
_logger.info("queryPageResults by mapperId..."); _logger.info("queryPageResults by mapperId...");
Students student=new Students(); Students student=new Students();
student.setStdGender("M"); student.setStdGender("M");
//student.setStdMajor(政治"); //student.setStdMajor(政治");
student.setPageSize(10); student.setPageSize(10);
student.setPageNumber(2); student.setPageNumber(2);
List<Students> allListStudents = List<Students> allListStudents =
service.queryPageResults("queryPageResults1",student).getRows(); service.queryPageResults("queryPageResults1",student).getRows();
for (Students s : allListStudents) { for (Students s : allListStudents) {
_logger.info("Students "+s); _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 @Test
public void findAll() throws Exception{ public void findAll() throws Exception{
...@@ -351,34 +285,25 @@ public class MyBatisTestRunner { ...@@ -351,34 +285,25 @@ public class MyBatisTestRunner {
_logger.info("init Spring Context..."); _logger.info("init Spring Context...");
SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startTime=sdf_ymdhms.format(new Date()); String startTime=sdf_ymdhms.format(new Date());
try{ try{
MyBatisTestRunner runner=new MyBatisTestRunner(); MyBatisTestRunner runner=new MyBatisTestRunner();
runner.init(); runner.init();
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
_logger.info("-- --Init Start at " + startTime+" , End at "+sdf_ymdhms.format(new Date())); _logger.info("-- --Init Start at " + startTime+" , End at "+sdf_ymdhms.format(new Date()));
} }
//Initialization ApplicationContext for Project //Initialization ApplicationContext for Project
public void init(){ public void init(){
_logger.info("Application dir "+System.getProperty("user.dir")); _logger.info("Application dir "+System.getProperty("user.dir"));
context = new ClassPathXmlApplicationContext(new String[] {"spring/applicationContext.xml"}); context = new ClassPathXmlApplicationContext(new String[] {"spring/applicationContext.xml"});
WebContext.applicationContext=context; WebContext.applicationContext=context;
service =(StudentsService)WebContext.getBean("studentsService"); service =(StudentsService)WebContext.getBean("studentsService");
} }
} }
``` ```
## 3、映射文件配置 ## 3、映射文件配置
```xml ```xml
......
...@@ -34,13 +34,10 @@ import org.springframework.context.ApplicationContext; ...@@ -34,13 +34,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyBatisTestRunner { public class MyBatisTestRunner {
private static final Logger _logger = LoggerFactory.getLogger(MyBatisTestRunner.class); private static final Logger _logger = LoggerFactory.getLogger(MyBatisTestRunner.class);
public static ApplicationContext context; public static ApplicationContext context;
public static StudentsService service; public static StudentsService service;
@Test @Test
public void insert() throws Exception{ public void insert() throws Exception{
_logger.info("insert..."); _logger.info("insert...");
...@@ -57,10 +54,8 @@ public class MyBatisTestRunner { ...@@ -57,10 +54,8 @@ public class MyBatisTestRunner {
Thread.sleep(1000); Thread.sleep(1000);
_logger.info("insert id " + student.getId()); _logger.info("insert id " + student.getId());
//service.remove(student.getId()); //service.remove(student.getId());
} }
@Test @Test
public void merge() throws Exception{ public void merge() throws Exception{
_logger.info("merge..."); _logger.info("merge...");
...@@ -94,14 +89,12 @@ public class MyBatisTestRunner { ...@@ -94,14 +89,12 @@ public class MyBatisTestRunner {
new int[]{Types.VARCHAR,Types.INTEGER} new int[]{Types.VARCHAR,Types.INTEGER}
) )
); );
} }
@Test @Test
public void get() throws Exception{ public void get() throws Exception{
_logger.info("get..."); _logger.info("get...");
Students student=service.get("317d5eda-927c-4871-a916-472a8062df23"); Students student=service.get("317d5eda-927c-4871-a916-472a8062df23");
System.out.println("Students "+student); System.out.println("Students "+student);
_logger.info("Students "+student); _logger.info("Students "+student);
} }
...@@ -125,12 +118,10 @@ public class MyBatisTestRunner { ...@@ -125,12 +118,10 @@ public class MyBatisTestRunner {
@Test @Test
public void remove() throws Exception{ public void remove() throws Exception{
_logger.info("remove..."); _logger.info("remove...");
Students student=new Students(); Students student=new Students();
student.setId("921d3377-937a-4578-b1e2-92fb23b5e512"); student.setId("921d3377-937a-4578-b1e2-92fb23b5e512");
service.remove(student.getId()); service.remove(student.getId());
} }
@Test @Test
...@@ -195,11 +186,8 @@ public class MyBatisTestRunner { ...@@ -195,11 +186,8 @@ public class MyBatisTestRunner {
for (Students s : allListStudents) { for (Students s : allListStudents) {
_logger.info("Students "+s); _logger.info("Students "+s);
} }
} }
@Test @Test
public void query() throws Exception{ public void query() throws Exception{
_logger.info("findAll..."); _logger.info("findAll...");
...@@ -224,15 +212,12 @@ public class MyBatisTestRunner { ...@@ -224,15 +212,12 @@ public class MyBatisTestRunner {
_logger.info("init Spring Context..."); _logger.info("init Spring Context...");
SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startTime=sdf_ymdhms.format(new Date()); String startTime=sdf_ymdhms.format(new Date());
try{ try{
MyBatisTestRunner runner=new MyBatisTestRunner(); MyBatisTestRunner runner=new MyBatisTestRunner();
runner.init(); runner.init();
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
_logger.info("-- --Init Start at " + startTime+" , End at "+sdf_ymdhms.format(new Date())); _logger.info("-- --Init Start at " + startTime+" , End at "+sdf_ymdhms.format(new Date()));
} }
...@@ -244,7 +229,5 @@ public class MyBatisTestRunner { ...@@ -244,7 +229,5 @@ public class MyBatisTestRunner {
WebContext.applicationContext=context; WebContext.applicationContext=context;
service =(StudentsService)WebContext.getBean("studentsService"); service =(StudentsService)WebContext.getBean("studentsService");
} }
}
} \ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册