IDEA中设置MyBatis核心配置文件以及MyBatis-mapper映射文件模板

121 阅读1分钟

操作顺序:

image.png

image.png

MyBatis核心配置文件 (mybatis-config)

<?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核心配置文件中,标签的顺序:
    properties?,settings?,typeAliases?,typeHandlers?,
    objectFactory?,objectWrapperFactory?,reflectorFactory?,
    plugins?,environments?,databaseIdProvider?,mappers?
    -->

    <!--读取外部properties配置文件-->
    <!--引入properties文件-->
    <properties resource="jdbc.properties"></properties>

    <!--别名扫描的包路径-->
    <!--设置类型别名-->
    <typeAliases>
        <!--
            typeAlias:设置某个类型的别名
            属性:
            type:设置需要设置别名的类型
            alias:设置某个类型的别名,若不设置该属性,那么该类型拥有默认的别名,即类名
            且不区分大小写
            -->
        <!--<typeAlias type="com.atguigu.mybatis.pojo.User"></typeAlias>-->
        <!--<typeAlias type="com.atguigu.mybatis.pojo.User" alias="abc"></typeAlias>-->
        <!--以包为单位,将包下所有的类型设置默认的类型别名,即类名且不区分大小写-->
        <package name="com.atguigu.mybatis.pojo"/>
    </typeAliases>
    <!--设置连接数据库的环境-->
    <!--
        environments:配置多个连接数据库的环境
        属性:
        default:设置默认使用的环境的id
    -->
    <environments default="development">
        <!--
            environment:配置某个具体的环境
            属性:
            id:表示连接数据库的环境的唯一标识,不能重复
        -->
        <environment id="development">
            <!--
                environment:配置某个具体的环境
                属性:
                id:表示连接数据库的环境的唯一标识,不能重复
            -->

            <transactionManager type="JDBC"/>
                <!--
                dataSource:配置数据源
                    属性:
                        type:设置数据源的类型
                        type="POOLED|UNPOOLED|JNDI"
                        POOLED:表示使用数据库连接池缓存数据库连接
                        UNPOOLED:表示不使用数据库连接池
                        JNDI:表示使用上下文中的数据源
                -->
            <dataSource type="POOLED">
                <!--设置连接数据库的驱动-->
                <property name="driver" value="${jdbc.driver}"></property>
                <!--设置连接数据库的连接地址-->
                <property name="url" value="${jdbc.url}"></property>
                <!--设置连接数据库的用户名-->
                <property name="username" value="${jdbc.username}"></property>
                <!--设置连接数据库的密码-->
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!--引入映射文件-->
    <mappers>

        <!--<mapper resource="mappers/UserMapper.xml"/>-->
        <!--
        以包为单位引入映射文件
            要求:
                1、mapper接口所在的包要和映射文件所在的包一致
                2、mapper接口要和映射文件的名字一致
        -->
        <!--<package name="mappers/UserMapper.xml/"/>-->
        <package name="com.atguigu.mybatis.mapper"/>
    </mappers>
</configuration>

MyBatis-mapper映射文件(mybatis-mapper)

<?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.atguigu.mybatis.mapper.UserMapper"><!--映射接口的全类名-->

    <!--YJM
        mapper接口和映射文件要保证两个一致:
        1、mapper接口的全类名和映射文件的namespace一致
        2、mapper接口中的方法的方法名要和映射文件中的sql的id保持一致
    -->

    <!--int insertUser();-->
    <insert id="insertUser">
        insert into t_user values(null,'admin','123456',23,'男','12345@qq.com')
    </insert>

    <!--void updateUser();-->
    <update id="updateUser">
        update t_user set username='root',password='123' where id = 3
    </update>

    <!--void deleteUser();-->
    <delete id="deleteUser"><!--id等于mapper接口中的方法名-->
        delete from t_user where id = 3
    </delete>

    <!--User getUserById();-->
    <!--
        resultType:设置结果类型,即查询的数据要转换为的java类型
        resultMap:自定义映射,处理多对一或一对多的映射关系
    -->
    <select id="getUserById" resultType="com.atguigu.mybatis.pojo.User">
        select * from t_user where id = 3
    </select>

    <!--List<User> getAllUser();-->
    <select id="getAllUser" resultType="com.atguigu.mybatis.pojo.User">
        select * from t_user
    </select>

<!--
注意:
1、查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射关系
resultType:自动映射,用于属性名和表中字段名一致的情况
resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况

-->

</mapper>