企业项目开发--mybatis注解与xml并用(1)

勿忘初心2018-12-19 14:50

此文已由作者赵计刚授权网易云社区发布。

欢迎访问网易云社区,了解更多网易技术产品运营经验。


本章的代码建立在第四章《Java框架整合--切分配置文件》的项目代码之上,链接如下:

http://www.cnblogs.com/java-zhao/p/5118184.html

在实际开发中,我们在使用mybatis的时候,会注解与xml形式一起使用。

1、二者的使用场景

xml使用场景(3个):

  • 条件不定的查询(eg.下边代码中的getAdminByConditions方法)
  • 增加对象返回自增主键(eg.下边代码的insertAdminWithBackId方法)
  • 在一个Mapper接口中,出现多个select查询(>=3个),且每个查询都需要写相同的返回@Results内容(这一部分内容通常很多),这样的话,为了使Mapper接口比较整洁,重复代码比较少,我们会将这些select方法的具体实现写在xml文件中,因为在xml文件的顶部我们就会配置与注解@Results异曲同工的东西。(当然,这一点如果嫌配置xml麻烦,这一点可忽略)

注意:前两条是硬性的,是注解所解决不了的,而第三条只是建议。

除了以上这三条之外,其他的都使用去注解就好。

2、代码实现

基本代码不变,这只列出修改过得代码:

2.1、ssmm0-userManagement:

AdminController

 1 package com.xxx.web.admin;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.ResponseBody;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.xxx.model.userManagement.Admin;
13 import com.xxx.service.userManagement.AdminService;
14 
15 /**
16  * adminController
17  */
18 @Controller
19 @RequestMapping("/admin")
20 public class AdminController {
21     
22     @Autowired
23     private AdminService adminService;
24     
25     /**
26      * 管理员注册
27      */
28     @ResponseBody
29     @RequestMapping("/register")
30     public boolean register(@RequestParam("username") String username,
31                             @RequestParam("password") String password){
32         Admin admin = new Admin();
33         admin.setUsername(username);
34         admin.setPassword(password);
35         
36         boolean isRegisterSuccess = adminService.register(admin);
37         
38         return isRegisterSuccess;
39     }
40     
41     /**
42      * 管理员登录
43      */
44     @RequestMapping("/login")
45     public ModelAndView login(@RequestParam("username") String username,
46                               @RequestParam("password") String password){
47         Admin admin = adminService.login(username, password);
48         
49         ModelAndView modelAndView = new ModelAndView();
50         if(admin == null){
51             modelAndView.addObject("message", "用户不存在或者密码错误!请重新输入");
52             modelAndView.setViewName("error");
53         }else{
54             modelAndView.addObject("admin", admin);
55             modelAndView.setViewName("userinfo");
56         }
57         
58         return modelAndView;
59     }
60     
61     /*****************************mybatis xml方式解决的问题*******************************/
62     /**
63      * 根据username或password查找List<Admin>
64      */
65     @ResponseBody
66     @RequestMapping("/findAdmin")
67     public List<Admin> findAdmin(@RequestParam(value="username",required=false) String username,
68                                     @RequestParam(value="password",required=false) String password,
69                                     @RequestParam("start") int start,
70                                     @RequestParam("limit") int limit){
71         List<Admin> adminList = adminService.findAdmin(username, password, start, limit);
72         return adminList;
73     }
74     
75     /**
76      * 插入一个用户并返回主键
77      * 注意:get请求也会自动装配(即将前台传入的username和password传入admin)
78      */
79     @ResponseBody
80     @RequestMapping("/insert")
81     public Admin insertAdminWithBackId(Admin admin){
82         return adminService.insertAdminWithBackId(admin);
83     }
84 }

说明:在这里增加了两个方法,具体看代码与注释

注:

  • springMVC通过get方式传递的属性值username、password也能自动装配到对象admin中

2.2、ssmm0-data:

AdminService

 1 package com.xxx.service.userManagement;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.xxx.dao.userManagement.AdminDao;
 9 import com.xxx.model.userManagement.Admin;
10 
11 /**
12  * 管理员service
13  */
14 @Service
15 public class AdminService {
16     @Autowired
17     private AdminDao adminDao;
18     
19     public boolean register(Admin admin){
20         return adminDao.register(admin);
21     }
22     
23     public Admin login(String username, String password) {
24         return adminDao.login(username, password);
25     }
26     
27     /***********以下方法是为了测试mybatis中使用xml**********/
28     public List<Admin> findAdmin(String username, String password, int start, int limit){
29         return adminDao.findAdmin(username, password, start, limit);
30     }
31     
32     public Admin insertAdminWithBackId(Admin admin){
33         int record = adminDao.insertAdminWithBackId(admin);
34         if(record==1){
35             return admin;//这时的admin已经被赋予主键了
36         }
37         return null;
38     }
39 }


免费领取验证码、内容安全、短信发送、直播点播体验包及云服务器等套餐

更多网易技术、产品、运营经验分享请点击