发表于: 2017-10-18 21:33:32

1 744



今天完成的事情:


1 更改之前的配置文件spirng-dao.xml:

使用MappingScannerConfigurer代替MapperFactoryBean,增加可扩展性。

 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

 <property name="basePackage" value="org.shunly.application.dao"/>

 </bean>


对昨天的spring部分的ApplicationServiceImp测试:


自己借鉴摸索以后写的Controller:

@Controller
@RequestMapping("/application")
public class ApplicationController {

   @Autowired
   private ApplicationService applicationService;

   /**
    * 根据id查询
    * @param id id
    * @param model model
    * @return get
    */
   @RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
   public @ResponseBody String id(@PathVariable int id, Model model){
       Application application = applicationService.getById(id);
       model.addAttribute("get", application);
       return  "get";
   }

   /**
    * 根据姓名查询
    * @param name 名字
    * @param model model
    * @return get
    */
   @RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
   public String name(@PathVariable String name,Model model){
       Application application = applicationService.getByName(name);
       model.addAttribute("get",application);
       return "get";
   }

   /**
    * 显示所有申请信息
    * @param model 1
    * @param httpServletRequest 1
    * @return list
    */
   @RequestMapping(value = "/list", method = RequestMethod.GET)
   public String list(Model model, HttpServletRequest httpServletRequest){
       List<Application> list = applicationService.listApplication();
       model.addAttribute("list",list);
       return "list";
   }

   /**
    * 以json的格式返回所有申请信息
    * @return
    */
   @RequestMapping(value = "/list2", method = RequestMethod.GET)
   @ResponseBody
   public Map<String, Object> list2(){
       List<Application> applications = applicationService.listApplication();
       for(Application a: applications){
           System.out.println(a);
       }
       Map<String, Object> map = new HashMap<String, Object>(2);
       map.put("list2",applications);
       return map;
   }

   /**
    * 根据id删除信息
    * @param id id
    * @param model 1
    * @return delete
    */
   @RequestMapping(value = "/id/{id}", method = RequestMethod.DELETE)
   public String delete(@PathVariable int id, Model model){
      applicationService.delete(id);
       return "delete";
   }

   /**
    * 添加
    * @param application a
    * @return add
    */
   @RequestMapping(value = "/new",method = RequestMethod.POST)
   public String add(Application application){
       applicationService.insert(application);
       return  "add";
   }
}


3

Postman测试:

①返回json的:


②正常的:

明天计划的事情: 



遇到的问题:

1  在写serviceImp时候

@Autowired

ApplicationMapper applicationMapper;

自动注入一直显示:

Could not autowire. No beans of 'ApplicationMapper' type found. less... (Ctrl+F1) 

Checks autowiring problems in a bean class.


解决方法:

@Autowired(required = false)



2  list方法返回json对象报错

No converter found for return value of type: class java.util.HashMap

添加依赖包:

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-core</artifactId>

    <version>${jackson.version}</version>

</dependency>

<dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    <version>${jackson.version}</version>

</dependency>


收获

小知识点:

1 IDEA 自动生成单元测试的插件:JunitGeneration V2.0


2 mybatis-spring中的MapperScannerConfigurer类:


MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一个类。

使用场景:需要使用多个MapperFacortyBean时,减少一一定义的工作量。

功能:自动查找类路径下的映射器并自动创建对应的MapperFactoryBean。



知识补充:MapperFactoryBean:

代替手工使用SqlSessionDaoSupport或SqlSessionTemplate编写数据访问对象(DAO)的代码,使用动态代理实现。

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">

  <property name="mapperInterface" value="org.xx.UserMapper" />

  <property name="sqlSessionFactory" ref="sqlSessionFactory" />

</bean>

org.xx.UserMapper是一个接口,我们创建一个MapperFactoryBean实例,然后注入接口和sqlSessionFactory两个属性。

之后想使用这个UserMapper接口的话,直接通过spring注入这个bean,然后就可以直接使用了,spring内部会创建一个这个接口的动态代理。


参考http://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html


3注解
好处是减少xml文件的体积、不用写很多property。。。简化了代码。不用在xml文件和java之间切换,提高了代码可读性。

不过,好像提高了一点耦合性?(修改时要在java中)



①@Repository、@Service、@Controller 和 @Componen:

Componen是最普通的一个把类注入IoC容器的注解,Repository用于数据访问层(DAO),Service用于业务层,Controller用于控制层。

默认的bean名字为类名首字母小写,可以通过类似@Service("xxxx")指定名称。


范围默认都是单例模式(singleton),可以通过加注@Scope(“XX”)改变:

prototype 原型,请求一次创建一个实例,自行销毁实例。

request web中HTTP请求的prototype

session  web中的会话

global session 基于porlet?的web应用程序中



②@Autowired 自动装配属性

通过@Autowired自动装配方式,从IoC容器中去查找到,并返回给该属性。


eg:

@Service

public class ApplicationServiceImp implements ApplicationService{

    @Autowired(required = false)

    ApplicationMapper applicationMapper;

    public int insert(Application a) {

        applicationMapper.insert(a);

        return a.getId();

    }


注意点在于对应类型的bean在IoC容器中:

 刚好为一个,装配之

 不止一个,根据名称来。

 为空,那么会抛出异常。使用required=false 处理。




返回列表 返回列表
评论

    分享到