mybatis03

Mybatis基于代理Dao的CRUD操作

使用代理模式开发时,不需要写实现类,只需要说明接口并在xml配置文件中写明,只需要通过代理类来调用方法

 IUserDao.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.edu.hznu.dao.IUserDao">
    <!--配置属性列名与查询的结果集对应关系-->
    <resultMap id="userMap" type="User">
        <!--主键字段-->
        <id property="id" column="id"></id>
        <!--非主键字段-->
        <result property="username" column="username"></result>
        <result property="address" column="address"></result>
        <result property="sex" column="sex"></result>
        <result property="birthday" column="birthday"></result>
    </resultMap>


    <!--    配置查询索引-->
    <select id="findAll" resultMap="userMap"><!--将resulttype改为resultMap-->
        select * from user
    </select>
    <!--配置添加用户 获取id-->
    <insert id="addUser" parameterType="cn.edu.hznu.domain.User">
        <selectKey keyProperty="id" keyColumn="id" resultType="java.lang.Integer" order="AFTER">
            select last_insert_id()
        </selectKey>
        insert  into user(username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
    </insert>
    <!--配置更新用户-->
    <update id="updateUser" parameterType="cn.edu.hznu.domain.User">
        update user set username=#{username},address=#{address},birthday=#{birthday},sex=#{sex} where id=#{id}
    </update>
    <!--配置删除用户 -->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
    <!--配置根据id查询用户-->
    <select id="findUserById" resultType="cn.edu.hznu.domain.User" parameterType="java.lang.Integer">
        select * from user where id=#{id}
    </select>
    <!--模糊查询-->
    <select id="findUserByName" resultType="cn.edu.hznu.domain.User" parameterType="java.lang.String">
        select * from user where username like #{username}
    </select>
    <!--查询所以用户-->
    <select id="findAllUser" resultType="java.lang.Integer">
        select count(id) from user
    </select>
    <!--根据queryvo对象查询-->
    <select id="findUserByVo" parameterType="cn.edu.hznu.domain.QueryVo" resultType="cn.edu.hznu.domain.User">
        select * from user where username like #{user.username}
    </select>
</mapper>

mapper属性标志着该文件与哪个接口类对应

resultMap,如果实体类中的属性名和数据库中的属性名不对应时,需要使用map来进行对应,这样返回值就可以直接封装进实体类中

<select> <delete><update><insert>增删改查标签

  id: 对应的接口类中的方法

  resulttype:返回值结果,如果采用了map映射,就使用resultMap的id

  perparmeterType:参数集数据类型,可以使用基本数据类型,也可以采用引用数据类型

  因为参数使用OGNL表达式,所以不需要使用get set,可以使用.perparmetername来访问,在根据queryVo查询中,因为queryvo中封装了user,所以想要使用user类中的username属性就需要采用user.username来访问。

OGNL表达式:
Object Graphic Navigation Language
对象 图 导航 语言

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
    可以使用resource
    也可以使用URL
    http://localhost:8080/j/homework
    协议    主机号     端口号    URI(uniform resource  Identifier)统一资源定位符
    file协议
    -->
    <properties resource="jdbcConfig.properties">
<!--        <property name="driver" value="com.mysql.jdbc.Driver"/>-->
<!--        <property name="url" value="jdbc:mysql://localhost:3306/eesy_mybatis"/>-->
<!--        <property name="username" value="root"/>-->
<!--        <property name="password" value="199945"/>-->
    </properties>


<!--    使用typeAliases-->
    <typeAliases>
        <!--     typeAlias 起别名  -->
        <typeAlias type="cn.edu.hznu.domain.User" alias="user"></typeAlias>
        <!--     使用packge 包下所有类自动起别名,并且类名就是别名-->
        <package name="cn.edu.hznu"/>
    </typeAliases>

    <!--    配置环境-->
    <environments default="mysql">
        <!--        配置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>
    <!--    映射配置文件
            使用注解的话,使用class指定注解的dao
    -->
    <mappers>
                <mapper resource="cn/edu/hznu/dao/IUserDao.xml"/>
        <!--        <mapper class="cn.edu.hznu.dao.IUserDao"/>-->
    </mappers>
</configuration>

原文地址:https://www.cnblogs.com/wjune-0405/p/12605694.html