服务器之家

服务器之家 > 正文

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

时间:2021-12-25 16:59     来源/作者:钙奶加辣条。

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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的接口。但是这样在实现中需要这样写:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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">如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?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就能这样写:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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这样写

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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的实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/two_people/article/details/51759881

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部