MyBatis配置文件模板

MyBatis中XML配置文件格式

MyBatis-Config.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>
    <!--配置环境-->
    <environments default="mysql">
        <!--配置mysql的环境-->
        <environment id="mysql">
            <!--配置事务类型-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源(连接池)-->
            <dataSource type="POOLED">
                <!--配置连接数据库的4个基本信息-->
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/demo?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123321"/>
            </dataSource>
        </environment>
    </environments>

    <!--指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件-->
    <mappers>
    	<!--如果采用注解的方式,用class属性,值为dao接口的全限定类名-->
        <mapper resource="cn/goodboyding/dao/UserMapper.xml" />
    </mappers>
</configuration>

Mapper.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.goodboyding.dao.UserDao">

    <!--配置查询所有-->
    <!--
    	id与dao接口的方法名对应
    	resultType是方法的返回值类型,使用javaBean作为返回值需要写全限定类名
    	parameterType是方法的入参
    	中间的value值为方法需要执行的sql语句
    -->
    <select id="findAll" resultType="cn.goodboyding.entiy.User">
        select * from user;
    </select>

    <!--配置根据id查询-->
    <select id="findUserById" resultType="cn.goodboyding.entiy.User" parameterType="int">
        select * from user where id = #{userId};
    </select>

    <!--配置根据名字模糊查询-->
    <select id="findByName" parameterType="string" resultType="cn.goodboyding.entiy.User">
        select * from user where userName like #{name};
    </select>

    <!--配置保存-->
    <insert id="saveUser" parameterType="cn.goodboyding.entiy.User">
        <!--配置读取刚插入信息的id并封装到user对象中-->
        <!--
        	keyProperty是字段名
        	resultType是类型
        	order指selectKey中语句的执行时机,AFTER代表后执行
        -->
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user (userName,birthday,sex,dress) value (#{userName},#{birthday},#{sex},#{dress})
    </insert>

    <!--配置更新操作-->
    <update id="updateUser" parameterType="cn.goodboyding.entiy.User">
        update user set userName=#{userName},birthday=#{birthday},sex=#{sex},dress=#{dress} where id=#{id}
    </update>
    <!--配置删除操作-->

    <delete id="deleteUser" parameterType="int">
        delete from user where id = #{id}
    </delete>

    <!--配置查询用户总数-->
    <select id="findTotal" resultType="int">
        select count(id) from user;
    </select>

</mapper>
posted @ 2020-03-01 22:43  GoodBoyDing  阅读(51)  评论(0)    收藏  举报