public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
public class FooDto
{
public int ID { get; set; }
public string Name { get; set; }
}
public void Map()
{
var config = new MapperConfiguration(cfg => cfg.CreateMap());
var mapper = config.CreateMapper();
Foo foo = new Foo { ID = 1, Name = "Tom" };
FooDto dto = mapper.Map(foo);
}
2 注册
在使用 Map 方法之前,首先要告诉 AutoMapper 什么类可以映射到什么类。
var config = new MapperConfiguration(cfg => cfg.CreateMap());
每个 AppDomain 只能进行一次配置。这意味着放置配置代码的最佳位置是在应用程序启动中,例如 ASP.NET 应用程序的 Global.asax 文件。
从 9.0 开始 Mapper.Initialize 方法就不可用了。
2.1 Profile
Profile 是组织映射的另一种方式。新建一个类,继承 Profile,并在构造函数中配置映射。
public class EmployeeProfile : Profile
{
public EmployeeProfile()
{
CreateMap();
}
}
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile();
});
public class Foo
{
public int Id { get; set; }
public string MyName { get; set; }
}
public class FooDto
{
public int ID { get; set; }
public string My_Name { get; set; }
}
public void Map()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap();
cfg.SourceMemberNamingConvention = new PascalCaseNamingConvention();
cfg.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
});
var mapper = config.CreateMapper();
Foo foo = new Foo { Id = 2, MyName = "Tom" };
FooDto dto = mapper.Map(foo);
}
3.2 配置可见性
默认情况下,AutoMapper 仅映射 public 成员,但其实它是可以映射到 private 属性的。
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.SetMethod.IsPrivate;
cfg.CreateMap();
});
需要注意的是,这里属性必须添加 private set,省略 set 是不行的。
3.3 全局属性/字段过滤
默认情况下,AutoMapper 尝试映射每个公共属性/字段。以下配置将忽略字段映射。
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapField = fi => false;
});
3.4 识别前缀和后缀
var config = new MapperConfiguration(cfg =>
{
cfg.RecognizePrefixes("My");
cfg.RecognizePostfixes("My");
}
3.5 替换字符
var config = new MapperConfiguration(cfg =>
{
cfg.ReplaceMemberName("Ä", "A");
});
这功能我们基本上用不上。
4 调用构造函数
有些类,属性的 set 方法是私有的。
public class Commodity
{
public string Name { get; set; }
public int Price { get; set; }
}
public class CommodityDto
{
public string Name { get; }
public int Price { get; }
public CommodityDto(string name, int price)
{
Name = name;
Price = price * 2;
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Employee2 : Employee
{
public string DeptName { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
}
public class EmployeeDto2 : EmployeeDto
{
public string DeptName { get; set; }
}
数组映射代码如下:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().Include();
cfg.CreateMap();
});
IMapper mapper = config.CreateMapper();
var employees = new[]
{
new Employee { ID = 1, Name = "Tom" },
new Employee2 { ID = 2, Name = "Jerry", DeptName = "R & D" }
};
var dto = mapper.Map(employees);
public class Employee
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetFullName()
{
return $"{FirstName} {LastName}";
}
}
public class EmployeeDto
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
}
7 自定义映射
当源类型与目标类型名称不一致时,或者需要对源数据做一些转换时,可以用自定义映射。
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoinTime { get; set; }
}
public class EmployeeDto
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public int JoinYear { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public int DepartmentID { get; set; }
public string DepartmentName { get; set; }
}
Department 类中的属性名,直接跟 EmployeeDto 类中的属性名一致,则可以使用 IncludeMembers 方法指定。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().IncludeMembers(e => e.Department);
cfg.CreateMap();
});
9 嵌套映射
有时,我们可能不需要展平。看如下例子:
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public string Heads { get; set; }
}
public class EmployeeDto
{
public int ID { get; set; }
public string Name { get; set; }
public DepartmentDto Department { get; set; }
}
public class DepartmentDto
{
public int ID { get; set; }
public string Name { get; set; }
}
我们要将 Employee 映射到 EmployeeDto,并且将 Department 映射到 DepartmentDto。
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap();
cfg.CreateMap();
});
Tomcat的组成部分 1、server
A Server element represents the entire Catalina servlet container. (Singleton) 2、service
service包括多个connector以及一个engine,其职责为处理由connector获得的客户请求。
3、connector
一个connector
基本概念: 1.OOP中唯一关系的是对象的接口是什么,就像计算机的销售商她不管电源内部结构是怎样的,他只关系能否给你提供电就行了,也就是只要知道can or not而不是how and why.所有的程序是由一定的属性和行为对象组成的,不同的对象的访问通过函数调用来完成,对象间所有的交流都是通过方法调用,通过对封装对象数据,很大限度上提高复用率。 2.OOP中最重要的思想是类,类是模板是蓝图,
由于明天举要上课,所以刚刚将代码敲了一遍PL/SQL的函数和包体的实现(单例模式过几天好好的总结下再发出来);以便明天能更好的学习PL/SQL的循环,今天太累了,所以早点睡觉,明天继续PL/SQL总有一天我会将你永远的记载在心里,,,
函数;
函数:PL/SQL中的函数相当于java中的方法;函数有返回值
定义函数的
--输入姓名找到该姓名的年薪
create or re
/*
*编写控制结构
*/
--条件分支语句
--简单条件判断
DECLARE
v_sal NUMBER(6,2);
BEGIN
select sal into v_sal from emp
where lower(ename)=lower('&name');
if v_sal<2000 then
update emp set
public class CollectionDemo implements Serializable,Comparable<CollectionDemo>{
private static final long serialVersionUID = -2958090810811192128L;
private int id;
private String nam