zoukankan      html  css  js  c++  java
  • MyBatis详解 与配置MyBatis+Spring+MySql

    MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。
    需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spring结合包)。
    MyBatis简介
          MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,Mybatis 是一种“半自动化”的ORM实现。
    需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spring结合包)。
    下载地址:
    http://ibatis.apache.org/tools/ibator
    http://code.google.com/p/mybatis/



    1.2MyBatis+Spring+MySql简单配置
    1.2.1搭建Spring环境
    1,建立maven的web项目;
    2,加入Spring框架、配置文件;
    3,在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);
    4,更改web.xml和spring的配置文件;
    5,添加一个jsp页面和对应的Controller;
    6,测试。

    可参照:http://limingnihao.javaeye.com/blog/830409。使用Eclipse的Maven构建SpringMVC项目
    1.2.2建立MySql数据库
    建立一个学生选课管理数据库。
    表:学生表、班级表、教师表、课程表、学生选课表。
    逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;

    使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。

    更多sql请下载项目源文件,在resource/sql中。

    CREATE DATABASE STUDENT_MANAGER; 
    USE STUDENT_MANAGER; 
     
     
    CREATE TABLE STUDENT_TBL 
    ( 
       STUDENT_ID         VARCHAR(255) PRIMARY KEY, 
       STUDENT_NAME       VARCHAR(10) NOT NULL, 
       STUDENT_SEX        VARCHAR(10), 
       STUDENT_BIRTHDAY   DATE, 
       CLASS_ID           VARCHAR(255) 
    ); 
    INSERT INTO STUDENT_TBL (STUDENT_ID, 
                             STUDENT_NAME, 
                             STUDENT_SEX, 
                             STUDENT_BIRTHDAY, 
                             CLASS_ID) 
      VALUES   (123456, 
                '某某某', 
                '', 
                '1980-08-01', 
                121546 
                ) 

    创建连接MySql使用的配置文件mysql.properties。

    Mysql.properties代码

    jdbc.driverClassName=com.mysql.jdbc.Driver 
    jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8 


    1.2.3搭建MyBatis环境
    顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。

    1.2.3.1创建实体类: StudentEntity

    public class StudentEntity implements Serializable { 
     
        private static final long serialVersionUID = 3096154202413606831L; 
        private ClassEntity classEntity; 
        private Date studentBirthday; 
        private String studentID; 
        private String studentName; 
        private String studentSex; 
         
        public ClassEntity getClassEntity() { 
            return classEntity; 
        } 
     
        public Date getStudentBirthday() { 
            return studentBirthday; 
        } 
     
        public String getStudentID() { 
            return studentID; 
        } 
     
        public String getStudentName() { 
            return studentName; 
        } 
     
        public String getStudentSex() { 
            return studentSex; 
        } 
     
        public void setClassEntity(ClassEntity classEntity) { 
            this.classEntity = classEntity; 
        } 
     
        public void setStudentBirthday(Date studentBirthday) { 
            this.studentBirthday = studentBirthday; 
        } 
     
        public void setStudentID(String studentID) { 
            this.studentID = studentID; 
        } 
     
        public void setStudentName(String studentName) { 
            this.studentName = studentName; 
        } 
     
        public void setStudentSex(String studentSex) { 
            this.studentSex = studentSex; 
        } 
    } 
    View Code

    1.2.3.2创建数据访问接口
    Student类对应的dao接口:StudentMapper。

    public interface StudentMapper { 
         
        public StudentEntity getStudent(String studentID); 
         
        public StudentEntity getStudentAndClass(String studentID); 
         
        public List<StudentEntity> getStudentAll(); 
         
        public void insertStudent(StudentEntity entity); 
         
        public void deleteStudent(StudentEntity entity); 
         
        public void updateStudent(StudentEntity entity); 
    } 

    1.2.3.3创建SQL映射语句文件

    Student类的sql语句文件StudentMapper.xml
    resultMap标签:表字段与属性的映射。
    Select标签:查询sql

    <?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="com.manager.data.StudentMapper"> 
     
        <resultMap type="StudentEntity" id="studentResultMap"> 
            <id property="studentID" column="STUDENT_ID"/> 
            <result property="studentName" column="STUDENT_NAME"/> 
            <result property="studentSex" column="STUDENT_SEX"/> 
            <result property="studentBirthday" column="STUDENT_BIRTHDAY"/> 
        </resultMap> 
         
        <!-- 查询学生,根据id --> 
        <select id="getStudent" parameterType="String" resultType="StudentEntity" resultMap="studentResultMap"> 
            <![CDATA[
                SELECT * from STUDENT_TBL ST
                    WHERE ST.STUDENT_ID = #{studentID} 
            ]]>  
        </select> 
         
        <!-- 查询学生列表 --> 
        <select id="getStudentAll"  resultType="com.manager.data.model.StudentEntity" resultMap="studentResultMap"> 
            <![CDATA[
                SELECT * from STUDENT_TBL
            ]]>  
        </select> 
         
    </mapper> 
    View Code

    1.2.3.4创建MyBatis的mapper配置文件
    在src/main/resource中创建MyBatis配置文件:mybatis-config.xml。
    typeAliases标签:给类起一个别名。com.manager.data.model.StudentEntity类,可以使用StudentEntity代替。
    Mappers标签:加载MyBatis中实体类的SQL映射语句文件。

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
    <configuration> 
        <typeAliases> 
            <typeAlias alias="StudentEntity" type="com.manager.data.model.StudentEntity"/> 
        </typeAliases> 
        <mappers> 
            <mapper resource="com/manager/data/maps/StudentMapper.xml" /> 
        </mappers> 
    </configuration>   

    1.2.3.5修改Spring 的配置文件
    主要是添加SqlSession的制作工厂类的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。
    和数据访问接口对应的实现bean。通过MapperFactoryBean创建出来。需要执行接口类全称和SqlSession工厂bean的引用。
    <!-- 导入属性配置文件 --> 

    <context:property-placeholder location="classpath:mysql.properties" /> 
     
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${jdbc.driverClassName}" /> 
        <property name="url" value="${jdbc.url}" /> 
    </bean> 
     
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 
     
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="configLocation" value="classpath:mybatis-config.xml" /> 
        <property name="dataSource" ref="dataSource" /> 
    </bean> 
     
    <!— mapper bean --> 
    <bean id="studentMapper" class="org.mybatis.spring.MapperFactoryBean"> 
        <property name="mapperInterface" value="com.manager.data.StudentMapper" /> 
        <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
    </bean> 

    1.2.4测试StudentMapper
    使用SpringMVC测试,创建一个TestController,配置tomcat,访问index.do页面进行测试:

    @Controller 
    public class TestController { 
     
        @Autowired 
        private StudentMapper studentMapper; 
         
        @RequestMapping(value = "index.do") 
        public void indexPage() {    
            StudentEntity entity = studentMapper.getStudent("10000013"); 
            System.out.println("name:" + entity.getStudentName()); 
        }    
    } 

    使用Junit测试: 

    @RunWith(value = SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(value = "test-servlet.xml") 
    public class StudentMapperTest { 
         
        @Autowired 
        private ClassMapper classMapper; 
         
        @Autowired 
        private StudentMapper studentMapper; 
         
        @Transactional 
        public void getStudentTest(){ 
            StudentEntity entity = studentMapper.getStudent("10000013"); 
            System.out.println("" + entity.getStudentID() + entity.getStudentName()); 
             
            List<StudentEntity> studentList = studentMapper.getStudentAll(); 
            for( StudentEntity entityTemp : studentList){ 
                System.out.println(entityTemp.getStudentName()); 
            } 
             
        } 
    } 

             更详细的功能源代码http://limingnihao.javaeye.com/admin/blogs/782190页面最下面;

    二、SQL语句映射文件(1)resultMap
    SQL 映射XML 文件是所有sql语句放置的地方。需要定义一个workspace,一般定义为对应的接口类的路径。写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如:

    <mappers> 
        <mapper resource="com/liming/manager/data/mappers/UserMapper.xml" /> 
        <mapper resource="com/liming/manager/data/mappers/StudentMapper.xml" /> 
        <mapper resource="com/liming/manager/data/mappers/ClassMapper.xml" /> 
        <mapper resource="com/liming/manager/data/mappers/TeacherMapper.xml" /> 
    </mappers

    SQL 映射XML 文件一些初级的元素:
    1. cache – 配置给定模式的缓存
    2. cache-ref – 从别的模式中引用一个缓存
    3. resultMap – 这是最复杂而却强大的一个元素了,它描述如何从结果集中加载对象
    4. sql – 一个可以被其他语句复用的SQL 块
    5. insert – 映射INSERT 语句
    6. update – 映射UPDATE 语句
    7. delete – 映射DELEETE 语句
    8. select  -  映射SELECT语句

    2.1 resultMap
            resultMap 是MyBatis 中最重要最强大的元素了。你可以让你比使用JDBC 调用结果集省掉90%的代码,也可以让你做许多JDBC 不支持的事。现实上,要写一个等同类似于交互的映射这样的复杂语句,可能要上千行的代码。ResultMaps 的目的,就是这样简单的语句而不需要多余的结果映射,更多复杂的语句,除了只要一些绝对必须的语句描述关系以外,再也不需要其它的。

    resultMap属性:type为java实体类;id为此resultMap的标识。

    resultMap可以设置的映射:

    1. constructor – 用来将结果反射给一个实例化好的类的构造器
    
    a) idArg – ID 参数;将结果集标记为ID,以方便全局调用
    b) arg –反射到构造器的通常结果
    
    2. id – ID 结果,将结果集标记为ID,以方便全局调用
    
    3. result – 反射到JavaBean 属性的普通结果
    
    4. association – 复杂类型的结合;多个结果合成的类型
    
    a) nested result mappings – 几resultMap 自身嵌套关联,也可以引用到一个其它上
    
    5. collection –复杂类型集合a collection of complex types
    
    6. nested result mappings – resultMap 的集合,也可以引用到一个其它上
    
    7. discriminator – 使用一个结果值以决定使用哪个resultMap
    
    a) case – 基本一些值的结果映射的case 情形
    
    i. nested result mappings –一个case 情形本身就是一个结果映射,因此也可以包括一些相同的元素,也可以引用一个外部resultMap。

    2.1.1 id、result
    id、result是最简单的映射,id为主键映射;result其他基本数据库表字段到实体类属性的映射。
      最简单的例子:

    <resultMap type="StudentEntity" id="studentResultMap"> 
        <id property="studentID" column="STUDENT_ID"/> 
        <result property="studentName" column="STUDENT_NAME"/> 
        <result property="studentSex" column="STUDENT_SEX"/> 
        <result property="studentBirthday" column="STUDENT_BIRTHDAY"/> 
    </resultMap> 

    id、result语句属性配置细节:
    属性
    描述

    property
    需要映射到JavaBean 的属性名称。

    column
    数据表的列名或者标签别名。

    javaType
    一个完整的类名,或者是一个类型别名。如果你匹配的是一个JavaBean,那MyBatis 通常会自行检测到。然后,如果你是要映射到一个HashMap,那你需要指定javaType 要达到的目的。

    jdbcType
    数据表支持的类型列表。这个属性只在insert,update 或delete 的时候针对允许空的列有用。JDBC 需要这项,但MyBatis 不需要。如果你是直接针对JDBC 编码,且有允许空的列,而你要指定这项。

    typeHandler
    使用这个属性可以覆写类型处理器。这项值可以是一个完整的类名,也可以是一个类型别名。

    支持的JDBC类型
           为了将来的引用,MyBatis 支持下列JDBC 类型,通过JdbcType 枚举:
    BIT,FLOAT,CHAR,TIMESTAMP,OTHER,UNDEFINED,TINYINT,REAL,VARCHAR,BINARY,BLOB,NVARCHAR,SMALLINT,DOUBLE,LONGVARCHAR,VARBINARY,CLOB,NCHAR,INTEGER,NUMERIC,DATE,LONGVARBINARY,BOOLEAN,NCLOB,BIGINT,DECIMAL,TIME,NULL,CURSOR

    2.1.2 constructor

            我们使用id、result时候,需要定义java实体类的属性映射到数据库表的字段上。这个时候是使用JavaBean实现的。当然我们也可以使用实体类的构造方法来实现值的映射,这个时候是通过构造方法参数的书写的顺序来进行赋值的。
            使用construcotr功能有限(例如使用collection级联查询)。
            上面使用id、result实现的功能就可以改为:

    <resultMap type="StudentEntity" id="studentResultMap" > 
        <constructor> 
            <idArg javaType="String" column="STUDENT_ID"/> 
            <arg javaType="String" column="STUDENT_NAME"/> 
            <arg javaType="String" column="STUDENT_SEX"/> 
            <arg javaType="Date" column="STUDENT_BIRTHDAY"/> 
        </constructor> 
    </resultMap> 


            当然,我们需要定义StudentEntity实体类的构造方法:

    public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){ 
        this.studentID = studentID; 
        this.studentName = studentName; 
        this.studentSex = studentSex; 
        this.studentBirthday = studentBirthday; 
    } 


    2.1.3 association联合
    联合元素用来处理“一对一”的关系。需要指定映射的Java实体类的属性,属性的javaType(通常MyBatis 自己会识别)。对应的数据库表的列名称。如果想覆写的话返回结果的值,需要指定typeHandler。
    不同情况需要告诉MyBatis 如何加载一个联合。MyBatis 可以用两种方式加载:

    1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;
    2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

    例如,一个班级对应一个班主任。
    首先定义好班级中的班主任属性:

    private TeacherEntity teacherEntity; 

    2.1.3.1使用select实现联合
    例:班级实体类中有班主任的属性,通过联合在得到一个班级实体时,同时映射出班主任实体。

    这样可以直接复用在TeacherMapper.xml文件中定义好的查询teacher根据其ID的select语句。而且不需要修改写好的SQL语句,只需要直接修改resultMap即可。

    ClassMapper.xml文件部分内容:

    <resultMap type="ClassEntity" id="classResultMap"> 
        <id property="classID" column="CLASS_ID" /> 
        <result property="className" column="CLASS_NAME" /> 
        <result property="classYear" column="CLASS_YEAR" /> 
        <association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/> 
    </resultMap> 
     
    <select id="getClassByID" parameterType="String" resultMap="classResultMap"> 
        SELECT * FROM CLASS_TBL CT 
        WHERE CT.CLASS_ID = #{classID}; 
    </select> 


    TeacherMapper.xml文件部分内容:

    <resultMap type="TeacherEntity" id="teacherResultMap"> 
        <id property="teacherID" column="TEACHER_ID" /> 
        <result property="teacherName" column="TEACHER_NAME" /> 
        <result property="teacherSex" column="TEACHER_SEX" /> 
        <result property="teacherBirthday" column="TEACHER_BIRTHDAY"/> 
        <result property="workDate" column="WORK_DATE"/> 
        <result property="professional" column="PROFESSIONAL"/> 
    </resultMap> 
    <select id="getTeacher" parameterType="String"  resultMap="teacherResultMap"> 
        SELECT * 
          FROM TEACHER_TBL TT 
         WHERE TT.TEACHER_ID = #{teacherID} 
    </select> 

    2.1.3.2使用resultMap实现联合
    与上面同样的功能,查询班级,同时查询器班主任。需在association中添加resultMap(在teacher的xml文件中定义好的),新写sql(查询班级表left join教师表),不需要teacher的select。

    修改ClassMapper.xml文件部分内容:

    <resultMap type="ClassEntity" id="classResultMap"> 
        <id property="classID" column="CLASS_ID" /> 
        <result property="className" column="CLASS_NAME" /> 
        <result property="classYear" column="CLASS_YEAR" /> 
        <association property="teacherEntity" column="TEACHER_ID"  resultMap="teacherResultMap"/> 
    </resultMap> 
    <select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap"> 
        SELECT * 
          FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID 
         WHERE CT.CLASS_ID = #{classID}; 
    </select> 

    其中的teacherResultMap请见上面TeacherMapper.xml文件部分内容中。

    2.1.4 collection聚集
    聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);对应的数据库表的列名称;
    不同情况需要告诉MyBatis 如何加载一个聚集。MyBatis 可以用两种方式加载:

    1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;
    2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

    例如,一个班级有多个学生。
    首先定义班级中的学生列表属性:

     private List<StudentEntity> studentList;  


    2.1.4.1使用select实现聚集
    用法和联合很类似,区别在于,这是一对多,所以一般映射过来的都是列表。所以这里需要定义javaType为ArrayList,还需要定义列表中对象的类型ofType,以及必须设置的select的语句名称(需要注意的是,这里的查询 student的select语句条件必须是外键classID)。

    ClassMapper.xml文件部分内容:

    <resultMap type="ClassEntity" id="classResultMap"> 
        <id property="classID" column="CLASS_ID" /> 
        <result property="className" column="CLASS_NAME" /> 
        <result property="classYear" column="CLASS_YEAR" /> 
        <association property="teacherEntity" column="TEACHER_ID"  select="getTeacher"/> 
        <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/> 
    </resultMap> 
     
    <select id="getClassByID" parameterType="String" resultMap="classResultMap"> 
        SELECT * FROM CLASS_TBL CT 
        WHERE CT.CLASS_ID = #{classID}; 
    </select> 

    StudentMapper.xml文件部分内容:

    <!-- java属性,数据库表字段之间的映射定义 --> 
    <resultMap type="StudentEntity" id="studentResultMap"> 
        <id property="studentID" column="STUDENT_ID" /> 
        <result property="studentName" column="STUDENT_NAME" /> 
        <result property="studentSex" column="STUDENT_SEX" /> 
        <result property="studentBirthday" column="STUDENT_BIRTHDAY" /> 
    </resultMap> 
     
    <!-- 查询学生list,根据班级id --> 
    <select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap"> 
        <include refid="selectStudentAll" /> 
        WHERE ST.CLASS_ID = #{classID} 
    </select> 


    2.1.4.2使用resultMap实现聚集
    使用resultMap,就需要重写一个sql,left join学生表。

    <resultMap type="ClassEntity" id="classResultMap"> 
        <id property="classID" column="CLASS_ID" /> 
        <result property="className" column="CLASS_NAME" /> 
        <result property="classYear" column="CLASS_YEAR" /> 
        <association property="teacherEntity" column="TEACHER_ID"  resultMap="teacherResultMap"/> 
        <collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/> 
    </resultMap> 
    <select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap"> 
        SELECT * 
          FROM CLASS_TBL CT 
               LEFT JOIN STUDENT_TBL ST 
                  ON CT.CLASS_ID = ST.CLASS_ID 
               LEFT JOIN TEACHER_TBL TT 
                  ON CT.TEACHER_ID = TT.TEACHER_ID 
          WHERE CT.CLASS_ID = #{classID}; 
    </select> 

    其中的teacherResultMap请见上面TeacherMapper.xml文件部分内容中。studentResultMap请见上面StudentMapper.xml文件部分内容中。

    二、SQL语句映射文件(2)增删改查、参数、缓存
    2.2 select
    一个select 元素非常简单。例如:

    <!-- 查询学生,根据id --> 
    <select id="getStudent" parameterType="String" resultMap="studentResultMap"> 
        SELECT ST.STUDENT_ID, 
                   ST.STUDENT_NAME, 
                   ST.STUDENT_SEX, 
                   ST.STUDENT_BIRTHDAY, 
                   ST.CLASS_ID 
              FROM STUDENT_TBL ST 
             WHERE ST.STUDENT_ID = #{studentID} 
    </select> 

    这条语句就叫做‘getStudent,有一个String参数,并返回一个StudentEntity类型的对象。
    注意参数的标识是:#{studentID}。

    select 语句属性配置细节:

    属性 描述 取值 默认

    id 在这个模式下唯一的标识符,可被其它语句引用
    parameterType 传给此语句的参数的完整类名或别名
    resultType 语句返回值类型的整类名或别名。注意,如果是集合,那么这里填写的是集合的项的整类名或别名,而不是集合本身的类名。(resultType 与resultMap 不能并用)
    resultMap 引用的外部resultMap 名。结果集映射是MyBatis 中最强大的特性。许多复杂的映射都可以轻松解决。(resultType 与resultMap 不能并用)
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。
    预准备语句、可调用语句 STATEMENT
    PREPARED
    CALLABLE PREPARED
    resultSetType forward_only,scroll_sensitive,scroll_insensitive
    只转发,滚动敏感,不区分大小写的滚动 FORWARD_ONLY
    SCROLL_SENSITIVE
    SCROLL_INSENSITIVE 驱动器决定

    2.3 insert
    一个简单的insert语句:

    Xml代码
    <!-- 插入学生 --> 

    <insert id="insertStudent" parameterType="StudentEntity"> 
            INSERT INTO STUDENT_TBL (STUDENT_ID, 
                                              STUDENT_NAME, 
                                              STUDENT_SEX, 
                                              STUDENT_BIRTHDAY, 
                                              CLASS_ID) 
                  VALUES   (#{studentID}, 
                              #{studentName}, 
                              #{studentSex}, 
                              #{studentBirthday}, 
                              #{classEntity.classID}) 
    </insert> 
    insert可以使用数据库支持的自动生成主键策略,设置useGeneratedKeys=”true”,然后把keyProperty 设成对应的列,就搞定了。比如说上面的StudentEntity 使用auto-generated 为id 列生成主键.

    还可以使用selectKey元素。下面例子,使用mysql数据库nextval('student')为自定义函数,用来生成一个key。

    <!-- 插入学生 自动主键--> 

    <insert id="insertStudentAutoKey" parameterType="StudentEntity"> 
        <selectKey keyProperty="studentID" resultType="String" order="BEFORE"> 
                select nextval('student') 
        </selectKey> 
            INSERT INTO STUDENT_TBL (STUDENT_ID, 
                                     STUDENT_NAME, 
                                     STUDENT_SEX, 
                                     STUDENT_BIRTHDAY, 
                                     CLASS_ID) 
                  VALUES   (#{studentID}, 
                            #{studentName}, 
                            #{studentSex}, 
                            #{studentBirthday}, 
                            #{classEntity.classID})     
    </insert> 

    insert语句属性配置细节:

    属性 描述 取值 默认

    id 在这个模式下唯一的标识符,可被其它语句引用
    parameterType 传给此语句的参数的完整类名或别名
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。
    预准备语句、可调用语句 STATEMENT
    PREPARED
    CALLABLE PREPARED
    useGeneratedKeys
    告诉MyBatis 使用JDBC 的getGeneratedKeys 方法来获取数据库自己生成的主键(MySQL、SQLSERVER 等
    
    关系型数据库会有自动生成的字段)。默认:false

     true|false false keyProperty 标识一个将要被MyBatis 设置进getGeneratedKeys 的key 所返回的值,或者为insert 语句使用一个selectKey 

    selectKey语句属性配置细节:
    属性 描述 取值

    keyProperty selectKey 语句生成结果需要设置的属性。
    resultType 生成结果类型,MyBatis 允许使用基本的数据类型,包括String 、int类型。
    order 可以设成BEFORE 或者AFTER,如果设为BEFORE,那它会先选择主键,然后设置keyProperty,再执行insert语句;如果设为AFTER,它就先运行 insert 语句再运行selectKey 语句,通常是insert 语句中内部调用数据库(像Oracle)内嵌的序列机制。 BEFORE
    AFTER
    statementType 像上面的那样, MyBatis 支持STATEMENT,PREPARED和CALLABLE 的语句形式, 对应Statement ,PreparedStatement 和CallableStatement 响应 STATEMENT
    PREPARED
    CALLABLE
    2.4 update、delete
    一个简单的update:
    <!-- 更新学生信息 --> 
    <update id="updateStudent" parameterType="StudentEntity"> 
            UPDATE STUDENT_TBL 
                SET STUDENT_TBL.STUDENT_NAME = #{studentName},  
                    STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
                    STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
                    STUDENT_TBL.CLASS_ID = #{classEntity.classID} 
             WHERE STUDENT_TBL.STUDENT_ID = #{studentID};    
    </update> 

    一个简单的delete:

    <!-- 删除学生 --> 
    <delete id="deleteStudent" parameterType="StudentEntity"> 
            DELETE FROM STUDENT_TBL WHERE STUDENT_ID = #{studentID} 
    </delete> 

    update、delete语句属性配置细节:

    属性 描述 取值 默认

    id 在这个模式下唯一的标识符,可被其它语句引用
    parameterType 传给此语句的参数的完整类名或别名
    flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true|false false
    useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 true|false false
    timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
    fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
    statementType statement,preparedstatement,callablestatement。
    预准备语句、可调用语句 STATEMENT
    PREPARED
    CALLABLE PREPARED


    2.5 sql
    Sql元素用来定义一个可以复用的SQL 语句段,供其它语句调用。比如:
    <!-- 复用sql语句  查询student表所有字段 --> 

    <sql id="selectStudentAll"> 
            SELECT ST.STUDENT_ID, 
                       ST.STUDENT_NAME, 
                       ST.STUDENT_SEX, 
                       ST.STUDENT_BIRTHDAY, 
                       ST.CLASS_ID 
                  FROM STUDENT_TBL ST 
    </sql> 

    这样,在select的语句中就可以直接引用使用了,将上面select语句改成:

    <!-- 查询学生,根据id --> 
    <select id="getStudent" parameterType="String" resultMap="studentResultMap"> 
        <include refid="selectStudentAll"/> 
                WHERE ST.STUDENT_ID = #{studentID}  
    </select> 

    2.6

    parameters
            上面很多地方已经用到了参数,比如查询、修改、删除的条件,插入,修改的数据等,MyBatis可以使用的基本数据类型和Java的复杂数据类型。
            基本数据类型,String,int,date等。
            但是使用基本数据类型,只能提供一个参数,所以需要使用Java实体类,或Map类型做参数类型。通过#{}可以直接得到其属性。

    2.6.1基本类型参数
    根据入学时间,检索学生列表:

    <!-- 查询学生list,根据入学时间  --> 
    <select id="getStudentListByDate"  parameterType="Date" resultMap="studentResultMap"> 
        SELECT * 
          FROM STUDENT_TBL ST LEFT JOIN CLASS_TBL CT ON ST.CLASS_ID = CT.CLASS_ID 
         WHERE CT.CLASS_YEAR = #{classYear};     
    </select>
    List<StudentEntity> studentList = studentMapper.getStudentListByClassYear(StringUtil.parse("2007-9-1")); 
    for (StudentEntity entityTemp : studentList) { 
        System.out.println(entityTemp.toString()); 
    }

    2.6.2Java实体类型参数
    根据姓名和性别,检索学生列表。使用实体类做参数:

    <!-- 查询学生list,like姓名、=性别,参数entity类型 --> 
    <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
            WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
              AND ST.STUDENT_SEX = #{studentSex} 
    </select> 
    StudentEntity entity = new StudentEntity(); 
    entity.setStudentName("李"); 
    entity.setStudentSex("男"); 
    List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity); 
    for (StudentEntity entityTemp : studentList) { 
        System.out.println(entityTemp.toString()); 
    } 


    2.6.3Map参数
    根据姓名和性别,检索学生列表。使用Map做参数:

    <!-- 查询学生list,=性别,参数map类型 --> 
    <select id="getStudentListWhereMap" parameterType="Map" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
         WHERE ST.STUDENT_SEX = #{sex} 
              AND ST.STUDENT_SEX = #{sex} 
    </select> 
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("sex", "女"); 
    map.put("name", "李"); 
    List<StudentEntity> studentList = studentMapper.getStudentListWhereMap(map); 
    for (StudentEntity entityTemp : studentList) { 
        System.out.println(entityTemp.toString()); 
    } 

    2.6.4多参数的实现
    如果想传入多个参数,则需要在接口的参数上添加@Param注解。给出一个实例:
    接口写法:
     public List<StudentEntity> getStudentListWhereParam(@Param(value = "name") String name, @Param(value = "sex") String sex, @Param(value = "birthday") Date birthdar, @Param(value = "classEntity")ClassEntityclassEntity);  
    SQL写法:

    <!-- 查询学生list,like姓名、=性别、=生日、=班级,多参数方式 --> 
    <select id="getStudentListWhereParam" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <where> 
            <if test="name!=null and name!='' "> 
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{name}),'%') 
            </if> 
            <if test="sex!= null and sex!= '' "> 
                AND ST.STUDENT_SEX = #{sex} 
            </if> 
            <if test="birthday!=null"> 
                AND ST.STUDENT_BIRTHDAY = #{birthday} 
            </if> 
            <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> 
                AND ST.CLASS_ID = #{classEntity.classID} 
            </if> 
        </where> 
    </select> 

    进行查询:
     List<StudentEntity> studentList = studentMapper.getStudentListWhereParam("", "",StringUtil.parse("1985-05-28"), classMapper.getClassByID("20000002")); for (StudentEntity entityTemp : studentList) { System.out.println(entityTemp.toString()); }  

    2.6.5字符串代入法
            默认的情况下,使用#{}语法会促使MyBatis 生成PreparedStatement 属性并且使用PreparedStatement 的参数(=?)来安全的设置值。尽量这些是快捷安全,也是经常使用的。但有时候你可能想直接未更改的字符串代入到SQL 语句中。比如说,对于ORDER BY,你可能会这样使用:ORDER BY ${columnName}但MyBatis 不会修改和规避掉这个字符串。
            注意:这样地接收和应用一个用户输入到未更改的语句中,是非常不安全的。这会让用户能植入破坏代码,所以,要么要求字段不要允许客户输入,要么你直接来检测他的合法性 。

    2.7 cache缓存
            MyBatis 包含一个强在的、可配置、可定制的缓存机制。MyBatis 3 的缓存实现有了许多改进,既强劲也更容易配置。默认的情况,缓存是没有开启,除了会话缓存以外,它可以提高性能,且能解决全局依赖。开启二级缓存,你只需要在SQL 映射文件中加入简单的一行:<cache/>


    这句简单的语句的作用如下:

    1. 所有在映射文件里的select 语句都将被缓存。
    2. 所有在映射文件里insert,update 和delete 语句会清空缓存。
    3. 缓存使用“最近很少使用”算法来回收
    4. 缓存不会被设定的时间所清空。
    5. 每个缓存可以存储1024 个列表或对象的引用(不管查询出来的结果是什么)。
    6. 缓存将作为“读/写”缓存,意味着获取的对象不是共享的且对调用者是安全的。不会有其它的调用
    7. 者或线程潜在修改。


    例如,创建一个FIFO 缓存让60 秒就清空一次,存储512 个对象结果或列表引用,并且返回的结果是只读。因为在不用的线程里的两个调用者修改它们可能会导致引用冲突。

    <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"> 
    </cache> 

      还可以在不同的命名空间里共享同一个缓存配置或者实例。在这种情况下,你就可以使用cache-ref 来引用另外一个缓存。

    <cache-ref namespace="com.liming.manager.data.StudentMapper"/> 
    Cache 语句属性配置细节:
    
    属性 说明 取值 默认值
    eviction 缓存策略:
    LRU - 最近最少使用法:移出最近较长周期内都没有被使用的对象。
    FIFI- 先进先出:移出队列里较早的对象
    SOFT - 软引用:基于软引用规则,使用垃圾回收机制来移出对象
    WEAK - 弱引用:基于弱引用规则,使用垃圾回收机制来强制性地移出对象 LRU
    FIFI
    SOFT
    WEAK LRU
    flushInterval 代表一个合理的毫秒总计时间。默认是不设置,因此使用无间隔清空即只能调用语句来清空。 正整数
    不设置
    
    size 缓存的对象的大小 正整数 1024
    readOnly
    只读缓存将对所有调用者返回同一个实例。因此都不能被修改,这可以极大的提高性能。可写的缓存将通过序列
    
    化来返回一个缓存对象的拷贝。这会比较慢,但是比较安全。所以默认值是false。

    三、动态SQL语句
            有些时候,sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。
            下文均采用mysql语法和函数(例如字符串链接函数CONCAT)。

    3.1 if标签
    一个很普通的查询:

    <!-- 查询学生list,like姓名 --> 
    <select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST  
    WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
    </select> 
    但是此时如果studentName是null或空字符串,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就进行此条件的判断。


    修改为:

    <!-- 查询学生list,like姓名 --> 
    <select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <if test="studentName!=null and studentName!='' "> 
            WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
        </if> 
    </select> 
    此时,当studentName的值为null或’’的时候,我们并不进行where条件的判断,所以当studentName值为null或’’值,不附带这个条件,所以查询结果是全部。

    由于参数是Java的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, new一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会where这个条件,相反不去赋值就可以不在where中判断。

       代码中的where标签,请参考3.2.1.

    <!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 --> 
    <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <where> 
            <if test="studentName!=null and studentName!='' "> 
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
            </if> 
            <if test="studentSex!= null and studentSex!= '' "> 
                AND ST.STUDENT_SEX = #{studentSex} 
            </if> 
            <if test="studentBirthday!=null"> 
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday} 
            </if> 
            <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> 
                AND ST.CLASS_ID = #{classEntity.classID} 
            </if> 
        </where> 
    </select> 
    查询,姓名中有‘李’,男,生日在‘1985-05-28’,班级在‘20000002’的学生。


    Java代码

    StudentEntity entity = new StudentEntity(); 
    entity.setStudentName("李"); 
    entity.setStudentSex("男"); 
    entity.setStudentBirthday(StringUtil.parse("1985-05-28")); 
    entity.setClassEntity(classMapper.getClassByID("20000002")); 
    List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity); 
    for( StudentEntity entityTemp : studentList){ 
        System.out.println(entityTemp.toString()); 
    } 
    3.2 where、set、trim标签
    3.2.1 where

    当if标签较多时,这样的组合可能会导致错误。例如,like姓名,等于指定性别等:

    <!-- 查询学生list,like姓名,=性别 --> 
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
            WHERE 
            <if test="studentName!=null and studentName!='' "> 
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
            </if> 
            <if test="studentSex!= null and studentSex!= '' "> 
                AND ST.STUDENT_SEX = #{studentSex} 
            </if> 
    </select> 


    如果上面例子,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。
    这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
    上面例子修改为:

    <!-- 查询学生list,like姓名,=性别 --> 
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <where> 
            <if test="studentName!=null and studentName!='' "> 
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
            </if> 
            <if test="studentSex!= null and studentSex!= '' "> 
                AND ST.STUDENT_SEX = #{studentSex} 
            </if> 
        </where> 
    </select> 

    3.2.2 set
    当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
    没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:

    <!-- 更新学生信息 --> 
    <update id="updateStudent" parameterType="StudentEntity"> 
        UPDATE STUDENT_TBL 
           SET STUDENT_TBL.STUDENT_NAME = #{studentName}, 
               STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
               STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
               STUDENT_TBL.CLASS_ID = #{classEntity.classID} 
         WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; 
    </update> 

    使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。如下示例:

    <!-- 更新学生信息 --> 
    <update id="updateStudent" parameterType="StudentEntity"> 
        UPDATE STUDENT_TBL 
        <set> 
            <if test="studentName!=null and studentName!='' "> 
                STUDENT_TBL.STUDENT_NAME = #{studentName}, 
            </if> 
            <if test="studentSex!=null and studentSex!='' "> 
                STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
            </if> 
            <if test="studentBirthday!=null "> 
                STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
            </if> 
            <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "> 
                STUDENT_TBL.CLASS_ID = #{classEntity.classID} 
            </if> 
        </set> 
        WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; 
    </update> 

    3.2.3 trim
    trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

    where例子的等效trim语句:


    <!-- 查询学生list,like姓名,=性别 --> 
    <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <trim prefix="WHERE" prefixOverrides="AND|OR"> 
            <if test="studentName!=null and studentName!='' "> 
                ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
            </if> 
            <if test="studentSex!= null and studentSex!= '' "> 
                AND ST.STUDENT_SEX = #{studentSex} 
            </if> 
        </trim> 
    </select> 

    set例子的等效trim语句:

    <!-- 更新学生信息 --> 
    <update id="updateStudent" parameterType="StudentEntity"> 
        UPDATE STUDENT_TBL 
        <trim prefix="SET" suffixOverrides=","> 
            <if test="studentName!=null and studentName!='' "> 
                STUDENT_TBL.STUDENT_NAME = #{studentName}, 
            </if> 
            <if test="studentSex!=null and studentSex!='' "> 
                STUDENT_TBL.STUDENT_SEX = #{studentSex}, 
            </if> 
            <if test="studentBirthday!=null "> 
                STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, 
            </if> 
            <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "> 
                STUDENT_TBL.CLASS_ID = #{classEntity.classID} 
            </if> 
        </trim> 
        WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; 
    </update> 

    3.3 choose (when, otherwise)
             有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
             if是与(and)的关系,而choose是或(or)的关系。
             例如下面例子,同样把所有可以限制的条件都写上,方面使用。选择条件顺序,when标签的从上到下的书写顺序:

    <!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose --> 
    <select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap"> 
        SELECT * from STUDENT_TBL ST 
        <where> 
            <choose> 
                <when test="studentName!=null and studentName!='' "> 
                        ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') 
                </when> 
                <when test="studentSex!= null and studentSex!= '' "> 
                        AND ST.STUDENT_SEX = #{studentSex} 
                </when> 
                <when test="studentBirthday!=null"> 
                    AND ST.STUDENT_BIRTHDAY = #{studentBirthday} 
                </when> 
                <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> 
                    AND ST.CLASS_ID = #{classEntity.classID} 
                </when> 
                <otherwise> 
                     
                </otherwise> 
            </choose> 
        </where> 
    </select> 

    3.4 foreach
    对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。
    List 实例将使用“list”做为键,数组实例以“array” 做为键。
    3.4.1参数为list实例的写法:
    SQL写法:
    Xml代码

    <select id="getStudentListByClassIDs" resultMap="studentResultMap"> 
        SELECT * FROM STUDENT_TBL ST 
         WHERE ST.CLASS_ID IN  
         <foreach collection="list" item="classList"  open="(" separator="," close=")"> 
            #{classList} 
         </foreach>    
    </select> 

    接口的方法声明:

    Java代码

    public List<StudentEntity> getStudentListByClassIDs(List<String> classList); 

      测试代码,查询学生中,在20000002、20000003这两个班级的学生:

    Java代码

    List<String> classList = new ArrayList<String>(); 
    classList.add("20000002"); 
    classList.add("20000003"); 
     
    List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList); 
    for( StudentEntity entityTemp : studentList){ 
        System.out.println(entityTemp.toString()); 
    } 


    3.4.2参数为Array实例的写法:
    SQL语句:

    <select id="getStudentListByClassIDs" resultMap="studentResultMap"> 
        SELECT * FROM STUDENT_TBL ST 
         WHERE ST.CLASS_ID IN  
         <foreach collection="array" item="ids"  open="(" separator="," close=")"> 
            #{ids} 
         </foreach> 
    </select>   

    接口的方法声明:
    Java代码

    public List<StudentEntity> getStudentListByClassIDs(String[] ids); 

    测试代码,查询学生中,在20000002、20000003这两个班级的学生:
    Java代码

    String[] ids = new String[2]; 
    ids[0] = "20000002"; 
    ids[1] = "20000003"; 
    List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids); 
    for( StudentEntity entityTemp : studentList){ 
        System.out.println(entityTemp.toString()); 
    } 
  • 相关阅读:
    关于此主题 v1
    从博客园主题了解前端 CSS
    VS2019 许可证到期
    从博客园主题了解前端 HTML
    关于此主题
    从博客园主题了解前端 JS
    GCC 编译器
    Python的Set和List的性能比较 + 两者之间的转换
    wsgi初探(转)
    权限设计概要
  • 原文地址:https://www.cnblogs.com/wxd0108/p/4811917.html
Copyright © 2011-2022 走看看