MyBatisTestRunner.java 6.6 KB
Newer Older
M
MaxKey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
18 19
package org.apache.mybatis.jpa.test;

M
find  
MaxKey 已提交
20
import java.sql.Types;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
21 22 23 24 25 26
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;
M
MaxKey 已提交
27
import org.apache.mybatis.jpa.test.entity.Students;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
28 29 30 31 32 33 34 35 36 37 38
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;
MaxKey单点登录官方's avatar
Test  
MaxKey单点登录官方 已提交
39
	public static StudentsService service;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
40 41 42 43 44
	
	@Test
	public void insert() throws Exception{
		_logger.info("insert...");
		Students student=new Students();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
45
		//student.setId("10024");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
46 47 48 49 50 51
		student.setStdNo("10024");
		student.setStdGender("M");
		student.setStdName("司马昭");
		student.setStdAge(20);
		student.setStdMajor("政治");
		student.setStdClass("4");
MaxKey单点登录官方's avatar
Test  
MaxKey单点登录官方 已提交
52
		service.insert(student);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
53 54
		
		Thread.sleep(1000);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
55
		_logger.info("insert id " + student.getId());
M
Highgo  
MaxKey 已提交
56
		//service.remove(student.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
57 58
	}
	
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	@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());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
74
		//service.remove(student.getId());
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
75 76 77
		
	}
	
M
find  
MaxKey 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	@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}
						)
		);	
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
94 95 96
	@Test
	public void get() throws Exception{
		_logger.info("get...");
MaxKey单点登录官方's avatar
JPA 3  
MaxKey单点登录官方 已提交
97
		Students student=service.get("317d5eda-927c-4871-a916-472a8062df23");
MaxKey单点登录官方's avatar
log4j2  
MaxKey单点登录官方 已提交
98
		System.out.println("Students "+student);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
99 100 101
		 _logger.info("Students "+student);
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	@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.");
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
119 120 121 122 123
	@Test
	public void remove() throws Exception{
		_logger.info("remove...");
		Students student=new Students();
		student.setId("921d3377-937a-4578-b1e2-92fb23b5e512");
MaxKey单点登录官方's avatar
Test  
MaxKey单点登录官方 已提交
124
		service.remove(student.getId());
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
125 126 127 128 129 130 131 132 133 134
	}
	
	@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");
M
MaxKey 已提交
135 136 137 138 139 140 141 142 143 144 145 146
		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);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
147
	}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
148 149 150 151
	
	@Test
	public void batchDeleteByIds() throws Exception{
		_logger.info("batchDeleteByIds...");
M
MaxKey 已提交
152 153
		service.deleteBatch("2");
		service.deleteBatch("2,639178432667713536");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
154
	}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
155 156 157 158 159 160 161

	@Test
	public void queryPageResults() throws Exception{
		
		_logger.info("queryPageResults...");
		 Students student=new Students();
		 //student.setId("af04d610-6092-481e-9558-30bd63ef783c");
M
MaxKey 已提交
162
		 //student.setStdGender("M");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
163
		 //student.setStdMajor(政治");
MaxKey单点登录官方's avatar
GA  
MaxKey单点登录官方 已提交
164
		 student.setPageSize(10);
M
MaxKey 已提交
165 166
		 //student.setPageNumber(2);
		 student.calculate(21);
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
167 168 169 170 171
		 List<Students> allListStudents = 
				 service.queryPageResults(student).getRows();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
172 173 174 175 176 177 178 179 180
	}
	
	@Test
	public void queryPageResultsByMapperId() throws Exception{

		_logger.info("queryPageResults by mapperId...");
		 Students student=new Students();
		 student.setStdGender("M");
		 //student.setStdMajor(政治");
MaxKey单点登录官方's avatar
GA  
MaxKey单点登录官方 已提交
181 182
		 student.setPageSize(10);
		 student.setPageNumber(2);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
183
		 
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
184 185 186 187 188
		 List<Students> allListStudents = 
				 service.queryPageResults("queryPageResults1",student).getRows();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
189 190
	}
	
M
fix  
MaxKey 已提交
191 192 193 194 195 196 197 198 199
	@Test
	public void query() throws Exception{
		_logger.info("findAll...");
		List<Students> allListStudents =service.query(null);
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
	}
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
200 201 202
	@Test
	public void findAll() throws Exception{
		_logger.info("findAll...");
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
203 204 205 206
		List<Students> allListStudents =service.findAll();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	}
	
	@Before
	public void initSpringContext(){
		if(context!=null) return;
		_logger.info("init Spring Context...");
		SimpleDateFormat sdf_ymdhms =new SimpleDateFormat("yyyy-MM-dd HH:mm: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"});
		
MaxKey单点登录官方's avatar
Test  
MaxKey单点登录官方 已提交
230 231
		WebContext.applicationContext=context;
		service =(StudentsService)WebContext.getBean("studentsService");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
232
	}
M
MaxKey 已提交
233
}