MyBatis-Plus--三种方式整合之一 Mybatis+MP整合

本文介绍了如何进行MyBatis-Plus的快速入门,包括创建子工程,定义POJO和Mapper,配置resources文件如log4j.properties,UserMapper.xml和mybatis-config.xml,以及进行Mybatis和MyBatis-Plus的测试运行。在测试过程中,解决了userName字段映射问题,并展示了MP整合的代码差异。
摘要由CSDN通过智能技术生成

加油,新时代打工人!

MyBatis-Plus快速入门–环境搭建

注意
整合之前查看上面文章环境搭建

(Mybatis+MP整合)

一、创建子工程Module

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mybatis-plus</artifactId>
        <groupId>com.github/itboywh</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>itboywh-mybatis-plus-simple</artifactId>



</project>

二、创建itboywh.mp.simple包

2.1 pojo

package itboywh.mp.simple.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wh
 * @date 2021年11月25日9:23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private String email;
}

2.2 mapper

package itboywh.mp.simple.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import itboywh.mp.simple.pojo.User;

import java.util.List;

/**
 * @author wh
 * @date 2021年11月25日9:37
 */
public interface UserMapper {
  List<User> findAll();
}

三、配置resources

3.1 log4j.properties

log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

3.2 UserMapper.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="itboywh.mp.simple.mapper.UserMapper">
    <!--解决实体类和数据库查询结果不匹配,也可以在sql语句起别名-->
    <resultMap id="userMap" type="itboywh.mp.simple.pojo.User">
        <id property="id" column="id"></id>
        <result property="userName" column="user_name"></result>
        <result property="password" column="password"></result>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="email" column="email"></result>

    </resultMap>
    <select id="findAll" resultMap="userMap">
        select * from tb_user
    </select>
</mapper>

3.3 mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mp?
useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;allowMultiQuerie
s=true&amp;useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

四、 测试运行代码和截图

4.1 测试Mybatis

package itboy.mp.simple.mapper;

import itboywh.mp.simple.mapper.UserMapper;
import itboywh.mp.simple.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @author wh
 * @date 2021年11月25日9:39
 */
public class testmybatis {
    @Test
    public void findAll() throws Exception {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        
    }
}

4.2 运行截图

在这里插入图片描述

4.3 MyBatis错误总结

这里遇到userName名字为空,解决方案可以在UserMapper.xml添加如下代码

 <!--解决实体类和数据库查询结果不匹配,也可以在sql语句起别名和实体类相同-->
    <resultMap id="userMap" type="itboywh.mp.simple.pojo.User">
        <id property="id" column="id"></id>
        <result property="userName" column="user_name"></result>
        <result property="password" column="password"></result>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="email" column="email"></result>

    </resultMap>

五、整合MP

只需要在mapper继承 BaseMapper<>

public interface UserMapper extends BaseMapper<User>{
  List<User> findAll();
}

5.1 测试MP

这里面和Mybatis的区别
使用MP中MybatisSqlSessionFactoryBuilder()
使用MP提供的查询方法selectList(),
另外我们不用起别名,因为MP查询的是每个字段不是用 *
在这里插入图片描述

package itboy.mp.simple.mapper;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import itboywh.mp.simple.mapper.UserMapper;
import itboywh.mp.simple.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @author wh
 * @date 2021年11月25日9:39
 */
public class testmybatisplus {
    @Test
    public void findAll() throws Exception {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory=new MybatisSqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

//        List<User> users = mapper.findAll();
        List<User> users = mapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }


    }
}

5.2 运行截图

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Hello World呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值