???Spring???е??????????
???????????? ???????[ 2013/6/5 11:10:53 ] ????????
?????????????
????????????????????????? Spring ????ó??????? MVC ???????????????????????????????? AccountService????????????????????????????????? AccountDao ????????????? Spring Jdbc Template ?????????????????AccountService ?? AccountDao ????????????????????????? Spring ??????????й?????
??????????????? AccountService ????в?????????? Spring ??????????????????????????
?????嵥 1. Account.Java
????????????????????????? getter ?? setter ??????
package domain;
public class Account {
public static final String SEX_MALE = "male";
public static final String SEX_FEMALE = "female";
private int id;
private String name;
private int age;
private String sex;
public String toString() {
return String.format("Account[id=%d??name=%s??age:%d??sex:%s]"??id??name??age??sex);
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static Account getAccount(int id??String name??int age??String sex) {
Account acct = new Account();
acct.setId(id);
acct.setName(name);
acct.setAge(age);
acct.setSex(sex);
return acct;
}
}
???????????? Account ??????? toString() ?????????????? getAccount ??????getAccount ????????????? Account ???????
?????嵥 2. AccountDao.Java
??????? DAO ????????????????????? Spring Jdbc Template ??????
package DAO;
import Java.sql.ResultSet;
import Java.sql.SQLException;
import Java.util.HashMap;
import Java.util.List;
import Java.util.Map;
import org.Springframework.context.ApplicationContext;
import org.Springframework.context.support.ClassPathXmlApplicationContext;
import org.Springframework.jdbc.core.RowMapper;
import org.Springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.Springframework.jdbc.core.simple.ParameterizedRowMapper;
import domain.Account;
public class AccountDao extends NamedParameterJdbcDaoSupport {
public void saveAccount(Account account) {
String sql = "insert into tbl_account(id??name??age??sex) " +
"values(:id??:name??:age??:sex)";
Map paramMap = new HashMap();
paramMap.put("id"?? account.getId());
paramMap.put("name"?? account.getName());
paramMap.put("age"?? account.getAge());
paramMap.put("sex"??account.getSex());
getNamedParameterJdbcTemplate().update(sql?? paramMap);
}
public Account getAccountById(int id) {
String sql = "select id??name??age??sex from tbl_account where id=:id";
Map paramMap = new HashMap();
paramMap.put("id"?? id);
List<Account> matches = getNamedParameterJdbcTemplate().query(sql??
paramMap??new ParameterizedRowMapper<Account>() {
@Override
public Account mapRow(ResultSet rs?? int rowNum)
throws SQLException {
Account a = new Account();
a.setId(rs.getInt(1));
a.setName(rs.getString(2));
a.setAge(rs.getInt(3));
a.setSex(rs.getString(4));
return a;
}
});
return matches.size()>0?matches.get(0):null;
}
}
????AccountDao ?????????????????????????????
?????? saveAccount????????????????????
?????? getAccountById????????? Id ??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11