顺序从上到下:
Controller类名要加@RestController,引用service类定义加@Resource,函数@RequestMapping(value = "/list", method = RequestMethod.GET)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39@RestController @RequestMapping(value = "/rest/v1/user/info") public class UserInfoController { private static final Logger log = LogManager.getLogger(UserInfoController.class); @Resource private IUserInfoService userInfoService; @RequestMapping(value = "/id/{id}", method = RequestMethod.GET) public UserInfo getUserById(@PathVariable int id) { return userInfoService.findById(id); } @RequestMapping(value = "/list", method = RequestMethod.GET) //@LoginToken public List<UserInfo> getAll() { return userInfoService.getAll(); } @RequestMapping(value = "/save", method = RequestMethod.POST)//Add User //@LoginToken public UserInfo save(@RequestBody String data) { ObjectMapper mapper = new ObjectMapper(); UserInfo ui; try { ui = mapper.readValue(data, UserInfo.class); //ui.setRegTime((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date())); //ui.setVisitTime((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date())); //ui.setDeleted(0); return userInfoService.save(ui); } catch (JsonParseException e) { log.error(e.toString()); return null; } catch (JsonMappingException e) { log.error(e.toString()); return null; } catch (IOException e) { log.error(e.toString()); return null; } } }
Impl类名要加@Service(什么时候加@Transactional?),定义implements类,引用repo类定义要加@Autowired
复制代码
1
2
3
4
5
6
7
8
9@Service public class UserNoteServiceImpl implements IUserNoteService{ @Autowired private UserNoteRepo userNoteRepo; @Override public List<UserNote> findBySysId(int sysId) { return userNoteRepo.findAllByUserId(sysId); } }
Service和repo层是interface,定义时不用加注解
Entity类名要加@Entity和对应的表@Table(name="TEST")
复制代码
1
2
3
4
5@Entity @Table(name="DTT_USERINFO") @JsonIgnoreProperties(value = { "deleted" }) public class UserInfo implements Serializable{ }
定义变量要加 @Column(name="EMP_NAME", length=50, nullable=true), 主键用@Id,表示自增加
复制代码
1
2
3@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id;
最后
以上就是安静石头最近收集整理的关于Java Spring注解与接口开发 之二 —— 注解要求的全部内容,更多相关Java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复