mybatis的resultMap学习(基础配置(一)

一、依赖dependency以及数据库配置

  1. 依赖

  2. 数据库配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx.xsd">
        <!--扫描包-->
        <context:component-scan base-package="com.kk.controller, com.kk.mapper, com.kk.service"/>
        <!--配置数据库连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="jdbc:mysql://localhost:3306/usedata?serverTimezone=GMT" />
            <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
            <property name="maxActive" value="16"/>
            <property name="minIdle" value="5"/>
        </bean>
        <!--创建Mybatis SqlSessionFactory,和数据库连接池连接-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!--加载mybatis的配置-->
            <property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
        </bean>
       <!--Mybatis整合Spring,告诉spring,数据库代码存在位置(创建各自接口的动态代理类)-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.kk.mapper"/>
        </bean>
        <!--将数据源关联到事务-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--开启事务-->
        <tx:annotation-driven/>
</beans>

二、知识点
4. resultType要求数据库中列名和pojo对象的属性名一模一样,面对不一样的情况可以用resultMap处理
示例:
1.1 在接口OrderMapper中

import models.Order;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;

@Mapper
public interface OrderMapper {
    // 查询所有的订单
    List<Order> queryAllOrderByResultMap();
}

1.2 在OrderMapper.xml中
它在文件的位置
在这里插入图片描述

<?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.kk.mapper.OrderMapper">
    <resultMap id="baseOrderMap" type="com.kk.models.Order">
        <id column="id"  jdbcType="VARCHAR" property="idBean"/>
        <result column="pid" jdbcType="INTEGER" property="pidBean"/>
        <result column="userid" jdbcType="INTEGER" property="useridBean"/>
    </resultMap>
    <select id="queryAllOrderByResultMap" resultMap="baseOrderMap">
        select * from `order`
    </select>
</mapper>

1.3 在serviceImpl中

package com.kk.service;

import com.kk.mapper.OrderMapper;
import com.kk.models.Order;
import com.kk.models.Product;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Override
    public int generateOrder(Product product) {
        Order order = new Order();

        return 0;
    }

    @Override
    public void queryAllOrderByResultMap() {
        List<Order> orders = orderMapper.queryAllOrderByResultMap();
        System.out.println(orders);
    }
}

1.4 在测试类中

package com.test;

import com.kk.service.OrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@ContextConfiguration(locations = {"classpath:spring/spring.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderControllerTest {
    @Resource
    private OrderService orderService;

    @Test
    public void testQueryAllOrder() {
        orderService.queryAllOrderByResultMap();
    }
}

1.5 查询结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值