欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis映射文件mapper.xml的具体写法

程序员文章站 2022-04-03 20:58:59
mapper映射文件是一个xml格式文件,必须遵循相应的dtd文件规范在学习mybatis的时候我们通常会在映射文件这样写:

mapper映射文件是一个xml格式文件,必须遵循相应的dtd文件规范

在学习mybatis的时候我们通常会在映射文件这样写:

<?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="com.qbd.mapper.studentmappers">
    <select id="findbyid" parametertype="integer" resultmap="studentresult">
        select *from student where id=#{id}
    </select>
    
    <select id="findbygradeid" parametertype="integer" resultmap="studentresult">
        select *from student where gid=#{gid}
    </select>
    
    <resultmap type="student" id="studentresult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <association property="address" column="addid" select="com.qbd.mapper.addressmappers.findbyid">    
        </association>
        <association property="grade" column="gid" select="com.qbd.mapper.grademappers.findbyid">    
        </association>
    </resultmap>
</mapper>

然后再写dao的实现在写一个类,来实现dao的接口。但是这样在实现中需要这样写:

package com.qbd.service;
import org.apache.ibatis.session.sqlsession;
import org.apache.log4j.logger;
 
import com.qbd.mapper.studentmappers;
import com.qbd.model.student;
import com.qbd.util.sqlsessionfactoryutil;
 
public class studentservice {
    private static logger logge=logger.getlogger(studentservice.class);
    public static void main(string[] args) {
        sqlsession sqlsession=sqlsessionfactoryutil.getsqlsession();
        
        studentmappers studentmappers=(studentmappers)sqlsession.getmapper(studentmappers.class);
        student student=new student();
        student.setname("22");
        student.setage(2);
        int s=studentmappers.add(student);
        sqlsession.commit();
        if(s>0){
            logge.info("success");
            system.out.println("success");
        }
        
    }
}

来读取配置文件

另一种方法就是:

直接在mapper.xml中的这一部分写成dao<mapper namespace="com.qbd.mapper.studentmappers">如下

<?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="com.qbd.ssm.dao.userdao">
    <!-- 定义缓存  一般是一级缓存,如果用同一个sqlsession 那么相同查询直接会从缓存中查找 
    <cache size="1024" flushinterval="60000" eviction="lru" readonly="false"></cache>
    -->
    <!-- 查找所有 -->
    <select id="find" parametertype="map" resultmap="studentresult">
        select * from user
        <where>
            <if test="uname!=null and uname!='' ">
                and uname like #{uname}
            </if>
        </where>
        <if test="start!=null and size!=null">
            limit #{start},#{size}
        </if>
    </select>
    
    <select id="gettotal" parametertype="map" resulttype="long">
        select count(*) from user
        <where>
            <if test="uname!=null and uname!='' ">
                and uname like #{uname}
            </if>
        </where>
    </select>
    <!-- 按照用户名和密码查找 -->
    <select id="getuser" resultmap="studentresult" parametertype="map">
        select *from user where uname=#{uname} and upassword=#{upassword}
    </select>
    <!-- 删除 -->
    <delete id="delete" parametertype="map">
        delete from user where uid=#{uid}
    </delete>
    <!-- 修改 -->
    <update id="update" parametertype="user">
        update user
        <set>
            <if test="uname!=null">
                 uname=#{uname},
            </if>
            <if test="upassword!=null">
                upassword=#{upassword},
            </if>
            <if test="upower!=null">
                upower=#{upower},
            </if>
        </set>
        where uid=#{uid}
    </update>
    <!-- 增加 -->
    <insert id="add" parametertype="user">
        insert into user values(null,#{uname},#{upassword},#{upower})
    </insert>
    <resultmap type="user" id="studentresult">
        <id property="uid" column="uid"/>
        <result property="uname" column="uname"/>
        <result property="upassword" column="upassword"/>
    </resultmap>
</mapper>

那么就不用写dao的实现在service中就能掉用       mybatis默认会把mapper.xml映射为dao的实现

那么下面dao就能这样写:

package com.qbd.ssm.dao;
 
import java.util.list;
import java.util.map;
 
import org.apache.ibatis.annotations.delete;
 
import com.qbd.ssm.model.user;
 
public interface userdao {
 
    public list<user> getall();
    public user getuser(user user);
    public int delete(user user);
    public int update(user user);
    public int add(user user);
    public list<user> find(map<string,object> map);
    public long gettotal(map<string,object> map);
}

service这样写

package com.qbd.ssm.service;
import java.util.list;
import java.util.map;
import com.qbd.ssm.model.user; 
public interface userservice {
 
    public list<user> getall();
    public user getuser(user user);
    public int delete(user user);
    public int update(user user);
    public int add(user user);
    public list<user> find(map<string,object> map);
    public long gettotal(map<string,object> map);
}

service的实现:

package com.qbd.ssm.serviceimpl;
import java.util.list;
import java.util.map;
import javax.annotation.resource; 
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import com.qbd.ssm.dao.userdao;
import com.qbd.ssm.model.user;
import com.qbd.ssm.service.userservice;
 
@service("userservice")
public class userserviceimpl implements userservice {
 
    private userdao userdao;
    
    public userdao getuserdao() {
        return userdao;
    }
 
    @resource
    public void setuserdao(userdao userdao) {
        this.userdao = userdao;
    }
 
    public list<user> getall() {
        // todo auto-generated method stub
        return userdao.getall();
    }
 
    public user getuser(user user) {
        // todo auto-generated method stub
        return userdao.getuser(user);
    }
 
    public int delete(user user) {
        // todo auto-generated method stub
        return userdao.delete(user);
    }
 
    public int update(user user) {
        // todo auto-generated method stub
        return userdao.update(user);
    }
 
    public int add(user user) {
        // todo auto-generated method stub
        return userdao.add(user);
    }
 
    public list<user> find(map<string, object> map) {
        // todo auto-generated method stub
        return userdao.find(map);
    }
 
    public long gettotal(map<string, object> map) {
        // todo auto-generated method stub
        return userdao.gettotal(map);
    }
}

在这里面userdao不能写错,spring会按照name进行注入

到此这篇关于mybatis映射文件mapper.xml的具体写法的文章就介绍到这了,更多相关mybatis映射文件mapper.xml内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: mybatis mapper.xml