项目中 MyBatis 的使用
1.xml 文件
1-1.xml文件格式
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="admin.adminMapper"> </mapper>
1-2.select 语句
<select id="getStudent" parameterType="pd" resultType="pd"> SELECT * from student <where> <if test="s_id!=null and s_id!=''"> stuid=#{s_id} </if> <if test="s_name!=null and s_name!=''"> and stuname=#{s_name} </if> </where> </select>
1-3.其它语句 (以 delete 为例)
<delete parameterType="pd" id="deleteTeacherById"> DELETE FROM teacher WHERE t_id=#{t_id} </delete>
1-4.模糊查询
name LIKE "%"#{name}"%"
2.传入参数类型
package com.ssm.sample.util; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; import javax.servlet.http.HttpServletRequest; @SuppressWarnings("rawtypes") public class PageData extends HashMap implements Map { private static final long serialVersionUID = 1L; Map<Object, Object> map = null; HttpServletRequest request; public static void main(String[] args) { PageData pageData = new PageData(); List<PageData> list = new ArrayList<>(); PageData a=new PageData(); a.put("key", "asdas"); list.add(a); pageData.put("list", list); pageData.put("222", "23232"); System.err.println(pageData); } public PageData(HttpServletRequest request) { this.request = request; Map properties = request.getParameterMap(); Map<Object, Object> returnMap = new HashMap<Object, Object>(); Iterator entries = properties.entrySet().iterator(); Map.Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Map.Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if (null == valueObj) { value = ""; } else if (valueObj instanceof String[]) { value = ""; String[] values = (String[]) valueObj; for (int i = 0; i < values.length; i++) { value += values[i] + ","; } value = value.substring(0, value.length() - 1); } else { value = valueObj.toString(); } returnMap.put(name, value); } map = returnMap; } public PageData() { map = new HashMap<Object, Object>(); } public Object get(Object key) { Object obj = null; if (map.get(key) instanceof Object[]) { Object[] arr = (Object[]) map.get(key); obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]); } else { obj = map.get(key); } return obj; } /** * 以安全转换方式获取键值,如果找不到键则返回的值为空 * * @param key * @return */ public String getString(Object key) { Object obj = get(key); return obj == null ? "" : obj.toString(); } /** * 以安全转换方式获取键值,如果找不到键则返回的值为null */ public String getStringNull(Object key) { Object obj = get(key); return obj == null ? null : obj.toString(); } /** * 添加键值对 */ public Object put(Object key, Object value) { return map.put(key, value); } /** * 移除键 */ public Object remove(Object key) { return map.remove(key); } /** * 清空 */ public void clear() { map.clear(); } /** * 是否包含键 */ public boolean containsKey(Object key) { return map.containsKey(key); } public Map<Object,Object> getMap(){ return map; } /** * 是否包含值 */ public boolean containsValue(Object value) { return map.containsValue(value); } public Set entrySet() { return map.entrySet(); } public boolean isEmpty() { return map.isEmpty(); } public Set<Object> keySet() { return map.keySet(); } @SuppressWarnings("unchecked") public void putAll(Map t) { map.putAll(t); } public int size() { return map.size(); } public Collection<Object> values() { return map.values(); } /** * 适用jdk8的方法 */ @SuppressWarnings("unchecked") @Override public void forEach(BiConsumer action) { this.map.forEach(action); } }
3.Service层
1.baseService
package com.ssm.sample.service.base; import java.util.Properties; import javax.annotation.Resource; import org.mybatis.spring.SqlSessionTemplate; import com.ssm.sample.util.Logger; public class BaseService { protected Logger logger = Logger.getLogger(this.getClass()); @Resource(name="sqlSessionTemplate") protected SqlSessionTemplate sqlSessionTemplate; @Resource(name = "globalConf") protected Properties globalConf; }
2.UserService
package com.ssm.sample.service.user; import java.util.List; import org.springframework.stereotype.Service; import com.ssm.sample.service.base.BaseService; import com.ssm.sample.util.PageData; @Service("UserService") public class UserService extends BaseService { /* * 学生 */ public List<PageData> getStudent(PageData pd) { return this.sqlSessionTemplate.selectList("user.userMapper.getStudent", pd); } }
4.Facade层
1.baseFacade (空的)
2.UserFacade
package com.ssm.sample.facade.user; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ssm.sample.facade.base.BaseFacade; import com.ssm.sample.service.user.UserService; import com.ssm.sample.util.PageData; @Service("UserFacade") public class UserFacade extends BaseFacade { @Autowired private UserService userservice; /* * 获得学生 */ public List<PageData> getStudent(PageData pd) { List<PageData> list = this.userservice.getStudent(pd); return list; } }
5.Controller层
/* * 学生登录 */ @ResponseBody @RequestMapping({ "/s_login" }) public Object s_Login() { PageData pd = this.getPageData(); List<PageData> student = new ArrayList<>(); Map<String, String> map = new HashMap<String, String>(); try { student = this.userFacade.getStudent(pd); if (student.size() != 0) { map.put("check", "true"); } else { map.put("check", "false"); } } catch (Exception e) { e.printStackTrace(); } return map; }