码迷,mamicode.com
首页 > 编程语言 > 详细

spring,spring mvc,mybatis 常用注解

时间:2018-06-21 15:30:58      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:font   private   控制器   llb   IV   字符   resource   www   .class   

文章来源:https://www.cnblogs.com/hello-tl/p/9209063.html 

0.在spring,soring mvc, mybistis 中的常用注解有一下
  <!-- 扫描指定的包中的类上的注解,常用的注解有: -->
  <!-- @Controller 声明Action组件 -->
  <!-- @Service 声明Service组件 @Service("xxxService") -->
  <!-- @Repository 声明Dao组件 -->
  <!-- @Component 泛指组件, 当不好归类时. -->
  <!-- @RequestMapping("/menu") 请求映射 -->
  <!-- @Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") -->
  <!-- @Autowired 用于注入,(spring提供的) 默认按类型装配 -->
  <!-- @Transactional( rollbackFor={Exception.class}) 事务管理 -->
  <!-- @ResponseBody将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流 -->
  <!-- @Scope("prototype") 设定bean的作用域 -->
1. @Controller 声明Action组件
package com.web.controller;

import org.springframework.stereotype.Controller;
// Controller 声明控制器
@Controller
public class TestController {
    /**
     * 代码体
     */
}
2. @Service 声明Service组件 @Service("xxxService")
package com.web.service.impl;

import org.springframework.stereotype.Service;

// 声明service 
@Service("testService")
public class TestServiceImpl implements ITestService {

}
3. @Repository 声明Dao组件
package com.web.dao;

import org.springframework.stereotype.Repository;
// 声明Dao
@Repository
public interface ITestDao {
   /**
     * 代码体
     */
}
4. @Component 泛指组件, 当不好归类时.

5. @RequestMapping("/menu") 请求映射
package com.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
// 请求映射 @RequestMapping("test") public class TestController{
// 请求映射
@RequestMapping("index") public String getProvince(){ return "试图地址"; } }
6. @Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
package com.web.service.impl;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.web.dao.testDao; // 声明service @Service("testService") public class TestServiceImpl implements ITestService { // @Resource 注入 @Resource private ITestDao testDao; }
7. @Autowired 用于注入,(spring提供的) 默认按类型装配
package com.web.controller;

import org.springframework.stereotype.Controller;
import com.web.service.ITestService;

// Controller 声明控制器
@Controller
public class TestController {
    // Autowired 用法
    @Autowired
    private ITestService testService;
}
8. @Transactional( rollbackFor={Exception.class})
package com.web.service.impl;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.web.dao.testDao;
import org.springframework.transaction.annotation.Transactional;
// 声明service 
@Service("testService")
public class TestServiceImpl implements ITestService {
  // @Resource 注入
  @Resource private ITestDao testDao;
  @Override
  // 添加事务处理
@Transactional( rollbackFor={Exception.class})
  public int addTest(TestModel testModel){
    return testDao.addtest(testModel);
  }
}
9. @ResponseBody将内容或对象作为 HTTP 响应正文返回,并调用适合HttpMessageConverter的Adapter转换对象,写入输出流
package com.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
// 请求映射
@RequestMapping("test")
public class TestController{
    
    // 请求映射
    @RequestMapping("index")
    // 写入输出
    @ResponseBody
    public String getProvince(){
        return  "输入的内容 如 json xml 字符串 等";
    }
}
10.@Scope("prototype") 设定bean的作用域
文章来源:https://www.cnblogs.com/hello-tl/p/9209063.html 

spring,spring mvc,mybatis 常用注解

标签:font   private   控制器   llb   IV   字符   resource   www   .class   

原文地址:https://www.cnblogs.com/hello-tl/p/9209063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!