Mybatis入门到精通系列

03Mybatis参数深入了解

2020-04-24  本文已影响0人  better_future

已经介绍了 SQL 语句传参,使用标签的 parameterType 属性来设定。该属性的取值可以是基本类型,引用类型(例如:String 类型),还可以是实体类类型(POJO 类)。同时也可以使用实体类的包装类,本章节将介绍如何使用实体类的包装类作为参数传递。

注意
基本类型和 String 我们可以直接写类型名称 ,也可以使用包名 . 类名的方式 ,例如 :java.lang.String。

实体类类型,目前我们只能使用全限定类名。

究其原因,是 mybaits 在加载时已经把常用的数据类型注册了别名,从而我们在使用时可以不写包名,而我们的是实体类并没有注册别名,所以必须写全限定类名。

传递Pojo包装对象,即对pojo对象进行再次封装。

//对user对象进行再次的封装
public class QueryVo  implements Serializable {
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

直接通过一个封装了User的vo对象去查询User

List<User> findByVo(QueryVo vo);
<select id="findByVo" resultType="com.it.pojo.User" parameterType="com.it.vo.QueryVo">
            select * from user where username like #{user.username}
        </select>

注意:此时的参数类型是QueryVo,里面封装了一个user属性,那么 #{user.username}就会拿到这个属性的username

@Test
    public void testFindByVo(){
        QueryVo queryVo = new QueryVo();
        User user = new User();
        user.setUsername("%王%");
        queryVo.setUser(user);
        List<User> users = userDao.findByVo(queryVo);
        for(User u:users){
            System.out.println(u);
        }
    }

Mybatis 输出结果的封装

一、resultType 配置结果类型

resultType 属性可以指定结果集的类型,它支持基本类型实体类类型。我们在前面的 CRUD 案例中已经对此属性进行过应用了。

需要注意的是,它和 parameterType 一样,如果注册过类型别名的,可以直接使用别名。没有注册过的必须使用全限定类名。

例如:我们的实体类此时必须是全限定类名(今天最后一个章节会讲解如何配置实体类的别名)
同时,还有一个要求,实体类中的属性名称必须和查询语句中的列名保持一致,否则无法实现封装。

1、基本类型示例
<!-- 查询总记录条数 -->
<select id="findTotal" resultType="int">
select count(*) from user;
</select>
2、实体类型示例
<select id="findAll" resultType="com.itheima.domain.User">
select * from user
</select>
3、 特殊情况示例(实体类的属性名称与数据列名不一致)
public class User implements Serializable {
private Integer userId;
private String userName;
private Date userBirthday;
private String userSex;
private String userAddress;
//gettter setter
<select id="findAll" resultType="com.itheima.domain.User">
select * from user
</select>

能否查到呢?


image.png

为什么可以查到?
因为mysql在windows中是不区分大小写的

那如果不是在windows中呢?
使用别名查询

<!-- 配置查询所有操作 -->
<select id="findAll" resultType="com.itheima.domain.User">
select id as userId,username as userName,birthday as userBirthday,
sex as userSex,address as userAddress from user
</select>

id as userId id是数据库中的字段,userId是实体类中的字段

如果实体类的属性很多写起来很麻烦怎么办?

二、resultMap 配置结果类型

resultMap 标签可以建立查询的列名和实体类的属性名称不一致时建立对应关系。从而实现封装。

在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询。

在mapper的标签内定义映射规则

<resultMap type="com.it.pojo.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="address" property="userAddress"/>
<result column="birthday" property="userBirthday"/>
</resultMap>

id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称

使用自定义映射 resultMap="userMap"

<select id="findAll" resultMap="userMap">
select * from user
</select>

Mybaits传统DAO层开发

SqlMapConfig.xml配置文件的内容和顺序

image.png
<?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>
    <!-- 配置 mybatis 的环境 -->
    <environments default="mysql">
        <!-- 配置 mysql 的环境 -->
        <environment id="mysql">
            <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 配置连接数据库的信息:用的是数据源(连接池) -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
                <property name="username" value="root"/>
                <property name="password" value="110811"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 告知 mybatis 映射配置的位置 -->
    <mappers>
        <mapper resource="com/it/dao/UserDao.xml"/>
    </mappers>

   <!-- <mappers>
        <mapper class="com.it.dao.UserDao"></mapper>
    </mappers>-->
</configuration>
properties的配置方式一 内部配置
image.png
properties的配置方式二

使用属性引用外部文件 jdbcConfig.properties 属性配置

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy
jdbc.username=root
jdbc.password=1234
image.png
properties的 url 和 resource 属性有什么区别

resource 常用的,用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下

url 是按照url 统一资源定位符来写的 http://localhost:8080/ 也可以使用file协议的文件路径来写, 注意不要使用中文路径,或导致乱码

同理mapper的url属与properties的是一样的

typeAliases (类型别名)
在 SqlMapConfig.xml 中配置:
<typeAliases>
<!-- 单个别名定义 区分大小写 -->
<typeAlias alias="user" type="com.it.pojo.User"/>
<!-- 批量别名定义,扫描整个包下的类,别名为类名(首字母大写或小写都可以) -->
<package name="com.it.pojo"/>
<package name=" 其它包 "/>
</typeAliases>
SqlMapConfig.xml 中的package标签
image.png
注意:这里要保证,实体类与映射文件的路径都是相同的:com.it.dao下面,一个是java下的路径,一个是resource下的路径
上一篇 下一篇

猜你喜欢

热点阅读