Mybatis二

Posted xuweiweiwoaini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis二相关的知识,希望对你有一定的参考价值。

1 Mybatis的映射文件

1.1 #和$的区别?

1.1.1 #和$的相同点

  • 都是可以从map中获取值或者pojo对象的值

1.1.2 #和$的不同点

  • #{}以预编译的形式,将参数设置到sql语句中。
  • ${}取出的值直接拼接在sql语句中,但是可能会有SQL注入问题。

 

  • 示例:
    • EmployeeMapper.java  
package com.xuweiwei.mybatis.mapper;

import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.annotations.Param;

public interface EmployeeMapper {

    Employee fineEmployeeByIdAndName(@Param("id") Integer id, @Param("lastName") String lastName);

}
    • EmployeeMapper.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">
<!--namespace命名空间  指定接口的全类名-->
<mapper namespace="com.xuweiwei.mybatis.mapper.EmployeeMapper">

    <select id="fineEmployeeByIdAndName" resultType="com.xuweiwei.mybatis.pojo.Employee">
        select * from employee where id = ${id} and last_name = #{lastName}
    </select>
</mapper>
    • 测试  
技术分享图片
package com.xuweiwei.mybatis.test;

import com.xuweiwei.mybatis.mapper.EmployeeMapper;
import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class MybatisTest {

    /**
     * 测试增加
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        /**
         * 创建SqlSessionFactory对象
         */
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 创建SqlSession
         */
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

        Employee employee = employeeMapper.fineEmployeeByIdAndName(1, "许威威");

        System.out.println(employee);

        sqlSession.close();
    }

}
View Code

技术分享图片

 

  • ${}的应用场景:
  • ①分库分表的时候,可以使用${}
select * from ${year}_salary;
  • ②排序
select * from employee order by ${orderColumn} ${order}
  • ③表名
select * from ${tableName}

1.2 select返回

1.2.1 select返回List集合

  • 如果select语句返回的是List集合之类的,在select标签中的resultType只需要写List集合中元素的类型即可。

 

  • 示例:
    • EmployeeMapper.java  
package com.xuweiwei.mybatis.mapper;

import com.xuweiwei.mybatis.pojo.Employee;

import java.util.List;

public interface EmployeeMapper {

    List<Employee> findEmployeeListLikeLastName(String lastName);

}
    • EmployeeMapper.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">
<!--namespace命名空间  指定接口的全类名-->
<mapper namespace="com.xuweiwei.mybatis.mapper.EmployeeMapper">

    <select id="findEmployeeListLikeLastName" resultType="com.xuweiwei.mybatis.pojo.Employee">
        select * from employee where last_name like #{lastName}
    </select>
</mapper>
    • 测试  
技术分享图片
package com.xuweiwei.mybatis.test;

import com.xuweiwei.mybatis.mapper.EmployeeMapper;
import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest {

    /**
     * 测试增加
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        /**
         * 创建SqlSessionFactory对象
         */
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 创建SqlSession
         */
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = employeeMapper.findEmployeeListLikeLastName("%许%");
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }

        sqlSession.close();
    }
}
View Code

 

  • 示例:对应的注解写法:
package com.xuweiwei.mybatis.mapper;

import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface EmployeeMapper {

    @ResultType(Employee.class)
    @Select(" select * from employee where id = #{id} ")
    List<Employee> findEmployeeListLikeLastName(String lastName);

}

1.2.2 select返回Map

  • map中的key就是列名,value就是对应的值。

 

  • 示例:
    • EmployeeMapper.java  
package com.xuweiwei.mybatis.mapper;

import java.util.Map;

public interface EmployeeMapper {

    Map<String,Object> selectEmployeeById(Integer id);

}
    • EmployeeMapper.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">
<!--namespace命名空间  指定接口的全类名-->
<mapper namespace="com.xuweiwei.mybatis.mapper.EmployeeMapper">

    <select id="selectEmployeeById" resultType="java.util.Map">
        select * from employee where id = #{id}
    </select>

</mapper>
    • 测试  
技术分享图片
package com.xuweiwei.mybatis.test;

import com.xuweiwei.mybatis.mapper.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class MybatisTest {

    /**
     * 测试增加
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        /**
         * 创建SqlSessionFactory对象
         */
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 创建SqlSession
         */
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Map<String, Object> map = employeeMapper.selectEmployeeById(1);

        for (Map.Entry<String, Object> m : map.entrySet()) {
            System.out.println("key:" + m.getKey());
            System.out.println("value:" + m.getValue());
        }

        sqlSession.close();
    }

}
View Code

 

  • 示例:对应的注解写法
package com.xuweiwei.mybatis.mapper;

import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface EmployeeMapper {

    @ResultType(Map.class)
    @Select("select * from employee where id = #{id}")
    Map<String,Object> selectEmployeeById(Integer id);

}

 

  • Map中的key是主键,value是对应的对象

 

  • 示例:
    • EmployeeMapper.java  
package com.xuweiwei.mybatis.mapper;

import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.annotations.MapKey;

import java.util.Map;

public interface EmployeeMapper {

    /**
     * 使用注解MapKey告诉Mybatis那个是key
     * @return
     */
    @MapKey("id")
    Map<Integer, Employee> findEmployeeMap();

}
    • EmployeeMapper.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">
<!--namespace命名空间  指定接口的全类名-->
<mapper namespace="com.xuweiwei.mybatis.mapper.EmployeeMapper">

    <!--这边的resultType实际测试下来既可以是Map中元素Employee也可以是java.util.Map-->
    <select id="findEmployeeMap" resultType="com.xuweiwei.mybatis.pojo.Employee">
        select * from employee
    </select>


</mapper>
    • 测试  
技术分享图片
package com.xuweiwei.mybatis.test;

import com.xuweiwei.mybatis.mapper.EmployeeMapper;
import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class MybatisTest {

    /**
     * 测试增加
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        /**
         * 创建SqlSessionFactory对象
         */
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 创建SqlSession
         */
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        Map<Integer, Employee> employeeMap = employeeMapper.findEmployeeMap();
        System.out.println(employeeMap);

        sqlSession.close();
    }


}
View Code

 

  • 示例:对应的注解写法
package com.xuweiwei.mybatis.mapper;

import com.xuweiwei.mybatis.pojo.Employee;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface EmployeeMapper {


    @MapKey("id")
    @ResultType(Map.class)
    @Select(" select * from employee ")
    Map<Integer, Employee> findEmployeeMap();

}

1.2.3 select自定义结果集--resultMap

  • resultMap不能和resultType一起使用。

 

以上是关于Mybatis二的主要内容,如果未能解决你的问题,请参考以下文章

二代身份证号码编码规则

染色体基因芯片分析和第二代测序应用的区别

markdown [mybatis参考]关于mybatis #mybatis的一些片段

mybatis学习(39):动态sql片段

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

mybatis动态sql片段与分页,排序,传参的使用