Specific writing method of mybatis mapping file mapper. xml

  • 2021-11-13 01:31:21
  • OfStack

The Mapper mapping file is an xml format file and must follow the corresponding dtd file specification

When learning mybatis, we usually write this in the mapping file:


<?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>

Then write the implementation of dao and write a class to realize the interface of dao. But in the implementation, you need to write this:


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");
        }
        
    }
}

To read the configuration file

Another method is:

This part 1 is written directly in mapper. xml as dao < mapper namespace="com.qbd.mapper.StudentMappers" > As follows


<?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">
    <!--  Definition cache   1 Like is 1 Level cache, if you use the same as 1 A sqlsession  The same query will be found directly from the cache  
    <cache size="1024" flushInterval="60000" eviction="LRU" readOnly="false"></cache>
    -->
    <!--  Find all  -->
    <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>
    <!--  Find by username and password  -->
    <select id="getUser" resultMap="StudentResult" parameterType="Map">
        select *from user where uname=#{uname} and upassword=#{upassword}
    </select>
    <!--  Delete  -->
    <delete id="delete" parameterType="Map">
        delete from user where uid=#{uid}
    </delete>
    <!--  Modify  -->
    <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>
    <!--  Increase  -->
    <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>

You don't need to write the implementation of dao. You can use mybatis in service. By default, mapper. xml will be mapped to the implementation of dao

Then the following dao can be written as follows:


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 writes like this


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);
}

Implementation of 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);
    }
}

In this case, userDao can't be written wrong, and spring will be injected according to name


Related articles: