01 mybatis入门案例(mybatis框架CRUD操作)

版权声明:转载需经过作者同意,请尊重原创 https://blog.csdn.net/woshilovetg/article/details/85386005

一、mysql的mapper代理方式实现CRUD

不用写实现类,只需要满足mapper代理的三个规则

1.映射文件中namespace要和Dao接口类全名一致

2.映射文件中statement的id要和Dao接口的方法名称保持一致

3.映射文件要和Dao接口在同一个目录

映射文件中statement的parameterType要和方法的参数一致 

映射文件中statement的resultType要和方法的返回值一致   ------->注意:resultType不管返回的是单个集合还是单个对象,都使用类全名进行设置

mybatis中 #{} 和 ${} 的区别:

    #{} :是占位符

    ${}:字符串拼接

  如果传递的参数是基本类型,基本类型的包装类,Sring

        #{任意值}     ${value}

  如果传递的参数是pojo类型

    #{}和 ${}里面传递的都是pojo的属性名

<?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.itcast.dao.IUserDao">
    <!--配置user类的属性和user表中的列的对应关系(type="user"是在SqlMapConfig中配置别名了,所以可以直接写user)-->
   <!-- <resultMap id="userMap" type="user">
        &lt;!&ndash; 主键字段的对应 &ndash;&gt;
        <id property="userId" column="id"></id>
        &lt;!&ndash;非主键字段的对应&ndash;&gt;
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>-->
    <!--根据id查询用户-->
    <select id="findById" parameterType="java.lang.Integer" resultType="cn.itcast.domain.User">
        select * from user where id = #{id}
    </select>
    <!--保存用户-->
    <insert id="saveUser" parameterType="cn.itcast.domain.User">
        <!--保存成功后查询用户的id-->
        <selectKey keyColumn="id" keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            select last_insert_id();
        </selectKey>
        insert into user (id,username,birthday,sex,address) values (null,#{username},#{birthday},#{sex},#{address})
    </insert>
    <!--更新用户-->
    <update id="updateUser" parameterType="user">
      update user set username = #{username},birthday = #{birthday},sex = #{sex},address = #{address} where id = #{id}
    </update>
    <!--根据id删除用户-->
    <delete id="deleteUser" parameterType="java.lang.Integer">
      delete from user where id = #{id}
    </delete>
    <!--根据姓名模糊查询-->
    <select id="findUserByName" parameterType="java.lang.String" resultType="cn.itcast.domain.User">
        select * from user where username like #{username}
    </select>
    <!--查询名称带 "刚" 的总人数-->
    <select id="findUserByNameCount" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(*) from user where username like #{username}
    </select>
    <!--查询带 "刚"人员信息-->
    <select id="findUserByNames" parameterType="cn.itcast.domain.QueryVo" resultType="cn.itcast.domain.User">
        select * from user where username like #{user.username}
    </select>
    <!--查询所有(改变User类,使User的属性和user表中的列不对应,此时应该用别名来解决)-->
    <!-- <select id="findAll" resultType="cn.itcast.domain.User">
         SELECT
             u.id userId,
             u.username userName,
             u.birthday userBirthday,
             u.address userAddress,
             u.sex userSex
         FROM
         USER u
     </select>-->
    <!--查询所有(改变User类,使User的属性和user表中的列不对应,也可以使用resultMap来解决)-->
    <!--<select id="findAll" resultMap="userMap">
       select * from user
    </select>-->
</mapper>

SqlMapConfig.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 1.可以在标签内部配置连接数据库的信息。
                       2.也可以通过属性引用外部配置文件信息,url属性或者resource属性
                        resource属性: 常用的 用于指定配置文件的位置,是按照类路径的写法来写,并且必须存在于类路径下-->
    <!--<properties url="file:///D:/ideaProject/mybatis02/src/main/resources/db.properties" >--><!--file://这是一种文件协议(windows的)-->
    <properties resource="db.properties">
        <!--标签内部配置数据库连接信息(不推荐)-->
        <!-- <property name="driver" value="com.mysql.jdbc.Driver"></property>
                    <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"></property>
                    <property name="username" value="root"></property> <property name="password"
                    value="1234"></property> -->
    </properties>
    <!--配置别名-->
    <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
    <typeAliases>
        <!--1.typeAlias用于配置别名。type属性指定的是实体类全限定类名。alias属性指定别名,当指定了别名就不再区分大小写-->
        <!--<typeAlias type="cn.itcast.domain.User" alias="user"></typeAlias>-->
        <!-- 用于指定要配置别名的包,当指定之后,该包下的实体类都会注册别名,并且类名就是别名,不再区分大小写 -->
        <package name="cn.itcast.domain"></package>
    </typeAliases>
    <!-- 配置 mybatis 的环境 -->
    <environments default="mysql">
        <environment id="mysql">
            <!-- 配置事务的类型 -->
            <transactionManager type="JDBC"/>
            <!-- 配置连接数据库的信息:用的是数据源(连接池) -->
            <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>
    <!-- 告知 mybatis 映射配置的位置 -->
    <mappers>
        <!--此种方法要求 r mapper  接口名称和 r mapper-->
        <!--<mapper class="cn.itcast.dao.IUserDao"></mapper>-->
        <!--<mapper resource="cn/itcast/dao/IUserDao.xml"></mapper>-->
        <!-- package标签是用于指定dao接口所在的包,当指定了之后就不需要在写mapper以及resource或者class了,大大减少开发时间 -->
        <!--此种方法要求  mar pper  接口名称和 r mapper  映射文件名称相同,且放在同一个目录中-->
        <package name="cn.itcast.dao"></package>
    </mappers>
</configuration>

猜你喜欢

转载自blog.csdn.net/woshilovetg/article/details/85386005