发表于: 2017-05-30 22:59:56
2 1089
今天完成的事情:
写CompanyServiceImpl 类的接口
//company 的插入
public Long insert(Company company) throws ServiceException, ServiceDaoException {
log.info(" insert data : " + company);
if (company == null) {
return null;
}
long currentTimeMillis = System.currentTimeMillis();
company.setCreateAt(currentTimeMillis);
company.setUpdateAt(currentTimeMillis);
Long result = null;
try {
result = (Long) dao.save(company);
} catch (DaoException e) {
log.error("insert wrong : " + company);
log.error(e);
e.printStackTrace();
throw new ServiceDaoException(e);
}
log.info(" insert data success : " + result);
return result;
}
//CompanyList的插入
public List<Company> insertList(List<Company> companyList) throws ServiceException, ServiceDaoException {
log.info(" insert lists : " + (companyList == null ? "null" : companyList.size()));
List<Company> resultList = null;
if (CollectionUtils.isEmpty(companyList)) {
return new ArrayList<Company>();
}
long currentTimeMillis = System.currentTimeMillis();
for (Company company : companyList) {
company.setCreateAt(currentTimeMillis);
company.setUpdateAt(currentTimeMillis);
}
try {
resultList = (List<Company>) dao.batchSave(companyList);
} catch (Exception e) {
log.error(" insert list wrong : " + companyList);
log.error(e);
e.printStackTrace();
throw new ServiceDaoException(e);
}
return resultList;
}
写代码注意事项:
1.入参的数据要打印出来
2.判断入参是否为null
3.插入之后要更新时间戳
4.捕获异常
5.打印插入的数据
明天计划的事情:
写职业的接口
遇到的问题:
暂无
收获:
Spring:
//初始化容器
ApplicationContext context = new ClassPathXmlApplicationContext ("application-context.xml")
//获取对象
ScrewDriver screwDriver = context.getBean("screwDriver", ScrewDriver.class)
//使用对象里的方法
screwDriver.usr();
Bean的作用域:
singleton:Spring默认的单例模式。
<bean id="screwDriver" class= "com.*********" scope="singleton"></bean>
prototype:每次引用创建一个实例。
<bean id="screwDriver" class= "com.*********" scope="prototype"></bean>
Web应用中的使用:request scope、session scope、application scope、global session
Bean的生命周期回调:
创建:申请资源
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
<bean id="screwDriver" class= "com.*********" init-method="init"></bean>
public class ScrewDriver{
public void init(){
system.out.println("........");
}
}
通过init-method 可以设置Spring初始化后,调用方法,定制自己想要的动作。
销毁:释放资源
public interface DisposableBean {
void destroy() throws Exception;
}
<bean id="screwDriver" class= "com.*********" destroy-method="cleanup"></bean>
public class ScrewDriver{
public void cleanup(){
system.out.println("........");
}
}
依赖注入:
构造函数(强依赖)
setter方法(可选依赖)
评论