Java Code Examples for net.dreamlu.mica.core.utils.BeanUtil#copy()

The following examples show how to use net.dreamlu.mica.core.utils.BeanUtil#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test4() {
	User1 user1 = new User1();
	user1.setId("1");
	user1.setName("张三");
	user1.setSix("男");

	User userx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userx);

	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");
	BeanUtil.copy(user1, user);
	System.out.println(user);

	Assert.assertNull(user.getId());
	Assert.assertEquals("张三", user.getName());
}
 
Example 2
Source File: BeanTest1.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
		List<Test1> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			Test1 test1 = new Test1();
			test1.setId(i);
			test1.setName("name" + i);
			test1.setAge(18 + i);
			list.add(test1);
		}
		Class<?> clazz = Test2.class;
		TestN1 testN1 = new TestN1();
		testN1.setList(list);
		TestN2 testN2 = BeanUtil.copy(testN1, TestN2.class);
		System.out.println(testN2);

//		ResolvableType.forMethodParameter(writeMethod, 0)
//			.getGeneric(0)
//			.resolve()

	}
 
Example 3
Source File: UserCopy3Test.java    From mica-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 使用 mica 的 BeanUtil copy 方法
	UserVo userVo = BeanUtil.copy(user, UserVo.class);

	// 3. 打印结果:UserVo(name=如梦技术, age=30, birthday=null)
	System.out.println(userVo);
}
 
Example 4
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test5() {
	List<User1> list = new ArrayList<>();
	for (int i = 0; i < 10; i++) {
		User1 user1 = new User1();
		user1.setId(i + "");
		user1.setName("张三" + i);
		list.add(user1);
	}
	List<User> copy1 = BeanUtil.copy(list, User.class);
	copy1.forEach(System.out::println);
	List<User> copy2 = BeanUtil.copyWithConvert(list, User.class);
	copy2.forEach(System.out::println);
}
 
Example 5
Source File: MapToBeanTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = BeanCopyUtilTest.class.getResource("/").getPath().split("mica-core")[0];
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath + "gen_code");

	Map<String, Object> map = new HashMap<>();
	map.put("id", 1);
	map.put("name", "张三");
	map.put("six", "女");
	map.put("gender", "男");
	map.put("xx", "xx");
	map.put("xInt", 100);
	map.put("xxInt", 101);
	map.put("xLong", 10000L);

	User1 user1 = BeanUtil.copy(map, User1.class);
	System.out.println(user1);
	System.out.println(BeanUtil.toMap(user1));

	User1 userx = new User1();
	BeanUtil.copy(user1, userx);
	System.out.println(userx);

	User1 user2 = BeanUtil.copyWithConvert(map, User1.class);
	System.out.println(user2);

	User user3 = BeanUtil.copy(map, User.class);
	System.out.println(user3);

	User user4 = BeanUtil.copyWithConvert(map, User.class);
	System.out.println(user4);

	User user5 = BeanUtil.copy(user2, User.class);
	System.out.println(user5);
}
 
Example 6
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser micaBeanCopyBMillion() {
	ToUser toUser = null;
	for (int i = 0; i < 10000; i++) {
		toUser = BeanUtil.copy(user, ToUser.class);
	}
	return toUser;
}
 
Example 7
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = BeanUtilTest.class.getResource("/").getPath().split("mica-core")[0];
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath + "gen_code");

	User1 user1 = new User1();
	user1.setId("1");
	user1.setId1(11);
	user1.setIds(new Integer[]{1, 2, 3});
	user1.setIdss(new int[]{1, 2, 3, 4, 5, 6});
	user1.setIdx(new long[]{1, 2, 3, 4, 5, 6});
	user1.setName("张三");

	BeanUtil.toMap(user1);

	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");

	BeanUtil.copy(user1, user);
	System.out.println(user);

	User userx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userx);

	User userxx = new User();
	userxx.setXx("123123");
	userxx.setPhoto("www.dreamlu.net/img/1");
	BeanUtil.copyNonNull(user1, userxx);
	System.out.println(userxx);

	User userxxx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userxxx);

	UserChain userChain = BeanUtil.copy(user, UserChain.class);
	System.out.println(userChain);

	Map<String, Object> data = new HashMap<>();
	data.put("id", 1);
	UserChain userChainx = BeanUtil.copy(data, UserChain.class);
	System.out.println(userChainx);

	Map<String, Object> data1 = new HashMap<>();
	data1.put("id", 1);
	data1.put("name", 1);
	data1.put("photo", 1);
	data1.put("xx", 1);
	UserChain userChainx1 = BeanUtil.copyWithConvert(data1, UserChain.class);
	System.out.println(userChainx1);

	Map<String, Object> data2 = new HashMap<>();
	data2.put("id", 1);
	data2.put("name", "1");
	data2.put("photo", "1");
	data2.put("xx", "1");
	data2.put("a", new int[]{1, 2, 3});
	data2.put("allowNull", null);

	UserChain userChainxxxx = BeanUtil.toBean(data2, UserChain.class);
	System.out.println(userChainxxxx);
	Map<String, Object> dataxxxx = BeanUtil.toMap(userChainxxxx);
	System.out.println(dataxxxx);

	UserChain userChainNull = new UserChain();
	userChainNull.setAllowNull(10000);
	BeanUtil.copyNonNull(data2, userChainNull);
	System.out.println(userChainNull);

	UserChain userChainNonNull = new UserChain();
	userChainNonNull.setAllowNull(10000);
	BeanUtil.copy(data2, userChainNonNull);
	System.out.println(userChainNonNull);
}
 
Example 8
Source File: BeanCopyListBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public List<ToUser> micaBeanCopy() {
	return BeanUtil.copy(userList, ToUser.class);
}
 
Example 9
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopy() {
	return BeanUtil.copy(user, ToUser.class);
}
 
Example 10
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopyAOne() {
	return BeanUtil.copy(user, ToUser.class);
}
 
Example 11
Source File: BeanCopyMapBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopy() {
	return BeanUtil.copy(userMap, ToUser.class);
}
 
Example 12
Source File: BeanConvertTest.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser MapToBeanMica() {
	return BeanUtil.copy(userMap, ToUser.class);
}