???????????????leader??????????????????????????????????????????or?????????????????for????????????ò?????false????????????or???????????????????true??????????????ζ???????????????or?????????????????????or???????????????????SQL????β?????????or????????????????????????????findUserInfo??????????????????strategy??1??????????????strategy??2??????????????
?????????????????????????·????
????public class Test {
????public static void main(String[] args) throws Exception {
????QueryUtil query = new QueryUtil();
????query.findUserInfo(new String[] { "Tom"?? "Jim"?? "Anna" }?? 2);
????}
????}
???????????????????????????????????SQL????????????????????????????????
??????????????leader???????????????????????????????????????????????????????????
????“??????д?????findUserInfo??????????????????????????????????????????????????????????е????????????????????????????????”  ???leader?????????????????????ò??????????????????????????????????????????????????????С?
???????????????????????????????????????ò??????????????
?????????????????????
????public interface Strategy {
????String getSQL(String[] usernames);
????}
????????????????????????????????????????SQL?????????????????????У?
public class Strategy1 implements Strategy {
@Override
public String getSQL(String[] usernames) {
StringBuilder sql = new StringBuilder("select * from user_info where ");
for (String user : usernames) {
sql.append("username = '");
sql.append(user);
sql.append("' or ");
}
sql.delete(sql.length() - " or ".length()?? sql.length());
return sql.toString();
}
}
public class Strategy2 implements Strategy {
@Override
public String getSQL(String[] usernames) {
StringBuilder sql = new StringBuilder("select * from user_info where ");
boolean needOr = false;
for (String user : usernames) {
if (needOr) {
sql.append(" or ");
}
sql.append("username = '");
sql.append(user);
sql.append("'");
needOr = true;
}
return sql.toString();
}
}
????????QueryUtil??findUserInfo?????????????????Strategy????????????????Strategy??getSQL???????????????SQL????????????????
public class QueryUtil {
public void findUserInfo(String[] usernames?? Strategy strategy) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test"?? "root"??
"123456");
Statement stat = conn.createStatement();
String sql = strategy.getSQL(usernames);
System.out.println(sql);
ResultSet resultSet = stat.executeQuery(sql);
while (resultSet.next()) {
// ?????????????????????
}
// ?????????????????????????????????????
}
}
??????????????????findUserInfo???????????????????????????????????????????
????public class Test {
????public static void main(String[] args) throws Exception {
????QueryUtil query = new QueryUtil();
????query.findUserInfo(new String[] { "Tom"?? "Jim"?? "Anna" }?? new Strategy1());
????query.findUserInfo(new String[] { "Jac"?? "Joe"?? "Rose" }?? new Strategy2());
????}
????}
???????????SQL???????????????????????
????select * from user_info where username = 'Tom' or username = 'Jim' or username = 'Anna'
????select * from user_info where username = 'Jac' or username = 'Joe' or username = 'Rose'
??????ò???????????????????????????????????????????????????μ????????????????????
???????????????????????????????????????????????????滻????????????仯????????????????????