Mybatis学习常见bug及其解决办法-sql映射篇3

bug1:


Process finished with exit code 0   //如您所见,一片空白

cause1:

<select id="findUserListByMap"
            resultType="edu.xlh.entity.User">
        <!--方法参数类型为map类型,占位符里面的属性名为map的key名 -->
        select * from student where sid like concat("%",#{ssid},"%") and sname=#{sname}   
    </select>

sourse code1:

@Test
    public void test03() throws IOException {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Map<String , Object> map = new HashMap<String , Object>();
        map.put("sid" , "s"); //key为sid
        map.put("sname" , "lisi");//key为sname
        List<User> userList = mapper.findUserListByMap(map);
        for(User user : userList)
        {
            System.out.println(user);
        }
    }

work1:

<select id="findUserListByMap"
            resultType="edu.xlh.entity.User">
        <!--方法参数类型为map类型,占位符里面的属性名为map的key名 -->
        select * from student where sid like concat("%",#{sid},"%") and sname=#{sname}   //将原因中的ssid改为sid
    </select>

bug2:

Caused by: org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mapper/User-Mapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. 
Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. 
Cause: org.apache.ibatis.builder.BuilderException: Error resolving class.
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'edu.xlh.entity.Use'.  //edu.xlh.entity.Use看不见
Cause: java.lang.ClassNotFoundException: Cannot find class: edu.xlh.entity.Use  //找不到类edu.xlh.entity.Use

cause2:

    <select id="findUserListByMap"
            resultType="edu.xlh.entity.Use"> //类写错了
        <!--方法参数类型为类类型,占位符里面的属性名为实体类的属性名 -->
        select * from student where sid like concat("%",#{sid},"%") and sname=#{sname}
    </select>

sourse code2:

package edu.xlh.entity;

public class User {
    private  String sid;
    private  String sname;
    private  String password;
    private  Integer superuser;
    private  Integer state;

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }
    ......

work2:

    <select id="findUserListByMap"
            resultType="edu.xlh.entity.User"> //将Use改为User
        <!--方法参数类型为类类型,占位符里面的属性名为实体类的属性名 -->
        select * from student where sid like concat("%",#{sid},"%") and sname=#{sname}
    </select>

bug3:

订单号 :1 订单编号 :1  用户主键id : null 姓名 :张三
OrderDetail{id=1, ordersId=1, itemsId=1, itemdNum=2}
订单号 :2 订单编号 :2  用户主键id : null 姓名 :张三
OrderDetail{id=2, ordersId=2, itemsId=1, itemdNum=2}
OrderDetail{id=3, ordersId=2, itemsId=2, itemdNum=2}
订单号 :3 订单编号 :3  用户主键id : null 姓名 :张三
OrderDetail{id=4, ordersId=3, itemsId=1, itemdNum=3}
OrderDetail{id=5, ordersId=3, itemsId=2, itemdNum=3}
OrderDetail{id=6, ordersId=3, itemsId=2, itemdNum=3}
订单号 :4 订单编号 :1  用户主键id : null 姓名 :李四
OrderDetail{id=7, ordersId=4, itemsId=2, itemdNum=1}
订单号 :5 订单编号 :2  用户主键id : null 姓名 :李四
OrderDetail{id=8, ordersId=5, itemsId=1, itemdNum=10}
OrderDetail{id=9, ordersId=5, itemsId=2, itemdNum=10}
订单号 :6 订单编号 :1  用户主键id : null 姓名 :王五
OrderDetail{id=10, ordersId=6, itemsId=2, itemdNum=20}

cause3:

   <resultMap id="BaseResultMap" type="edu.xlh.entity.Orders">
        <id property="id" column="id" />
        <result property="user_id" column="user_id" />
        <result property="number" column="number" />
        <result property="createTime" column="createTime" />
        <result property="note" column="note" />
        <result property="username" column="username" />
    </resultMap>
<!-- 多对一 -->
    <resultMap id="OrdersResultMap" type="edu.xlh.entity.Orders" extends="BaseResultMap">
        <association property="user" javaType="edu.xlh.entity.User">
        <id property="id" column="u_id" />
        <result property="username" column="username" />
        <result property="birthday" column="birthday" />
        <result property="sex" column="sex" />
        <result property="address" column="address" />
        </association>
    </resultMap>
<!-- 一对多 -->
    <resultMap id="orderAndDetailResultMap" type="edu.xlh.entity.Orders" extends="OrdersResultMap">
        <collection property="orderDetailList" ofType="edu.xlh.entity.OrderDetail">
            <id property="id" column="order_detail_id" />
            <result property="ordersId" column="orders_id" />
            <result property="itemsId" column="items_id" />
            <result property="itemdNum" column="items_num" />
        </collection>
    </resultMap>
 <select id="findOrderListWithDetail" resultMap="orderAndDetailResultMap">
select
o.*,
d.id as order_detail_id,
d.orders_id,
d.items_id,
d.items_num,
u.username,
u.id <!--与其他表中主键字段名重复,需要用别名,且别名的利用的唯一方法是表别名.表字段名 as 映射中定义的字段名,如果不利用映射中定义的字段名也会为空 -->
from
orders o
inner join orderdetail d on o.id = d.orders_id
inner join user u on u.id  = o.user_id
</select>

测试类:

@Test
    public void test03(){
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
        List<Orders> ordersList = ordersMapper.findOrderListWithDetail();
        for (Orders orders : ordersList) {
            System.out.println("订单号 :"+orders.getId()+" "+"订单编号 :"+orders.getNumber()+" "+" "+
                    "用户主键id : "+orders.getUser().getId()+" "+
                    "姓名 :"+orders.getUser().getUsername());
            for (OrderDetail orderDetail : orders.getOrderDetailList()) {
                System.out.println(orderDetail);
            }
        }
        MyBatisUtil.closeSqlSession(sqlSession);
    }

work3:

<select id="findOrderListWithDetail" resultMap="orderAndDetailResultMap">
select
o.*,
d.id as order_detail_id,
d.orders_id,
d.items_id,
d.items_num,
u.username,
u.id as u_id //使用别名
from
orders o
inner join orderdetail d on o.id = d.orders_id
inner join user u on u.id  = o.user_id
</select>

bug4:

数据库连接中断

### Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
### The error may exist in mapper/OrdersMapper.xml
### The error may involve edu.xlh.dao.OrdersMapper.findOrderListWithDetail
### The error occurred while executing a query
### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

work:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fire king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值