我想知道我是否正确地注释了这些类,因为我对注释是新手:
Country.java
@Component
public class Country {
private int countryId;
private String countryName;
private String countryCode;
/**
* No args constructor
*/
public Country() {
}
/**
* @param countryId
* @param countryName
* @param countryCode
*/
public Country(int countryId, String countryName, String countryCode) {
this.countryId = countryId;
this.countryName = countryName;
this.countryCode = countryCode;
}
//getters and setters
}CountryDAO.java
@Repository
public interface CountryDAO {
public List<Country> getCountryList();
public void saveCountry(Country country);
public void updateCountry(Country country);
}JdbcCountryDAO.java
@Component
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO{
private final Logger logger = Logger.getLogger(getClass());
@Autowired
public List<Country> getCountryList() {
int countryId = 6;
String countryCode = "AI";
logger.debug("In getCountryList()");
String sql = "SELECT * FROM TBLCOUNTRY WHERE countryId = ? AND countryCode = ?";
logger.debug("Executing getCountryList String "+sql);
Object[] parameters = new Object[] {countryId, countryCode};
logger.info(sql);
//List<Country> countryList = getJdbcTemplate().query(sql,new CountryMapper());
List<Country> countryList = getJdbcTemplate().query(sql, parameters,new CountryMapper());
return countryList;
}CountryManagerIFace.java
@Repository
public interface CountryManagerIFace extends Serializable{
public void saveCountry(Country country);
public List<Country> getCountries();
}CountryManager.java
@Component
public class CountryManager implements CountryManagerIFace{
@Autowired
private CountryDAO countryDao;
public void saveCountry(Country country) {
countryDao.saveCountry(country);
}
public List<Country> getCountries() {
return countryDao.getCountryList();
}
public void setCountryDao(CountryDAO countryDao){
this.countryDao = countryDao;
}
}发布于 2012-09-30 14:48:39
这个答案应该能澄清一点:What's the difference between @Component, @Repository & @Service annotations in Spring?
其他你应该知道的事情:
您的代码应该是这样的:
// No annotation
public class Country {和
// No annotation
public interface CountryDAO {和
@Repository
public class JdbcCountryDAO extends JdbcDaoSupport implements CountryDAO {和
// No annotation
public interface CountryManagerIFace extends Serializable{和
@Service
public class CountryManager implements CountryManagerIFace{
@Autowired
private CountryDAO countryDao;注意:我很少在代码中使用@Component作为@Controller (表示层)、@Service (服务层)和@Repository (dao层)来满足我的主要Spring需求。
https://stackoverflow.com/questions/12658136
复制相似问题