Mybatis >xml配置

MyBatis,pom依赖

	<packaging>jar</packaging>
	
    <dependencies>
        <dependency>
	        <!-- mysql依赖 -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

        <dependency>
	        <!-- mybatis依赖 -->
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
        	<!-- 单元测试 -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
        	<!-- 日志打印 -->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
    </dependencies>

jdbcConfig.properties (jdbc相关配置,与下面的MyBatisConfig.xml一起使用)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://159.75.xx.xxx:port/dome01?characterEncoding=utf8
jdbc.username=root
jdbc.password=xxxxxxxxx
characterEncoding=utf8 > 设置本机会话所使用的字符集

MyBatisConfig.xml (mybatis的配置文件)

<?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 写法1
        需配置标签中的属性此方法与在environments标签中写属性并没有太大差异
    -->
    <!--<properties>
        <property name="driver" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://159.75.19.211:3306/dome01?characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </properties>-->

    <!--
        properties 写法2
        此方法需以项目的路径为根路径查找属性文件相对简单
    -->
    <properties resource="./jdbcConfig.properties"/>
  

    <!--
        properties 写法3
        需写绝对路径
    -->
    <!--<properties url="file:///jdbcConfig.properties"/>-->
    
    <!-- 设置延迟加载 本内容并未用到懒加载和按需加载 -->
    <settings>
        <!-- 设置懒加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 设置按需加载 -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    <!--
        为实体类配置别名
    -->
    <typeAliases>
        <!-- 写法1 需定位实体类的路径 设置别名-->
        <!--<typeAlias type="cn.yhao.domain.User" alias="user"/>-->

        <!-- 写法2 只需要定位到包 别名为类名 不区分大小写 -->
        <package name="cn.yhao.domain"/>
    </typeAliases>

	<!-- 设置环境 -->
    <environments default="mysql_sql">
        <environment id="mysql_sql">
        	<!-- 设置事务管理器为JDBC -->
            <transactionManager type="JDBC"/>
            <!-- 设置数据源为池 其他值还有UNPOOLED JNDI -->
            <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>
        <!-- 以下为CRUD操作映射配置 此处映射的为接口 -->
        <!--<mapper resource="cn/yhao/dao/I_UserDao.xml"/>-->
        <!-- 此配置为注解配置 -->
        <!--<mapper class="cn.yhao.dao.I_UserDao"/>-->
        <!-- 配置解析的包 需解析的xml文件和接口需目录结构相同 -->
        <package name="cn.yhao.dao"/>
    </mappers>

</configuration>

I_UserDao.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="cn.yhao.dao.I_UserDao">
	<!-- user为需要映射的类,由于在上述的操作中已经配置完别名,因此此处写类名就可以了,不区分大小写 -->
    <resultMap id="mapper_properties" type="user">
    	<!-- property:为实体类的属性   column:为数据库中的列名称-->
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="birthday" column="birthday"/>
        <result property="gender" column="gender"/>
        <result property="address" column="address"/>
    </resultMap>
    
	<!-- findAll 为接口中的一个方法 resultType为返回值的类型 -->
    <select id="findAll" resultType="cn.yhao.domain.User">
        select * from user;
    </select>

	<!-- 当实体类中的属性与数据库所对应的属性不一致时间 会使用结果映射的设置进行映射 -->
    <select id="findAll_01" resultMap="mapper_properties">
        select * from user;
    </select>

    <select id="findAll_02" resultType="user">
        select * from user;
    </select>

    <insert id="saveUser" parameterType="cn.yhao.domain.User">
        insert into user value(#{id},#{username},#{birthday},#{gender},#{address});
    </insert>

    <!--<delete id="deleteUser" parameterMap="">
        delete from user where id = #{id} and #{username};
    </delete>-->
    
    <!--<select id="findByArgument" resultType="user">
        select * from user where 1 = 1
        <if test="username != null">
            and username = #{username}
        </if>
    </select>-->
	<!-- QueryVo 为包装类,里面存在一个范型为Integer的集合 -->
    <select id="findByIds" resultType="user" parameterType="QueryVo">
        select * from user
        <where>
        	<!-- 里面的属性为ids 这里判断集合是否为空,及其长度是否大于0 -->
            <if test="ids != null and ids.size > 0">
            	<!-- 遍历集合内的数据 -->
                <foreach collection="ids" open="and id in (" item="id" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
</mapper>

以下为项目结构

在这里插入图片描述

QueryVo类

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43309893/article/details/119330742