spring,spring mvc,mybatis 常用注解

文章来源: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 

转载于:https://www.cnblogs.com/hello-tl/p/9209063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值