MyBatisPlus介绍入门以及项目集成MyBatisPlus

场景

项目专栏

https://blog.csdn.net/column/manage/37194

简介

MybatisPlus是一个MyBatis的增强工具包,只做增强不做改变。

为简化开发工作、提高生产效率而生。

官网:

https://mp.baomidou.com/

GitBub地址:

https://github.com/baomidou/mybatis-plus

码云地址:

https://gitee.com/baomidou/mybatis-plus

官方文档:

https://baomidou.gitee.io/mybatis-plus-doc/#/quick-start

实现

新建数据库mp,新建表Employee

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第1张图片

插入数据

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第2张图片

新建项目

打开Eclipse--new--Maven Project--Create a simple project

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第3张图片

输入组织名等

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第4张图片

新建项目后打开pom.xml引入项目依赖

注意:

Mybatis以及Mybatis-Soring依赖不要加入到项目配置中,以免引起版本冲突,因为Mybatis-Plus会自动维护。


  4.0.0
  com.badao.mp
  hellomp
  0.0.1-SNAPSHOT
  
    
  
      com.baomidou
      mybatis-plus
      2.3
    
  
  
   junit
   junit
   4.9
  
  
  
   log4j
   log4j
   1.2.17
  
  
  
   com.mchange
   c3p0
   0.9.5.2
  
  
  
   mysql
   mysql-connector-java
   8.0.11
  
  
  
   org.springframework
   spring-context
   4.3.10.RELEASE
  
  
   org.springframework
   spring-orm
   4.3.10.RELEASE
  
 
  

新建bean Employee,属于beans包

 MyBatisPlus介绍入门以及项目集成MyBatisPlus_第5张图片

代码:

package com.badao.beans;

public class Employee {
 
 private Integer id;
 private String name;
 private String email;
 private Integer gender;
 private Integer age;
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public Integer getGender() {
  return gender;
 }
 public void setGender(Integer gender) {
  this.gender = gender;
 }
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 
}

新建配置文件

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第6张图片

db.ptoperties代码:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password= 123

log4j.xml代码:



 

 
 
   
   
    
   
 
 
   
 
 
   
 
 
   
   
 

mybatis-config.xml代码:




 

applicationContext.xml



 
 
 
 
 
  
  
  
  
 
 
 
 
  
 
 
 
 
 
 
 
  
  
  
  
    
  
  
 

 
 
  
 
 

进行测试:

在src/test/java下新建Junit Test Case

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第7张图片

 

 

编写代码如下,记得导包不要导错。

package com.badao.test;
import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;




public class TestMp {
 
 private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");

 @Test
 public void testDataSource() throws Exception{
  DataSource ds = ioc.getBean("dataSource",DataSource.class);
  System.out.println("******************************************"+ds);
  Connection connection = ds.getConnection();
  System.out.println("******************************************"+connection);
  
 }

}

运行测试效果:

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第8张图片

至此测试成功说明环境搭建成功。

集成MybatisPlus

我们只需要把Mybatis自带的SessionFactoryBean替换为MybatisPlus自带的MybatisSqlSessionFactoryBean即可。

打开applicationContext.xml

找到

MyBatisPlus介绍入门以及项目集成MyBatisPlus_第9张图片


 
  
  
  
  
     
 

源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11123716

 



 

你可能感兴趣的:(MyBatisPlus)