(千锋逆战)mybatis

//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">

    <environments default="development">

        <environment id="development">

            <transactionManager type="JDBC"/>

            <dataSource type="POOLED">

                <property name="driver" value="org.mariadb.jdbc.Driver"/>   //mariadb的驱动

                <property name="url" value="jdbc:mariadb://localhost:3306/mydb"/>        // url

                <property name="username" value="root"/>             // 用户

                <property name="password" value="admin"/>          // 密码

        <mapper resource="userMapper.xml"/>  //需要读取的资源文件

</configuration>


//crud等标签操作

<mapper namespace="mapper">  //命名空间    读取是需要 namespace + id 且组合名称唯一

    <select id="selectUser" resultType="zhu.User">

      select* from book

      <select id="selectUserCount" resultType="int">

          selectcount(1) from book

    <select id="selectUsersByPage1" resultType="zhu.User">

        select* from book limit 3,3

    <select id="selectUsersByPage2" resultType="zhu.User">

        select* from book limit #{pagesize}

    <select id="selectUsersByPage3" resultType="zhu.User">

        select* from book limit #{indexstart},#{pagesize}

    <insert id="saveUser" >

        insert into book values(null, #{userName}, #{password},  #{add});

    <delete id="delete" >

        delete from book where id=#{id};

    <update id="updata">

        update book set userName=#{userName},password=#{password} where id=8

</update>


#{   }:占位符号,可以防止sql注入(替换结果会增加单引号‘’)

${   }:sql拼接符号(替换结果不会增加单引号‘’,like和order by后使用,存在sql注入问题,需手动代码中过滤)

//类型别名的配置:

<typeAliases>

 <typeAlias    alias="Author"    type="domain.blog.Author"/>//就可直接使用"Author" 不需要添加啊路径

</typeAliases>

<typeAliases>

 <package name="domain.blog"/> /

</typeAliases>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容