Mybatis 系列5-配置说明

配置文件说明

一、SqlMapConfig.xml

<configuration>
    <!--配置properties
        可以在标签内部配置连接数据库的信息,也可以通过属性引用外部配置文件信息
        resource属性:常用的
            用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
        url属性:
            是要求按照url的写法来写地址
    -->
    <properties resource="jdbcConfig.properties" ></properties>
    
    <!--配置参数  开启Mybatis支持延迟加载-->
    <settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"></setting>
    </settings>

    <!--使用typeAliases配置别名,它只能配置domain中类的别名-->
    <typeAliases>
        <!--typeAlias用于配置别名,type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就不区分大小写了-->
<!--        <typeAlias type="com.mantishell.domain.User" alias="user"/>-->
        <!--由于配置typeAlias比较麻烦,所以有简化方式 package
            package:用于指定要配置别名的包,当指定后,该包下的实体类都会注册别名,并且类名就是别名,不区分大小写
        -->

        <package name="com.mantishell.domain"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/mantishell/dao/IUserDao.xml" />
        <!--package标签是用于指定dao接口所在的包,当指定后,就不需要写mapper、resource或class了-->
        <!--<package name="com.mantishell.dao"/>-->
    </mappers>
</configuration>

properties配置

可以在标签内部配置连接数据库的信息,也可以通过属性引用外部配置文件信息

在标签内部配置连接数据库的信息

<configuration>
    <properties>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db2"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </properties>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
    </mappers>
</configuration>

通过属性引用外部配置文件信息

<configuration>
    <!--配置properties
        resource属性:常用
            用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下
        url属性:
            是要求按照url的写法来写地址
    -->
    <properties resource="jdbcConfig.properties" ></properties>

    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
    </mappers>
</configuration>

typeAliases(类型别名)

使用别名后,持久层接口的配置文件里的返回类型不需要再写完全限定名了。
比如: <select id="findAll" resultType="User">

<configuration>
    <properties resource="jdbcConfig.properties" ></properties>
    <!--使用typeAliases配置别名,它只能配置domain中类的别名-->
    <typeAliases>
        <!--typeAlias用于配置别名,type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就不区分大小写了-->
<!--        <typeAlias type="com.mantishell.domain.User" alias="user"/>-->
        <!--由于配置typeAlias比较麻烦,所以有简化方式 package
            package:用于指定要配置别名的包,当指定后,该包下的实体类都会注册别名,并且类名就是别名,不区分大小写
        -->
        <package name="com.mantishell.domain"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
    </mappers>
</configuration>

mappers(映射器)

    <mappers>
        <!--方式1、配置文件时使用-->
        <mapper resource="com/mantishell/dao/IUserDao.xml" />
        <!--方式2、使用注解时使用-->
        <!--<mapper class="com.mantishell.dao.IUserDao" />-->
        <!--方式3、都可以使用。package标签是用于指定dao接口所在的包,当指定后,就不需要写mapper、resource或class了-->
        <!--<package name="com.mantishell.dao"/>-->
    </mappers>

二、IUserDao.xml

输出映射resultType

期望从这条语句中返回结果的类全限定名或别名。注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。resultType和resultMap之间只能使用一个。

输出简单类型

配置:

<!-- 查询总记录条数 -->
    <select id="findTotal" resultType="int">
        select count(*) from user;
    </select>

接口:

// 查询总记录数
int queryUserCount();

测试:

//查询用户总数
@Test
public void testQueryUserCount(){
    int count = userdao.queryUserCount();
    System.out.println(count);
}

输出pojo对象

配置:

    <select id="findById" parameterType="int" resultType="com.mantishell.domain.User">
        select * from user where id=#{userid}
    </select>

接口:

    User findById(Integer userId);

测试:

    @Test
    public void testFindById(){
        User user = userDao.findById(3);
        System.out.println(user);
    }

输出pojo集合

配置:

    <select id="findAll" resultType="com.mantishell.domain.User">
        select * from user;
    </select>

接口:

    List<User> findAll();

测试:

    @Test
    public void testFindAll() {
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

如果我们插入数据的同时想要获取自增长的id怎么办?
配置:

    <insert id="saveUser" parameterType="com.mantishell.domain.User">
        <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user(name,address,sex) values(#{name},#{address},#{sex});
    </insert>

接口:

    void saveUser(User user);

测试:

    @Test
    public void testSave(){
        User user = new User();
        user.setName("李四");
        user.setAddress("江苏省南京");
        user.setSex("男");
        user.setBirthday(new Date());
        System.out.println("Before Insert" + user);
        userDao.saveUser(user);
        System.out.println("After Insert" + user);
    }

resultMap

Mybatis使用ognl表达式解析对象字段的值、#{}或${}括号中的值为pojo属性名称。
OGNL表达式:Object Graphic Navigation Language。对象 图 导航 语言
它是通过对象的取值方法来获取数据,比如user.username。在写法上把get给省略了。
比如:我们获取用户的名称
类中的写法:user.getUsername();
OGNL表达式写法:user.username;
mybatis中为什么能直接写username,而不用user呢。因为在parameterType中已经提供了属性所属的类,所以此时不需要写对象名。

使用resultMap,配置查询结果的列名和实体类的属性名的对应关系

<?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.mantishell.dao.IUserDao">
    <!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
    <resultMap id="userMap" type="com.mantishell.domain.User">
        <!-- 主键字段的对应 -->
        <id property="userId" column="id" />
        <!--非主键字段的对应-->
        <result property="name" column="username" />
        <result property="address" column="address" />
        <result property="sex" column="sex" />
        <result property="birthday" column="birthday" />
    </resultMap>
    <!-- 查询所有,使用resultMap使映射生效 -->
    <select id="findAll" resultMap="userMap">
        select * from user;
    </select>
</mapper>

parameterType输入映射

简单类型

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

pojo对象

Mybatis使用ognl表达式解析对象字段的值、#{}或${}括号中的值为pojo属性名称。
OGNL表达式:Object Graphic Navigation Language。对象 图 导航 语言
它是通过对象的取值方法来获取数据,比如user.username。在写法上把get给省略了。
比如:我们获取用户的名称
类中的写法:user.getUsername();
OGNL表达式写法:user.username;
mybatis中为什么能直接写username,而不用user呢。因为在parameterType中已经提供了属性所属的类,所以此时不需要写对象名。

配置:

    <!--根据实体类对象查询-->
    <select id="findUserByVo" parameterType="com.mantishell.domain.QueryVo" resultType="com.mantishell.domain.User">
        select * from user where name like #{user.name}
    </select>

接口:

    List<User> findUserByVo(QueryVo vo);

测试:

    @Test
    public void testFindByVo(){
        QueryVo vo = new QueryVo();
        User user = new User();
        user.setName("李%");
        vo.setUser(user);
        List<User> users = userDao.findUserByVo(vo);
        for (User u : users) {
            System.out.println(u);
        }
    }
原文地址:https://www.cnblogs.com/mantishell/p/12503518.html