转载请注明出处:http://blog.youkuaiyun.com/qq_20011175/article/details/77623347
思考第一个问题:为什么模拟数据库操作时,要使用Map而不是List
例如:findById操作,如果是List,只能遍历 如果是Map<key,value>,
根据key读取value,效率高
下面利用了SpringMVC的迷你框架
首先模拟数据库:(利用Map集合---Dao层)
import java.util.HashMap;
import java.util.Map;
public class DB {
public static Map<Integer,Emp> empTable;
//这里声明一个静态块,就是在类加载的时候,已经存入,优先于Main()方法,所以
static{
empTable = new HashMap<Integer,Emp>();
//初始化数据
Emp e1 = new Emp(1001,"zhao",4000);
Emp e2 = new Emp(1002,"qian",3000);
Emp e3 = new Emp(1003,"sun",5000);
Emp e4 = new Emp(1004,"li",8000);
//存入数据
empTable.put(e1.getEmpno(), e1);
empTable.put(e2.getEmpno(), e2);
empTable.put(e3.getEmpno(), e3);
empTable.put(e4.getEmpno(), e4);
}
//测试,打印输入
public static void main(String[] args) {
//声明一个Map集合对象,存入DB.empTable里的数据
Map<Integer,Emp> emps = DB.empTable;
//这里遍历方式是,获取Map的键,然后利用增强for的方式 打印输入。
for(Map.Entry<Integer,Emp> entry:emps.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
业务层:(Service层)
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Service;
import com.company.dao.DB;
import com.company.dao.Emp;
@Service
//自动将EmpService取名为empService注入到IOC容器中
public class EmpService {
/**
* 新增数据
* @param emp
* @return
*/
public String save(Emp emp) {
DB.empTable.put(emp.getEmpno(), emp);
System.out.println("save " + emp + " success!");
return "success";
}
/**
* 删除数据
* @param emp
* @return
*/
public String delete(Emp emp) {
DB.empTable.remove(emp.getEmpno());
System.out.println("delete " + emp.getEmpno() + " success!");
return "success";
}
/**
* 更新数据
* @param emp
* @return
*/
public String update(Emp emp) {
DB.empTable.put(emp.getEmpno(), emp);
System.out.println("update " + emp + " success!");
return "success";
}
/**
* 检索所有数据---方式1:放回键
* @param emp
* @return
*/
//set 转 list 集合
public List<Emp> findAll() {
//定义一个list集合
List<Emp> emps = new ArrayList<Emp>();
//获取Map集合的键,放入Set集合里
Set<Integer> keys = DB.empTable.keySet();
//遍历set集合,
for (Integer key : keys) {
//并转存到list集合中
emps.add(DB.empTable.get(key));
}
//数据放回
return emps;
}
/**
* 检索所有数据-----方式2:放回值
* @param emp
* @return
*/
public List<Emp> findAll2() {
//同样先声明一个List集合对象emps
List<Emp> emps = new ArrayList<Emp>();
//同理:遍历,根据Map键
for (Map.Entry<Integer, Emp> entry : DB.empTable.entrySet()) {
//将键值存入list对象,放回数据
emps.add(entry.getValue());
}
return emps;
}
/**
* 检索所有数据-----方式2:放回值无遍历
* @param emp
* @return
*/
public List<Emp> findAll3() {
return new ArrayList<Emp>(DB.empTable.values());
}
/**
* 精确检索数据
* @param emp
* @return
*/
public Emp findById(int id) {
return DB.empTable.get(id);
}
/**
* 模糊检索相关数据
* @param emp
* @return
*/
public List<Emp> findByName(String name) {
//同理
List<Emp> emps = new ArrayList<Emp>();
//同理
for (Map.Entry<Integer, Emp> entry : DB.empTable.entrySet()) {
//判断,根据键的值获取数据,一一添加,判断是否还有数据。(-1代表无数据)
if (entry.getValue().getEname().indexOf(name) != -1) {
//添加到list集合对象中
emps.add(entry.getValue());
}
}
return emps;
}
}
控制层---Action
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.company.dao.Emp;
import com.company.service.EmpService;
@Controller
@RequestMapping("empunit")
public class EmpAction {
@Autowired
private EmpService empService;
@RequestMapping("showsave")
public String showSave(){
return "save";
}
@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(@ModelAttribute Emp emp) {
System.out.println("save " + emp);
return empService.save(emp);
}
@RequestMapping(value = "delete")
public String delete(@ModelAttribute Emp emp) {
System.out.println("delete " + emp);
return empService.delete(emp);
}
@RequestMapping(value = "update")
public String update(@ModelAttribute Emp emp) {
System.out.println("update " + emp);
return empService.update(emp);
}
@RequestMapping(value = "findall")
public String findAll(Map<String, Object> map) {
List<Emp> emps = empService.findAll();
map.put("emps", emps);
return "emps";
}
@RequestMapping(value = "findbyid")
public String findById(@RequestParam("empno") int id, Map<String, Object> map) {
List<Emp> emps = new ArrayList<Emp>();
emps.add(empService.findById(id));
map.put("emps", emps);
return "emps";
}
@RequestMapping(value = "findbyname")
public String findByName(@RequestParam("ename") String name, Map<String, Object> map) {
map.put("emps", empService.findByName(name));
return "emps";
}
}