controller
1 package cn.yzq.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 /** 7 * @author Administrator 8 *��ת 9 */ 10 @Controller 11 public class JumpController { 12 @RequestMapping("tomain.do") 13 public String jumpShow() { 14 15 return "show"; 16 } 17 18 }
1 package cn.yzq.controller; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 10 11 12 import cn.yzq.bean.Login; 13 import cn.yzq.bean.ReturnModel; 14 import cn.yzq.service.LoginService; 15 16 @RestController 17 @RequestMapping("/login") 18 public class Mycontroller { 19 @Autowired 20 private LoginService loginServiceImpl; 21 22 @RequestMapping("empLogin.do") 23 public ReturnModel EditEmp(Login log) { 24 return loginServiceImpl.login(log); 25 } 26 27 @RequestMapping("getPermission.do") 28 public List<Map<String,Object>> getPermission(){ 29 return loginServiceImpl.getPermission(); 30 } 31 @RequestMapping("getChildrenPermission.do") 32 public List<Map<String,Object>> getChildrenPermission(int pid){ 33 return loginServiceImpl.getChildrenPermission(pid); 34 } 35 36 37 }
service
1 package cn.yzq.service.impl; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import cn.yzq.bean.Login; 10 import cn.yzq.bean.ReturnModel; 11 import cn.yzq.dao.LoginDao; 12 import cn.yzq.service.LoginService; 13 import cn.yzq.utils.EmpUtil; 14 @Service 15 public class LoginServiceImpl implements LoginService { 16 @Autowired 17 private LoginDao loginDaoImpl; 18 @Override 19 public ReturnModel login(Login log) { 20 Integer id = loginDaoImpl.login(log); 21 if (id==null) { 22 return new ReturnModel(400, false, "登录账号不存在", null); 23 } 24 Map empOne = loginDaoImpl.getLoginById(id); 25 //当员工的状态栏为空时,说明员工信息已经别注销,请联系管理员 26 if ((int)empOne.get("state")==0) { 27 return new ReturnModel(300, false, "用户已经被注销,请联系管理员", null); 28 } 29 EmpUtil.setEmp(empOne); 30 return new ReturnModel(200, true, "登录成功哦", null); 31 } 32 @Override 33 public List<Map<String, Object>> getPermission() { 34 // TODO Auto-generated method stub 35 Map emp = EmpUtil.getEmp(); 36 return loginDaoImpl.getPermission(emp.get("roleID")); 37 } 38 @Override 39 public List<Map<String, Object>> getChildrenPermission(int pid) { 40 // TODO Auto-generated method stub 41 Map emp = EmpUtil.getEmp(); 42 return loginDaoImpl.getChildrenPermission(emp.get("roleID"),pid); 43 } 44 45 }
dao
1 package cn.yzq.dao.impl; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Repository; 8 9 import cn.yzq.bean.Login; 10 import cn.yzq.dao.LoginDao; 11 import cn.yzq.utils.BaseDao; 12 13 @Repository 14 public class LoginDaoImpl implements LoginDao { 15 @Autowired 16 private BaseDao baseDao; 17 18 @Override 19 public Integer login(Login log) { 20 String sql = "select id from login where username=? and password=?"; 21 List<Map<String, Object>> loginList = baseDao.executeQuery(sql, log.getUsername(), log.getPassword()); 22 if (loginList.size() > 0) { 23 return (Integer) loginList.get(0).get("id"); 24 } 25 return null; 26 } 27 28 @Override 29 public Map getLoginById(Integer id) { 30 String sql = "select * from employee where loginId=?"; 31 List<Map<String, Object>> empList = baseDao.executeQuery(sql, id); 32 return empList.get(0); 33 } 34 35 @Override 36 public List<Map<String, Object>> getPermission(Object roleID) { 37 String sql="select DISTINCT p2.id,p2.name title from role_per rp LEFT JOIN permission p1 on rp.pid = p1.id LEFT JOIN permission p2 on p1.parentid = p2.id where rp.rid = ?"; 38 List<Map<String, Object>> executeQuery = baseDao.executeQuery(sql, roleID); 39 return baseDao.executeQuery(sql,roleID); 40 } 41 42 @Override 43 public List<Map<String, Object>> getChildrenPermission(Object roleID, int pid) { 44 String sql="select p.id,p.name text,p.url tsrc from role_per rp LEFT JOIN permission p on rp.pid = p.id where rp.rid = ? and p.parentid = ?"; 45 return baseDao.executeQuery(sql,roleID,pid); 46 } 47 48 }
utils中包含BaseDao.java和EmpUtil.java
BaseDao.java
1 package cn.yzq.utils; 2 3 import java.sql.Connection; 4 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.ResultSetMetaData; 8 import java.sql.SQLException; 9 import java.util.ArrayList; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 17 import javax.sql.DataSource; 18 19 import org.springframework.beans.factory.annotation.Autowired; 20 import org.springframework.stereotype.Repository; 21 22 23 /** 24 * @author 陈建 25 * @描述:dao基础类 26 * 2018-10-25下午2:54:57 27 */ 28 @Repository 29 public class BaseDao { 30 private final static Logger logger = LoggerFactory.getLogger(BaseDao.class); 31 @Autowired 32 private DataSource dataSource; 33 //四大金刚 34 /*static String driver="com.mysql.jdbc.Driver"; 35 static String url="jdbc:mysql://localhost:3306/aaa?useSSL=false"; 36 static String user="root"; 37 static String password="root";*/ 38 //加载驱动 39 /*static{ 40 41 try { 42 Class.forName(driver); 43 } catch (ClassNotFoundException e) { 44 e.printStackTrace(); 45 } 46 }*/ 47 //获取数据库连接 48 /* public static Connection getConnection() { 49 try { 50 return DriverManager.getConnection(url, user, password); 51 } catch (SQLException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 return null; 56 57 }*/ 58 public Connection getConnection() { 59 try { 60 return dataSource.getConnection(); 61 } catch (SQLException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 return null; 66 67 } 68 //关闭数据库连接 69 /*public static void closeConnection(Connection conn ,PreparedStatement ps ,ResultSet rs) { 70 if(rs!=null){ 71 try { 72 rs.close(); 73 } catch (SQLException e) { 74 e.printStackTrace(); 75 } 76 } 77 if(ps!=null){ 78 try { 79 ps.close(); 80 } catch (SQLException e) { 81 e.printStackTrace(); 82 } 83 } 84 if(conn!=null){ 85 try { 86 conn.close(); 87 } catch (SQLException e) { 88 e.printStackTrace(); 89 } 90 } 91 92 }*/ 93 //查询通用方法 94 public List<Map<String,Object>> executeQuery(String sql,Object...params) { 95 List<Map<String,Object>> objList= new ArrayList<Map<String,Object>>(); 96 Connection conn = getConnection(); 97 PreparedStatement ps=null; 98 ResultSet rs=null; 99 try { 100 ps = conn.prepareStatement(sql); 101 logger.trace("trace级别的日志:"+sql); 102 logger.debug("debug级别的日志:"+sql); 103 logger.info("info级别的日志:"+sql); 104 logger.warn("warn级别的日志:"+sql); 105 logger.error("error级别的日志:"+sql); 106 ps = setParams(ps, params); 107 //执行sql 108 rs = ps.executeQuery(); 109 //获取表元数据信息 110 ResultSetMetaData metaData = rs.getMetaData(); 111 //解析结果集 112 while(rs.next()){ 113 Map<String,Object> row= new HashMap<String, Object>(); 114 //metaData.getColumnCount()获取列数 115 for (int i = 0; i < metaData.getColumnCount(); i++) { 116 row.put(metaData.getColumnLabel(i+1), rs.getObject(i+1)); 117 } 118 objList.add(row); 119 } 120 } catch (SQLException e) { 121 e.printStackTrace(); 122 }finally { 123 try { 124 conn.close(); 125 } catch (SQLException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } 129 } 130 return objList; 131 132 } 133 //增删改通用方法 134 public boolean executeUpdate(String sql,Object...params) { 135 Connection conn = getConnection(); 136 PreparedStatement ps=null; 137 try { 138 ps = conn.prepareStatement(sql); 139 140 ps = setParams(ps, params); 141 //执行sql 142 int ret = ps.executeUpdate(); 143 if(ret>0){ 144 logger.debug("操作成功!"); 145 return true; 146 147 }else 148 { 149 logger.debug("操作失败!"); 150 return false; 151 } 152 } catch (SQLException e) { 153 e.printStackTrace(); 154 }finally { 155 try { 156 conn.close(); 157 } catch (SQLException e) { 158 // TODO Auto-generated catch block 159 e.printStackTrace(); 160 } 161 } 162 return false; 163 164 } 165 //设值通用方法 166 public PreparedStatement setParams(PreparedStatement ps,Object...params) { 167 if(params!=null){ 168 for (int i = 0; i < params.length; i++) { 169 try { 170 ps.setObject(i+1, params[i]); 171 } catch (SQLException e) { 172 e.printStackTrace(); 173 } 174 175 } 176 } 177 178 return ps; 179 180 } 181 182 183 }
EmpUtil.java
1 package cn.yzq.utils; 2 3 import java.util.Map; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpSession; 7 8 import org.springframework.web.context.request.RequestContextHolder; 9 import org.springframework.web.context.request.ServletRequestAttributes; 10 11 12 public final class EmpUtil { 13 14 private static HttpSession getSession() { 15 HttpSession session = null; 16 try { 17 session = getRequest().getSession(); 18 } catch (Exception e) { 19 e.printStackTrace(); 20 } 21 return session; 22 } 23 24 /**RequestContextHolder.getRequestAttributes(); 鑾峰彇璇锋眰涓墍鏈夌殑璇锋眰鏁版嵁 request 25 * 閰嶇疆涓�涓洃鍚櫒 26 * @return 27 */ 28 public static HttpServletRequest getRequest() { 29 ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 30 return attrs.getRequest(); 31 } 32 33 /** 34 * 35 * @param session 36 * @return user shiro 37 */ 38 public static Map getEmp() { 39 return (Map) getSession().getAttribute("EMP"); 40 } 41 42 /*** 43 * 44 * @param emp 45 */ 46 public static void setEmp(Map emp) { 47 getSession().setAttribute("EMP", emp); 48 } 49 50 public static void removeEmp() { 51 HttpSession session = getSession(); 52 session.invalidate(); 53 } 54 55 }