1 @Service用于标注业务层组件
2 @Controller用于标注控制层组件(如struts中的action)
3 @Repository用于标注数据访问组件,即DAO组件
/**
* Created by Administrator on 2019/3/19.
*/
@Service("ClassPlaceInfoService")
@Transactional(rollbackFor = Exception.class)
public class ClassPlaceInfoServiceImpl implements ClassPlaceInfoService{
@Resource
private ClassPlaceInfoMapper classPlaceInfoMapper;
@Override
public List
可以看出,在这个方法上面申明了一个@Transactional,表明这个方法将要进行事务管理,同时需要说明的是rollbackFor参数定义了在出现了什么异常时进行事务的回滚,显然这里定义的就是所有的Exception都要进行事务回滚。与之相反的一个参数是norollbackFor,这里就不多说了。对于@Transactional注解我们不仅可以在一个方法上放置,而且可以在类上进行申明。如果在类级别上使用该注解,那么类中的所有公共方法都被事务化,否则就只有使用了@Transactional注解的公共方法才被事务化。
@Override是伪代码,表示重写(当然不写也可以),不过也有好处:
1、可以当注释用,方便阅读;
2、编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错。
例如,你如果没写@Override,而你下面的方法名又写错了,这时你的编译器是可以编译通过的,
@Resource和@Autowired [汉译英 :@资源和@自动装配]
都是做bean的注入时使用
@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。
1、共同点
两者都可以写在字段和setter方法上。两者如果都写在字段上,那么就不需要再写setter方法。
2、不同点
@Autowired 只按照类型注入
@Resource 默认按照名称注入
@Autowired 的目的:
使用Spring时,通过Spring注入的Bean一般都被定义成private,并且要有getter和setter方法,显得比较繁琐,增加了代码量,使用@Autowired可以减少代码量。
标注可以放在成员变量上,也可以放在成员变量的set方法上。前者,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;后者,Spring会调用setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。
例如:
public class TestServiceImpl {
// 下面两种@Autowired只要使用一种即可
@Autowired
private UserDao userDao; // 用于字段上
@Autowired
public void setUserDao(UserDao userDao) { // 用于属性的方法上
this.userDao = userDao;
}}
package com.controller;
/**
* Created by Administrator on 2019/3/19.
*/
import com.alibaba.fastjson.JSON;
import com.model.ClassPlaceInfo;
import com.service.ClassPlaceInfoService;
import com.service.serviceImpl.ClassPlaceInfoServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping(value = "/classPlaceInfo",produces = {"application/json;charset=UTF-8"})
public class ClassPlaceInfoController {
//设置查询所有的上课地点信息
@Resource
private ClassPlaceInfoService classPlaceInfoService;
@RequestMapping(value = "/getList", method = RequestMethod.GET)
@ResponseBody
public String getList(@RequestParam Map params)
{
List> list = this.classPlaceInfoService.getList(params);
return JSON.toJSONString(list);
}
//删除上课地点信息
@RequestMapping(value = "/deletePlaceItem", method = RequestMethod.GET, params="items_id")
@ResponseBody
public String deleteItems(@RequestParam(value="items_id",required=false) String items_id)throws Exception{
Map map = classPlaceInfoService.deleteByClassPlaceId(Integer.valueOf(items_id));
return JSON.toJSONString(map);
}
//添加上课地点信息
@RequestMapping(value = "/addClassPlace", method = RequestMethod.POST)
@ResponseBody
public String addClassPlaceItem(ClassPlaceInfo classPlaceInfo)throws Exception{
Map map = new HashMap();
System.out.print(classPlaceInfo.getPlaceName());
map.put("data",classPlaceInfoService.addClassPlaceInfo(classPlaceInfo));
return JSON.toJSONString(map);
}
}
1、@Controller [汉译英 :管理者]
只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器 @Controller 的使用方法
2、@RequestMapping [汉译英 :请求映射]
作用:处理请求映射,用于类或方法上,
用于类时,表示类中的所有响应请求的方法都是以该地址作为父路径。
6个属性:
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
3、 @ResponseBody和@RequestBody
@ResponseBody
public Student getStudent(Integer id){
Student student = sstudentService.getStudent(id);
return student;
}
//前端得到的Json数据:
'{"id":"201541030102","name":"刘聪","sex":"男"}'
//前端传来的的Json数据:
'{"id":"201541030102","name":"刘聪","sex":"男"}'
@ResponseBody
public Integer setStudent(@RequestBody Student student){
Integer total = sstudentService.setStudent(stedent);
return total;
}
这个类有对应的mapper.xml文件,用于写相关的sql语句。
package com.dao;
import com.model.ClassPlaceInfo;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2019/3/19.
*/
//@Repository
//public interface ClassPlaceInfoMapper extends BaseMapper {
//}
@Repository
public interface ClassPlaceInfoMapper {
Integer deleteClassPlace(Integer id);
List> GetAllClassPlaceList();
Integer addClassPlace(ClassPlaceInfo classPlaceInfo);
}
@RequestParam 用来处理Content-Type为application/x-www-form-urlencoded(默认类型如果不指定)
GET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上
例1 http://localhost:8080/test?name=zhangsan&age=30
@RequestMapping("/test")
public String test(@RequestParam Map param){
param.forEach((key,value)->{
System.out.println("key="+key+",value="+value);
});
}
如上打印结果为:
key=name,value=zhangsan
key=age,value=30
例2 http://localhost:8080/test?name=zhangsan
@RequestMapping("/test")
public String test(@RequestParam String name){
System.out.println("name="+zhangsan);
}
如上打印结果为:
name=zhangsan
@RequestBody注解可以接收json格式的数据,并将其转换成对应的数据类型。
@RequestMapping(value="/findBookByName", method = RequestMethod.POST)
@ResponseBody
public DbBook findBookByName(@RequestBody DbBook book){
System.out.println("book: " + book.toString());
System.out.println("book name: " + book.getTitle());
String bookName = book.getTitle();
DbBook book = wxService.findBookByName(bookName);
return book;
}
(1)前台界面,这里以小程序为例
wx.request({
url: host.host + `/WxProgram/deleteBookById`,
method: 'POST',
data: {
nick: this.data.userInfo.nickName,
bookIds: bookIds
},
success: (res) => {
console.log(res);
this.getCollectionListFn();
},
fail: (err) => {
console.log(err);
}
})
(2)控制器
@RequestMapping(value="/deleteBookById",method=RequestMethod.POST)
@ResponseBody
public void deleteBookById(@RequestBody Map map){
String bookIds = map.get("bookIds");
String nick = map.get("nick");
String[] idArray = bookIds.split(",");
Integer userId = wxService.findIdByNick(nick);
for(String id : idArray){
Integer bookid = Integer.parseInt(id);
System.out.println("bookid: " + bookid);
wxService.removeBookById(bookid, userId);
}
}
关于@RequestParam 和@RequestBody可以查看链接
以及 链接
以上只是意思的理解,没有结合配置文件。