Aggregate query and fuzzy query

Keywords: Session

Aggregate query

The code in the test class is:
Using the map collection

//Aggregate query
    @Test
    public void test4(){
        UsersDao dao=new UsersDaoImpl();
        Map m=dao.jisuan();
        System.out.println(m.get("MAX"));
    }

Output the result of the query. get("")

The code in the implementation class is:

//Aggregate query
    public Map jisuan(){
        SqlSession session=su.getsession();
        Map m= (Map) session.selectOne("jisuan");
        return m;
    }

Select one query result is one

The code in the mapper file is:

<select id="jisuan" resultType="Map">
        SELECT MAX(studentno) MAX,MIN(studentno) MIN,AVG(studentno) AVG FROM students;
    </select>

The result set returned is map,
The resultType property determines the return type,
The result of aggregate query is easy to get by de aliasing

Fuzzy query

Three ways to store data

  1. array
  2. list set
  3. map set

Using arrays

Code in test class

 //Querying users with arrays
    @Test
     public void test7() {
        sessionUtil s=new sessionUtil();
        SqlSession session=s.getsession();
        UsersDao dao=session.getMapper(UsersDao.class);
        int[] ids = {1, 2, 3, 4};
        List<Users> users = dao.findarray(ids);
        System.out.println(users);
    }

How to omit code in interface implementation class
Getmapper (class name. class)

The code of the file in mapper is:

<select id="findarray"  resultType="Users">
        SELECT * FROM students WHERE studentno IN (<foreach collection="array" item="id"  separator=",">#{id}</foreach>)
    </select>

Traversing data in an array or collection with foreach
Introduction to foreach property, the following link
https://blog.csdn.net/czd3355/article/details/75340080

Using the list collection
The code in the test class is:

 //Use the List collection to find the specified id user
    @Test
    public void test5(){

        sessionUtil s=new sessionUtil();
        SqlSession session=s.getsession();
        System.out.println(session);
        UsersDao dao=session.getMapper(UsersDao.class);
        //list store ids to find
        List ids=new ArrayList();
        ids.add(1);
        ids.add(3);
        ids.add(4);
        ids.add(5);
        List<Users> list=dao.findbyids(ids);
        for (Users users:list){
            System.out.println(users);
        }
    }

The code of the mapper file is:

<select id="findbyids" resultType="Users">
        SELECT * FROM students WHERE studentno IN <foreach collection="list" item="id" open="(" close=")" separator=",">#{id}</foreach>
    </select>

Using the mapper set

The code in the test class is:

 //Fuzzy query using map set
    @Test
    public  void test6(){
        sessionUtil s=new sessionUtil();
        SqlSession session=s.getsession();
        UsersDao dao=session.getMapper(UsersDao.class);
        //Using map set to store data
        Map m=new HashMap();
        m.put("studentname","Zhang");
        List<Users> list=dao.findidmap(m);
        System.out.println(list);
    }

Code for mapper file:

<select id="findidmap" resultType="Users" ><!--parameterType="map",Properties can be added-->
        SELECT * FROM students WHERE 1=1
        <if test="studentname!=null and studentname!=''"><!--And sentence use and  Instead of&&-->
            and  studentname like '%${studentname}%'
        </if>
    </select>

Posted by MerMer on Fri, 20 Dec 2019 07:37:52 -0800