Mybatis学习笔记(二)-Mybatis配置文件与映射文件详解

一、Mybatis配置文件详解

以下是mybatis.xml文件,提倡放在src目录下,文件名任意




<configuration>

    
    <properties resource="db.properties"/>


    
    <typeAliases>
        <typeAlias type="com.madman0621.entity.User" alias="user"/>
    typeAliases>


    
    <environments default="development">

        
        <environment id="development">
            
            <transactionManager type="JDBC"/>
            
            <dataSource type="POOLED">
                
                <property name="driver" value="${driver_class}"/>  
                <property name="url" value="${url}"/>  
                <property name="username" value="${uname}"/>   
                <property name="password" value="${upass}"/>   
            dataSource>
        environment>  
    environments>

    
    <mappers>
        <mapper resource="com/madman0621/entity/UserMapper.xml"/>
    mappers>
configuration>

db.properties文件的内容为:

uname=root
upass=root
driver_class=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test

initPoolSize=5
maxPoolSize=10

二、Mybaits映射文件详解

以下是StudentMapper.xml文件,提倡放在与实体同目录下,文件名任意





<mapper namespace="mynamespace">

    

    <resultMap type="student" id="studentMap">
                                 
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sal" column="sal"/>
    resultMap>


    <insert id="add1">
        insert into students(id,name,sal) values(1,'哈哈',7000)
    insert>

    <insert id="add2" parameterType="student">
        insert into students(id,name,sal) values(#{id},#{name},#{sal})
    insert>

    
    <select id="findById" parameterType="int" resultMap="studentMap">
        select id,name,sal from students where id = #{anything}
    select>

    
    <select id="findAll" resultType="student">
        select id,name,sal from students
    select>

    
    <update id="update" parameterType="student">
        update students set name=#{name},sal=#{sal} where id=#{id}
    update>

     
    <delete id="delete" parameterType="student">
        delete from students where id = #{id}
    delete>
     

mapper>

你可能感兴趣的:(JavaWeb)