测试例子:
controlelr层
1 package org.slsale.test; 2 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import net.sf.json.JSONArray; 8 9 import org.slsale.pojo.User; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.stereotype.Controller; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 15 @Controller 16 @RequestMapping("/test") 17 public class TestController { 18 19 @Autowired 20 private TestService service; 21 22 @RequestMapping(value="/getUsers",produces = { "text/html;charset=UTF-8" }) //返回页面的数据中有中文,用produces 处理乱码 23 @ResponseBody 24 public String getUsers(){ 25 Map<String, Object> params = new HashMap<String, Object>(); 26 params.put("isStart", 1); 27 params.put("userType", "1"); 28 params.put("startNum", 0); 29 params.put("pageSize", 5); 30 List<User> users = service.getUsers(params); 31 JSONArray jsonArray = JSONArray.fromObject(users); 32 return jsonArray.toString(); 33 } 34 35 }
2.service层
1 package org.slsale.test; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.slsale.dao.testmapper.TestUserMapper; 7 import org.slsale.pojo.User; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Service; 10 11 @Service 12 public class TestService { 13 14 @Autowired 15 private TestUserMapper mapper ; 16 17 public List<User> getUsers(Map<String, Object> params){ 18 return mapper.getUserList1(params); 19 } 20 21 }
3.dao层,返回list
1 package org.slsale.dao.testmapper; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import org.slsale.pojo.User; 7 8 public interface TestUserMapper { 9 10 public List<User> getUserList1(Map<String, Object> map); 11 12 }
sql映射mapper xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 5 <mapper namespace="org.slsale.dao.testmapper.TestUserMapper"> 6 7 <!-- getUserList1 根据条件查询分页用户列表,联表查询,表名使用了别名,需要使用ResultMap进行字段映射而不再是 resultType="user"--> 8 <select id="getUserList1" resultMap="userResultMap" parameterType="Map"> 9 select u.userName ,u.loginCode ,r.roleName 10 from au_user u 11 left join au_role r on r.id=u.roleId 12 <where> 13 <if test="userName!=null"> and u.userName like CONCAT(‘%‘, #{userName},‘%‘)</if> <!-- --> 14 <if test="isStart!=null">and u.isStart=#{isStart}</if> 15 <if test="userType!=null">and u.userType=#{userType}</if> 16 </where> 17 order by createTime desc limit #{startNum},#{pageSize} 18 </select> 19 20 <resultMap id="userResultMap" type="user"> 21 <id property="id" column="u.id" /> 22 <result property="userName" column="u.userName" /> 23 <result property="loginCode" column="u.loginCode" /> 24 <result property="roleName" column="r.roleName" /> 25 </resultMap> 26 27 </mapper>
联表查询,如果表名使用了别名,接收结果集必须 使用resultmap,resultmap中的column 是sql语句中的查询字段。sql中的参数就是controller中定义的key
原文地址:https://www.cnblogs.com/enjoyjava/p/9250429.html
时间: 2024-10-14 21:41:43