MyBatis Plus简单实例

1、定义

MyBatis-Plus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。

2、简单实例

(1)项目结构

(2)MySpringBootApplication.java

package cn.hwd.mp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

	public static void main(String[] args) {
		SpringApplication.run(MySpringBootApplication.class, args);
	}

}

(3)User.java

package cn.hwd.mp.domain;

import java.util.Date;

import lombok.Data;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import com.fasterxml.jackson.annotation.JsonFormat;

@Data
@TableName(value = "user")
public class User {

	@TableId(value = "id", type = IdType.AUTO) // 主键自增长
	private Integer id;
	@TableField(value = "name")
	private String name;
	@TableField(value = "age")
	private Integer age;
	@JsonFormat(pattern="yyyyMMddHHmmssSSS", timezone="GMT+8")
	@TableField(value = "birth")
	private Date birth;
	
}

(4)UserMapper.java

package cn.hwd.mp.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;

import cn.hwd.mp.domain.User;

@Mapper
public interface UserMapper extends BaseMapper {
	
}

(5)UserMapperTests.java

package cn.hwd.mp.mapper;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.hwd.mp.domain.User;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTests {

	@Autowired
	private UserMapper userMapper;
	
	@Test
	public void testSelectList() {
		List userList = userMapper.selectList(
				new EntityWrapper().eq("name", "Tom"));
		System.out.println(userList);
	}

	@Test
	public void testSelectPage() {
		List userList = userMapper.selectPage(
				new Page(1, 10), 
				new EntityWrapper().eq("name", "Tom"));
		System.out.println(userList);
	}
	
	@Test
	public void testSelectById() {
		User user = userMapper.selectById(1);
		System.out.println(user);
	}

	@Test
	public void testInsert() {
		User user = new User();
		user.setName("Diana");
		user.setAge(18);
		user.setBirth(new Date());
		int result = userMapper.insert(user);
		System.out.println(result);
	}

	@Test
	public void testUpdateById() {
		User user = new User();
		user.setId(1);
		user.setName("Jack");
		user.setAge(28);
		user.setBirth(new Date());
		int result = userMapper.updateById(user);
		System.out.println(result);
	}

	@Test
	public void testDeleteById() {
		int result = userMapper.deleteById(1);
		System.out.println(result);
	}

}

(6)application.properties

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/hwd?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = mysql

(7)pom.xml




  4.0.0

  cn.hwd
  mybatisplus01
  0.0.1-SNAPSHOT
  jar

  mybatisplus01
  http://www.example.com

  
	
		org.springframework.boot
		spring-boot-starter-web
		1.4.7.RELEASE
	
	
		org.springframework.boot
		spring-boot-starter
		1.4.7.RELEASE
	
	
		org.springframework.boot
		spring-boot-starter-test
		test
		1.4.7.RELEASE
	
	
		org.projectlombok
		lombok
		1.16.10
    
    
  		mysql
  		mysql-connector-java
  		5.1.30
  	
	
	    com.baomidou
	    mybatis-plus-boot-starter
	    2.2.0
	
  

 

你可能感兴趣的:(Mybatis)