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

	@Test
	public void queryPageResults() throws Exception{
		
		_logger.info("queryPageResults...");
		 Students student=new Students();
		 //student.setId("af04d610-6092-481e-9558-30bd63ef783c");
M
MaxKey 已提交
171
		 //student.setStdGender("M");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
172
		 //student.setStdMajor(政治");
MaxKey单点登录官方's avatar
GA  
MaxKey单点登录官方 已提交
173
		 student.setPageSize(10);
M
MaxKey 已提交
174 175
		 //student.setPageNumber(2);
		 student.calculate(21);
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
176 177 178 179 180
		 List<Students> allListStudents = 
				 service.queryPageResults(student).getRows();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
181 182 183 184 185 186 187 188 189
	}
	
	@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单点登录官方 已提交
190 191
		 student.setPageSize(10);
		 student.setPageNumber(2);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
192
		 
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
193 194 195 196 197
		 List<Students> allListStudents = 
				 service.queryPageResults("queryPageResults1",student).getRows();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
198 199 200 201 202
		 
	}
	
	
	
M
fix  
MaxKey 已提交
203 204 205 206 207 208 209 210 211
	@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单点登录官方 已提交
212 213 214
	@Test
	public void findAll() throws Exception{
		_logger.info("findAll...");
MaxKey单点登录官方's avatar
demo  
MaxKey单点登录官方 已提交
215 216 217 218
		List<Students> allListStudents =service.findAll();
		 for (Students s : allListStudents) {
			 _logger.info("Students "+s);
		 }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	}
	
	@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单点登录官方 已提交
245 246 247
		WebContext.applicationContext=context;
		service =(StudentsService)WebContext.getBean("studentsService");
		
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
248 249 250
	}
	
}