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
spring,spring mvc,mybatis 常用注解的更多相关文章
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- spring boot的一些常用注解
spring boot的一些常用注解: 使用@SpringBootApplication注释: 许多Spring Boot开发人员喜欢他们的应用程序使用自动配置,组件扫描,并能够在其“应用程序类”上定 ...
- Solon Web 开发,十四、与Spring、Jsr330的常用注解对比
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Spring Boot整合MyBatis(非注解版)
Spring Boot整合MyBatis(非注解版),开发时采用的时IDEA,JDK1.8 直接上图: 文件夹不存在,创建一个新的路径文件夹 创建完成目录结构如下: 本人第一步习惯先把需要的包结构创建 ...
- Spring Boot 实战 —— MyBatis(注解版)使用方法
原文链接: Spring Boot 实战 -- MyBatis(注解版)使用方法 简介 MyBatis 官网 是这么介绍它自己的: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过 ...
- Mybatis 常用注解
Mybatis常用注解对应的目标和标签如表所示: 注解 目标 对应的XML标签 @CacheNamespace 类 <cache> @CacheNamespaceRef 类 <cac ...
- Spring MVC的常用注解(一)
概述 Spring从2.5版本开始引入注解,虽然版本不断变化,但是注解的特性一直被延续下来并不断进行扩展,这里就来记录一下Spring MVC中常用的注解,本文记录@Controller.@Reque ...
- Spring和SpringMVC的常用注解
Spring的部分: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component-scan base-package=" ...
- Spring Boot (9) mybatis全注解化
ORM对比图 框架对比 Spring JDBC Spring Data Jpa Mybatis 性能 性能最好 性能最差 居中 代码量 多 少 多 学习成本 低 高 居中 推荐指数 ❤❤❤ ❤❤❤❤❤ ...
随机推荐
- JSP && Servlet | 上传图片到数据库
参考博客: https://blog.csdn.net/qiyuexuelang/article/details/8861300 Servlet+Jsp实现图片或文件的上传功能 https://blo ...
- java快速排序代码
public class test1 { public static int partition(int[] array,int lo,int hi){ int key=array[lo]; whil ...
- ( 2018 Multi-University Training Contest 2)
2018 Multi-University Training Contest 2) HDU 6311 Cover HDU 6312 Game HDU 6313 Hack It HDU 6314 Mat ...
- Codeforces Round #390 (Div. 2) D
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring s ...
- Hibernate-Session使用的背后
一.Session缓存的介绍 简单说,缓存介于应用程序和数据库之间,是临时存放数据的内存区域,作用是减少对数据库的访问次数,从而提高应用程序的运行性能.Session有一个缓存,也叫hibernate ...
- 复习线程——状态和几个Thread方法
一.线程的状态 (参考文章:https://blog.csdn.net/a58220655/article/details/76695142) 状态介绍 新建(new):处于该状态的时间很短暂.已被分 ...
- HTML 5的革新——更简洁的结构
今天我们阐述HTML 5的革新之一:更简洁的结构. 新的文档类型 DOCTYPE 先来解释一下文档类型 DOCTYPE:文档类型位于HTML源文件的第一行,在HTML4的标准中,DOCTYPE在被归在 ...
- android开发学习 ------- 自定义View 圆 ,其点击事件 及 确定当前view的层级关系
我需要实现下面的效果: 参考文章:https://blog.csdn.net/halaoda/article/details/78177069 涉及的View事件分发机制 https://www. ...
- Java编程简介
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=3 JAVA由Sun Microsystems In ...
- Java开发中存在这样的代码,反而影响整体整洁和可读性
不完美的库类 不完美的库类(Incomplete Library Class) 当一个类库已经不能满足实际需要时,你就不得不改变这个库(如果这个库是只读的,那就没辙了). 问题原因 许多编程技术都建立 ...