发表于: 2017-01-17 21:47:51
1 2033
今天完成的事情:
1.完成了service层的编码,记录下过程:
(1)配置创建一个spring配置dao层对象的配置文件spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.wanghao.springmvc.service" />
</beans>
( 2 )创建EnrollService类
package com.wanghao.springmvc.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wanghao.springmvc.dao.EnrollDao;
import com.wanghao.springmvc.models.Enroll;
import com.wanghao.springmvc.service.EnrollService;
@Service
public class EnrollServiceImpl implements EnrollService{
@Autowired
private EnrollDao dao;
public EnrollDao getDao() {
return dao;
}
public void setDao(EnrollDao dao) {
this.dao = dao;
}
@Override@Transactional
public int insert(Enroll enroll)throws Exception {
return dao.insert(enroll);
}
@Override@Transactional
public int update(Enroll enroll)throws Exception {
return dao.update(enroll);
}
@Override@Transactional
public int deleteByCondition(Enroll enroll)throws Exception {
return dao.deleteByCondition(enroll);
}
@Override@Transactional
public int deleteByID(Enroll enroll)throws Exception {
return dao.deleteByID(enroll);
}
@Override@Transactional
public List<Enroll> findByCondition(Enroll enroll)throws Exception {
return dao.findByCondition(enroll);
}
@Override@Transactional
public Enroll findByID(Enroll enroll)throws Exception {
return dao.findByID(enroll);
}
@Override@Transactional
public Long findCountByCondition(Enroll enroll)throws Exception {
return dao.findCountByCondition(enroll);
}
}
3.单元测试
package com.wanghao.springmvc.service;
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.wanghao.springmvc.models.Enroll;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml","classpath:spring/spring-service.xml"})
public class EnrollServiceTest {
@Resource
EnrollService enrollService;
@Test
public void testFindByID() throws Exception {
Long id=new Long(1);
Enroll enroll=new Enroll();
enroll.setId(id);
enroll=enrollService.findByID(enroll);
System.out.println(enroll);
}
}
2.完成controller层的编码,并把web.xml加入springmvc,顺利完成按id查询,其余几项由于时间原因没能完成,明天在完善
package com.wanghao.springmvc.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wanghao.springmvc.models.Enroll;
import com.wanghao.springmvc.service.EnrollService;
@Controller
@RequestMapping("/enroll")
public class EnrollController {
@Autowired
private EnrollService enrollService;
@RequestMapping(value = "findbyid/{id}",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> list(@PathVariable("id") Long id) throws Exception
{
Enroll enroll=new Enroll();
enroll.setId(id);
enroll=enrollService.findByID(enroll);
Map<String, Object> map = new HashMap<String, Object>();
map.put("enroll", enroll);
return map;
}
}
明天计划的事情:
1.完成增删查改的restful接口
2.学习PostMan进行接口
遇到的问题:
今天完成的内容较少,没看懂关于请求和响应对象的使用,明天继续研究
收获:
基本了解了springmvc实现restful的流程
初步了解了json的语法
评论