五:Day04_Mybatis02

一、别名 Alias

1. 明确指定别名

通过<typeAlias>标签明确设置类型的别名。

  • type:类型全限定路径。

  • alias:别名名称。

指定别名方式优点和缺点:

  • 优点:别名可以设置为单个字母,以后使用别名时比较方便。

  • 缺点:当需要定义别名的类比较多时,定义别名是个较大工程量。

小提示:

1. 如果需要给多个类型定义别名,需要编写多个<typeAlias>标签。

2. 如果需要给一个类定义多个别名,需要编写多个<typeAlias>标签。

 在mybatis.cfg.xml全局配置文件中,添加下面配置

<?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>
    <properties resource="db.properties"></properties>

    <!-- 按照DTD顺序要求:typeAliases放在properties下面,environments上面 -->
    <typeAliases>
        <!-- type:类型全限定路径   alias:别名名称 -->
        <typeAlias type="com.bjsxt.pojo.People" alias="p"></typeAlias>
        <typeAlias type="com.bjsxt.pojo.People" alias="p2"></typeAlias>
    </typeAliases>

    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/suiyi.xml"/>
    </mappers>
</configuration>

别名定义后,可以在resultType中使用别名,类全限定路径方式也可以使用。

<?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="a.b.c">

    <select id="list" resultType="p">
        select * from people
    </select>
    <select id="list2" resultType="p2">
        select * from people
    </select>

</mapper>
实体类定义别名,sql返回结果类型中可以指定为别名
注意事项:
    1、typeAlias:
        1.一个类可以指定多个别名
        2.使用别名时,不区分大小写
    2、package:
        1.别名为类名
        2.使用别名时,不区分大小写

2. 指定包中所有类的别名

当类个数较多时,明确指定别名工作量较大,可以通过<package>标签指定包下全部类的别名。

指定后所有类的别名就是类名。

<?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>
    <properties resource="db.properties"></properties>

    <typeAliases>
        <!-- 指定包 -->
        <package name="com.bjsxt.pojo"/>
    </typeAliases>

    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/suiyi.xml"/>
    </mappers>
</configuration>

3.总结

实体类定义别名,sql返回结果类型中可以指定为别名

注意事项:

  1、typeAlias:

        1.一个类可以指定多个别名

        2.使用别名时,不区分大小写

  2、package:

        1.别名为类名

        2.使用别名时,不区分大小写

4. MyBatis内置别名

MyBatis框架中内置了一些常见类型的别名。这些别名不需要配置

别名映射的类型别名映射的类型别名映射的类型
_bytebytestringStringdateDate
_longlongbyteBytedecimalBigDecimal
_shortshortlongLongbigdecimalBigDecimal
_intintshortShortobjectObject
_integerintintIntegermapMap
_doubledoubleintegerIntegerhashmapHashMap
_floatfloatdoubleDoublelistList
_booleanbooleanfloatFloatarraylistArrayList
booleanBooleancollectionCollection
iteratorIterator

二、SQL查询结果填充的方式(面试题)

1. 介绍

MyBatis会根据句映射关系把查询到的结果填充到指定结果集类型中。支持方式:

  • auto mapping:自动映射。当列名或列的别名与实体类属性名相同时不需要做额外配置。

  • resultMap:手动定义映射关系。

  • camel case:驼峰命名规则。

2. Auto Mapping(数据库中列名和类的属性名)

自定映射方式就是前面所讲的方式,只要保证数据库中列名和属性名相同,就可以自动进行映射。

也可以给列起别名,让别名和属性名对应。对应时不用区分大小写。

3. resultMap(在mapper配置文件中配置resultMap )

当数据库列名和属性名不同时是无法进行自动映射的,这时需要手动指定映射关系。

3.1修改mapper映射文件

在映射文件中需要注意的是<select>标签不再使用resultType属性,而是使用resultMap属性。resultMap属性表示使用哪个<resultMap>作为结果映射配置.

  • <resultMap>标签的子标签的property(类的属性名)属性必须和类的属性名严格对应,区分大小写。如果没有对应上,会出现反射异常。
  • 数据库查询结果填充到对应属性中优先使用setter方法,如果没有setter方法,直接对属性进行赋值。
  • column(数据库列名)属性对应数据库列名,由于数据库是不区分大小写的,所以column的值也是不区分大小写的。
  • resultMap可以存在多个。只要id属性不重名就可以。如果出现resultMap的id重名会出现异常
<?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="a.b.c">
    <!-- type:数据库中每行数据对应的实体类型,支持别名 -->
    <!-- id:自定义名称-->
    <resultMap id="myid" type="People2">
        <!-- id标签定义主键列和属性的映射关系关系 -->
        <!-- property 类中属性名称,区分大小写-->
        <!-- column 表中列名-->
        <id property="peoId" column="peo_id"/>
        <!-- result标签定义非主键列和属性的映射关系-->
        <result property="peoName" column="peo_name"/>
    </resultMap>
    <!-- resultMap的值必须和resultMap的id相同 -->
    <select id="myid" resultMap="myid">
        select * from tb_people
    </select>
</mapper>

4. 驼峰转换

  • MySQL中列名命名规范是xxx_yyy,多个单词之间使用下划线进行分割。在Java中属性命名规范是xxxYyy,小驼峰的方式进行命名。
  • MyBatis发现了这个问题,提供了驼峰转换的能力。通过全局配置文件开启驼峰转换功能后,就可以让xxx_yyy自动映射到xxxYyy上。例如:列名叫做peo_id,可以自动映射到peoId的属性上。转换时去掉列中的下划线,把下划线后面单词首字母变大写。
4.1 修改全局配置文件

在全局配置文件中添加设置,开启驼峰转换功能。

 <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
 </settings>

<?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>
    <properties resource="db.properties"></properties>
    <!-- 按照DTD顺序要求,settings需要定义在typeAliases上面,properties标签下面 -->
    <settings>
        <!-- 开启驼峰转换 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <typeAliases>
        <typeAlias type="com.bjsxt.pojo.People" alias="p"></typeAlias>
        <typeAlias type="com.bjsxt.pojo.People" alias="p2"></typeAlias>
        <package name="com.bjsxt.pojo"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.username}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/suiyi.xml"/>
    </mappers>
</configuration>
4.2 修改映射文件

只要是自动映射的,都直接使用resultType属性进行配置。

三、接口绑定方案(配置重要)

1. 接口绑定方案存在的意义

前面所学的方式都是通过SqlSession的方法调用指定的一个statement。在学习MyBatis之前,我们的习惯都是创建Dao对象,通过一个对象调用类中多个方法。这样的好处是一次创建,多次调用。显然要比前面所学的方式用起来更加方便。

MyBatis提供了也提供了一种接口绑定方案,通过SqlSession的getMapper方法产生接口的动态代理对象。然后通过对象调用接口中提供的功能。

2. 完整项目流程

数据库以ssm数据库的people为例。

2.1 创建项目并配置pom.xml

创建项目,命名为mybatis2。并配置pom.xml,配置的内容和之前一样,只需要导入MyBatis框架依赖和数据库驱动。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bjsxt</groupId>
    <artifactId>mybatis2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- MyBatis 框架依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
    </dependencies>
</project>
2.2 创建全局配置文件

在resources中创建mybatis.cfg.xml。

配置文件中保留了别名的配置,通过<mapper>的子标签换成了<package>标签

<?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>
        <!-- 别名 -->
        <package name="com.bjsxt.pojo"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true"/>
                <property name="username" value="root"/>
                <property name="password" value="smallming"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <!-- 此处换为package,里面写接口和映射文件所在的包 -->
        <package name="com.bjsxt.mapper"/>
    </mappers>


</configuration>
2.3 创建实体类

在src/main/java下新建com.bjsxt.pojo.People实体类。

package com.bjsxt.pojo;

public class People {
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Peope{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
2.3 创建接口

接口绑定方案要有接口,接口中编写访问数据库的方法声明。

按照MyBatis的规范,接口应该放在mapper包中。每个接口中编写对某个表的访问,且接口名称以Mapper结尾

所以:创建com.bjsxt.mapper.PeopleMapper接口

package com.bjsxt.mapper;


import com.bjsxt.pojo.People;

import java.util.List;

public interface PeopleMapper {
    List<People> selectAll();
}
2.4 创建映射文件

在接口绑定方案中,对于映射文件有几点强制要求:

  1. 映射文件和接口需要在同一个包中

  2. 映射文件名称要和接口名称相同

  3. namespace取值必须是接口的全限定路径

  4. id属性值必须和方法名对应

  5. resultType必须和方法返回值类型对应。如果方法返回值是集合类型,resultType中写泛型的类型

如果出现映射文件名和接口名不一致、namespace和接口全限定路径不一致、方法名和id值不一致、未导入资源拷贝插件这四种情况中的任意一种。会提示的异常信息。

根据上面重要提示的要求,在com.bjsxt.mapper下新建PeopleMapper.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="com.bjsxt.mapper.PeopleMapper">

    <select id="selectAll" resultType="People">
        select * from people
    </select>

</mapper>
2.5 添加资源拷贝插件(非必需)

如果映射文件添加到了PeopleMapper接口所在的包中,所以需要添加资源拷贝插件,保证映射文件能被编译。

资源拷贝插件配置的主要目的是为了让src/main/java下映射文件能被编译时包含。

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
2.6 编写测试类

如何调用来进行数据库的操作。


public class Test {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis.cfg.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = factory.openSession();
        // getMapper方法采用了动态代理模式,创建接口的代理对象
        PeopleMapper peopleMapper = session.getMapper(PeopleMapper.class);
        List<People> list = peopleMapper.selectAll();
        System.out.println(list);
        session.close();
    }
}

四、接口绑定方案下参数传递

  • 接口绑定方式其底层调用的就是前面学习的SqlSession的相关方法。
  • 接口中方法对应以前测试类中直接调用的SqlSession中的方法。
  • 对于映射文件的使用还是和之前学习的相同。

在接口绑定方案中唯一需要重点注意的是:

         当方法有多个参数将使用Map集合来存储数据,之后将Map集合对象当做参数传入底层的方法。

Map类型当做参数时,在映射文件中通过Map的key进行获取value值。 Mybatis会自动创建Map的key:

  1. 如果接口中方法没有使用注解定义名称,MyBatis使用内置名称作为key。

    规则:arg0、arg1、argM(M为从0开始的数字,和方法参数顺序对应)或param1、param2、paramN(N为从1开始的数字,和方法参数顺序对应)。

  2. 也可以在接口方法参数中通过@Param("key名称")的形式进行定义key。一定使用了注解argN的这种形式就不能使用了,但是paramN的方式还是可以使用。

  3. 接口中方法的参数前可以加注解,也可以不加注解,也可以部分加注解部分不加注解。

  4. 当传入的参数是引用类型时,可以通过arg0、param1两种方式获取到。arg0.属性名、param1.属性名  获取对象的属性。

五、主键回填

MyBatis中有两种方式可以获取到自增主键的值:

  • 使用<selectKey>子标签编写SQL进行回填属性。

  • 使用<select>的useGeneratedKeys属性值进行自动回填。

1 .在映射文件中添加标签

在映射文件中添加标签与接口中方法进行绑定。

<selectKey>标签是<insert>的子标签,作用:把查询到的结果填充进行回填。

keyProperty               :接口方法参数中,对象的哪个属性需要进行回填。

resultType                  :SQL查询到的结果类型。

select @@identity     :是MySQL内置的全局变量表示获取到自增主键值。

order                          :selectKey中SQL是在添加SQL执行之前还是之后执行。                                                       (可取值:AFTER和BEFORE)

<insert id="insert1">
    <selectKey keyProperty="id" resultType="int">
        select @@identity
    </selectKey>
    insert into people values(default,#{name},#{address})
</insert>

2. 自动主键回填

MyBatis的映射文件的<insert>标签带有自动主键回填功能,只需要设置useGeneratedKeys进行开启自动主键回填功能,同时设置keyProperty的值需要回填到对象的哪个属性。

<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
    insert into people values(default,#{name},#{address})
</insert>

六、动态SQL(重要)

MyBatis中动态SQL是编写在mapper.xml中的,其语法和JSTL类似,但是却是基于强大的OGNL表达式实现的。

1. if标签

通过if处理用户多变的查询条件。类似于:

if(){}
if(){}

<if>标签的test属性值为OGNL(对象导航图语言)表达式,通过对象属性名可以快速获取到对象属性值。

<select id="selectIf" resultType="People">
    select * from people where 1=1
    <if test="name!=null">
        and name=#{name}
    </if>
    <if test="address!=null">
        and address=#{address}
    </if>
</select>

代码解释说明:

name!=null : OGNL 表达式,直接写属性名可以获取到属性值。不需要添加${}或#{}。

name=#{name} 中name是表中的列名。#{name}是MyBatis获取参数对象属性值的写法。

2. choose

  • choose标签相当于Java中的if...else if....else。
  • 在choose标签里面可以有多个when标签和一个otherwise(可以省略)标签。
  • 只要里面有一个when成立了后面的when和otherwise就不执行了。
<select id="selectIf" resultType="People">
    select * from people where 1=1
    <choose>
        <when test="name!=null">
            and name=#{name}
        </when>
        <when test="address!=null">
            and address=#{address}
        </when>
        <otherwise>
            // do something
        </otherwise>
    </choose>
</select>

3. trim标签

trim标签包含四个属性:

  • prefix:子内容不是空字符串(""),就在子内容前面添加特定字符串。
  • prefixOverrides:子内容是以某个内容开头,去掉这个内容。
  • suffix:子内容不是空字符串(""),就在子内容后面添加特定字符串。
  • suffixOverrides:子内容以某个内容结尾,就去掉这个内容。
<select id="selectIf" resultType="People">
    select * from people
    <trim prefix="where" prefixOverrides="and">
        <if test="name!=null">
            and name=#{name}
        </if>
        <if test="address!=null">
            and address=#{address}
        </if>
    </trim>
</select>

trim作为很多其它标签的底层。无论是开头操作还是结尾的操作,都是先去掉容,后添加。

trim只会对里面的子内容进行操作。如果子内容为空则不进行任何操作。后添加的内容会有空格。

特例:

如果内部字符串为要去掉的字符串,去掉后认为内容不为空,prefix依然添加。

4. where标签

where标签属于trim标签的简化版,被where标签包含的内容具备:

  • 如果里面内容不为空串,在里面内容最前面添加where。

  • 如果里面内容是以and开头,去掉最前面的and。

<select id="selectIf" resultType="People">
    select * from people
    <where>
        <if test="name!=null">
            and name=#{name}
        </if>
        <if test="address!=null">
            and address=#{address}
        </if>
    </where>
</select>

5. set标签

set标签是专门用在修改SQL中的,属于trim的简化版,带有下面功能:

  • 如果子内容不为空串,在最前面添加set。

  • 去掉最后一个逗号。

<update id="update">
    update people
    <set>
        <if test="name!=null">
            name=#{name},
        </if>
        <if test="address!=null">
            address=#{address},
        </if>
        id=#{id}
    </set>
    where id = #{id}
</update>

6. foreach标签

foreach标签表示循环,主要用在in查询或批量新增的情况。

<select id="selectByIds" resultType="People">
    select * from people where id in
    <foreach collection="array" open="(" close=")" item="id" separator=",">
        #{id}
    </foreach>
</select>

foreach标签的属性解释说明:

  • collection:要遍历的数组或集合对象。(如果参数没有使用@Param注解:arg0或array或list。. 如果使用@Param注解,使用注解的名称或param1。 )
  • open:遍历结束在前面添加的字符串。
  • close:遍历结束在后面添加的字符串。
  • item:迭代变量。在foreach标签里面#{迭代变量}获取到循环过程中迭代变量的值。
  • separator:分隔符。在每次循环中间添加的分割字符串。
  • index:迭代的索引。从0开始的数字。
  • nullable:是否允许数组或集合对象为null。如果设置为true,表示集合或数组允许为null。如果设置为false表示不允许数组或集合对象为null,一旦为null会出现异常。

7. bind标签(拼接使用)

bind标签表示对传递进来的参数重新赋值。最多的使用场景为模糊查询。通过bind可以不用在Java代码中对属性添加%。

<select id="selectLike" resultType="People">
    <bind name="name" value="'%'+name+'%'"/>
    select * from people where name like #{name}
</select>

8. sql和include标签

MyBatis的sql标签用于定义SQL片段,include标签用于引用sql标签定义的片段。

<sql id="mysqlpart">
    id,name,address
</sql>
<select id="selectSQL" resultType="People">
    select <include refid="mysqlpart"></include> from people
</select>

七、MyBatis中常用注解

 MyBatis的注解通过全局配置文件<mappers>进行加载。

  • 如果一个Mapper接口中所有方法都使用注解定义SQL,可以在全局文件中配置。

  • 如果一个Mapper接口中既有注解又在mapper.xml定义SQL。可以在全局配置文件中通过<package>进行加载。这种方式和之前的接口绑定方案的配置是一样的。也就是说MyBatis在扫描这个包的时候就可以加载到注解。

在MyBatis中注解都是写在Mapper接口的方法上中,所有的注解都在org.apache.ibatis.annotations包中,常见注解:

注解解释
@Select查询
@Insert新增
@Delete删除
@Update修改
@SelectKey主键回填
@SelectProvider调用SQL构建器。查询专用
@DeleteProvider调用SQL构建器。删除专用
@UpdateProvider调用SQL构建器。修改专用
@INSERTProvider调用SQL构建器。删除专用
@Param定义参数的名称

1. CURD操作演示

简单的CURD操作直接在方法上添加对应类型的注解即可

public interface PeopleMapper {
    @Select("select * from people")
    List<People> selectAll();
    @Insert("insert into people values(default,#{name},#{address})")
    int insert(People peo);
    @Delete("delete from people where id=#{id}")
    int deleteById(int id);
    @Update("update people set name=#{name},address=#{address} where id=#{id}")
    int updateById(People peo);
}

2. 主键回填(注解实现)

使用注解时,主键回填需要通过@SelectKey注解。

该注解中:必有属性

  • keyProperty:表示要回填的属性名。
  • statement:要执行的sql。
  • before:表示是否在@Insert的SQL之前执行。
  • resultType:回填的属性类型。
@Insert("insert into people values(default,#{name},#{address})")
@SelectKey(keyProperty = "id",statement = "select @@identity",before = false,resultType = Integer.class)
int insert2(People people);

3. SQL构建器(Provider)

MyBatis的SQL构建器赋予了程序员在Java类中编写SQL的方式。把以前写在注解参数中的复杂SQL转移到了类的方法中进行书写。

先在方法上进行注解: 

@SelectProvider(type = MySQLProvider.class,method = "selectprovider")
List<People> select(People peo);

然后编写相应的类:

 1.直接写SQL方式代码演示:

//返回SQL的方法必须是public String的。
public class MySQLProvider {
    public String selectprovider(){
        return "select *" +
                " from people" +
                " where name=#{name} and address like #{address}" +
                " order by id desc";
    }
}

上面SQL看起来写的不是特别费劲,但是一定要注意关键字前后都有空格。下面代码每行前面都有空格,其实这点非常不友好。  

2.使用SQL类:

MyBatis提供了SQL类,该类中封装了很多方法,方法名称和SQL的关键字名称正好对应。

里面需要注意的点:

  • 如果多个条件可以放在一个where中,也可以放在多个连续where中(放在多个where方法中不需要and关键字)。
  • 最终需要调用toString()转换为字符串。
public String selectprovider(){
        return new SQL()
                .SELECT("*")
                .FROM("people")
                .WHERE("name=#{name}")
                // 没有and,多个并列条件使用WHERE方法
                .WHERE("address like #{address}")
                .ORDER_BY("id desc")
                .toString();
    }

4.使用注解进行结果映射

@Results的value属性中写多个@Result。

@Result注解:

  • column:数据库列
  • property:属性名
  • id:是否为主键,默认false
  @Results(value = {
            @Result(column = "peo_id",property = "id",id = true),
            @Result(column = "peo_name",property = "name")
    })
    @Select("select * from tb_people where peo_name=#{name}")
    List<People> select2(People peo);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值