控制层的编写
一、代码实现
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
package com.imooc.action; import java.sql.SQLException; import java.util.List; import java.util.Map; import com.imooc.dao.GoddnessDao; import com.imooc.model.Goddness; public class GoddessAction { public void add(Goddness goddness) throws Exception { GoddnessDao dao = new GoddnessDao(); dao.addGoddess(goddness); } public void del(Integer id) throws Exception { GoddnessDao dao = new GoddnessDao(); dao.delGoddess(id); } public Goddness get(Integer id) throws SQLException //为什么public class? { GoddnessDao dao = new GoddnessDao(); return dao.get(id); } public void update(Goddness goddness) throws Exception { GoddnessDao dao = new GoddnessDao(); dao.updateGoddess(goddness); } public List<Goddness>query() throws Exception { GoddnessDao dao = new GoddnessDao(); return dao.query(); } public List<Goddness> query(List<Map<String, Object>> params) throws Exception { GoddnessDao dao = new GoddnessDao(); return dao.query(params); } } /*public static void main(String[] args) throws Exception { // TODO Auto-generated method stub GoddnessDao g = new GoddnessDao(); //List<Goddness> result = g.query("xie","503822883","w"); List<Map<String, Object>> params = new ArrayList<Map<String,Object>>(); Map<String, Object>param = new HashMap<String,Object>(); param.put("name", "user_name"); param.put("rela", "like"); param.put("value", "'%xie%'"); params.add(param); param = new HashMap<String,Object>(); param.put("name", "mobile"); param.put("rela", "like"); param.put("value", "'%5038%'"); params.add(param); List<Goddness> result = g.query(params); for(int i = 0;i < result.size(); i++) { System.out.println(result.get(i).toString()); } /*List<Goddness> gs = g.query(); for(Goddness goddness : gs) { System.out.println(goddness.getUser_name()+","+goddness.getAge()); } Goddness g1 = new Goddness(); g1.setUser_name("test"); g1.setAge(19); g1.setSex(1); g1.setBirthday(new Date()); g1.setEmail("503822883@qq.com"); g1.setMobile("503822883"); g1.setCreate_user("admin"); g1.setUpdate_user("admin"); g1.setIsdel(1); //g1.setId(2); //Goddness g2 = g.get(2); //System.out.println(g2.toString()); //g.delGoddess(1); //g.updateGoddess(g1); //g.addGoddess(g1); } }*/ |
写一个actiontest类来测试控制层的功能:
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 40 41 42 43 44 45 46 47 48 |
package com.imooc.test; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.imooc.action.GoddessAction; import com.imooc.model.Goddness; public class testaction { public static void main(String[] args) throws Exception { GoddessAction action = new GoddessAction(); Goddness g = new Goddness(); g.setUser_name("test"); g.setSex(1); g.setAge(15); g.setBirthday(new Date()); g.setEmail("503822883@qq.com"); g.setMobile("missfortune"); g.setIsdel(0); g.setId(4); //action.add(g); //action.update(g); action.del(4); List<Map<String, Object>>params = new ArrayList<Map<String, Object>>(); Map<String, Object>map = new HashMap<String, Object>(); map.put("name", "user_name"); map.put("rela", "="); map.put("value", "'test'"); params.add(map); List<Goddness>result = action.query(params); for (int i = 0; i < result.size(); i++) { System.out.println(result.get(i).getId()+ ":"+result.get(i).getUser_name()); } } } |
比较简单的实现。