
本文共 5626 字,大约阅读时间需要 18 分钟。
������
���������������Myabtis������������SqlSessionFactory���SqlSessionFactoryBuilder().build()���������������������Builder������������������������������
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
1������������Builder���������
���������1���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������2������������������������������������������������������������������������������������������������������������������������
2������������������������Builder���������
���������1���������������������������������������������������������������������������������������������������������
���������2������������������������������������������������������������������������������������������������������������������������������������������������
���������3���������������������������������������������������������������������������������������������������������
���������4������������������Configuration������������������Builder���������
3���������������Builder������
���1���������������������������������������������������������������������������������������������������������������������User���������������������
- ������id���name
- ���id���name���age
- ���id���name���age���address
public class User { private int id; private String name; private int age; private String address; //������������������������������������������������������������������������ public User(int id, String name) { this.id = id; this.name = name; } public User(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public User(int id, String name, int age, String address) { this.id = id; this.name = name; this.age= age; this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
������������������������
public class Main { public static void main(String[] args) { //������1���id��������� User user = new User(1, "Zhangsan"); //������2���id������������������ User user2 = new User(2,"Lisi",22); //������3���id��������������������������� User user3 = new User(3,"Wangwu",23,"Beijing"); //������4���id������������������������������������������������������������������������������������������������������id/������ User user4 = new User(24,"Wangwu",24,"Beijing"); }}
OK���������������������������������������������������������
- ���������������4���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������(Open-Closed Principle, OCP������������������)
- ���������������������������������������������������������������������������������������address���������������������������������������������������������������������
���2���������������������Builder������������������������Builder������������Java���������������
/** * ������Builer��������������������������������������� * @author Lijian * */public class User2 { private int id; private String name; private int age; private String address; private User2(Builder builder) { this.id = builder.id; this.name = builder.name; this.age = builder.age; this.address = builder.address; } static class Builder{ private int id; private String name; private int age; private String address; //������������������ public Builder setId(int id) { this.id = id; return this; } public Builder setName(String name) { this.name = name; return this; } public Builder setAddress(String address) { this.address = address; return this; } public Builder setAge(int age) { this.age = age; return this; } //������build������User2������ public User2 build() { return new User2(this); } }}
������������
public class Main { public static void main(String[] args) { //������build���������User2������,������������setXXX���������������������������������������build������������ User2 user = new User2.Builder().setId(1).setName("Lijian").setAge(22).build(); //������1���id��������� User2 user2 = new User2.Builder().setId(2).setName("Zhangsan").build(); //������2���id��������������������������� User2 user3 = new User2.Builder().setId(2).setName("Lisi").build(); //������3���id���age������������������ User2 user4 = new User2.Builder().setId(23).setAge(23).build(); }}
������setXXX()���������������������������������build()���������������������������������������������������������������������������������������Java8���������������Stream API���������������������������������������������������������������������������������������������������������������������������������������������������������build()���������������
发表评论
最新留言
关于作者
