官方参考
file,new,spring starter project
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.zfh
springboot-zfh07-mybatisplus-3
0.0.1-SNAPSHOT
springboot-zfh07-mybatisplus-3
Demo project for Spring Boot
1.8
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.1.0
com.h2database
h2
runtime
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
data-h2.sql
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
package com.zfh.demo.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
package com.zfh.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zfh.demo.entity.User;
public interface UserMapper extends BaseMapper {
}
package com.zfh.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
**@MapperScan("com.zfh.demo.mapper")**
public class SpringbootZfh07Mybatisplus3Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootZfh07Mybatisplus3Application.class, args);
}
}
package com.zfh.demo;
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.zfh.demo.entity.User;
import com.zfh.demo.mapper.UserMapper;
import junit.framework.Assert;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootZfh07Mybatisplus3ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
@Test
public void testInsert() {
System.out.println(("----- Insert one record test ------"));
User user = new User();
user.setId(6l);
user.setName("Peter");
user.setAge(30);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
Assert.assertEquals(1,result);
System.out.println("insert 1 user");
System.out.println(user.toString());
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size());
userList.forEach(System.out::println);
}
@Test
public void testUpdate() {
System.out.println(("----- Insert one record test ------"));
User user = new User();
user.setId(6l);
user.setName("Peter");
user.setAge(30);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
Assert.assertEquals(1,result);
System.out.println("insert 1 user");
System.out.println(user.toString());
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size());
userList.forEach(System.out::println);
user.setId(6l);
user.setName("Peter Zheng");
user.setAge(31);
user.setEmail("[email protected]");
result = userMapper.updateById(user);
Assert.assertEquals(1,result);
System.out.println("update 1 user");
System.out.println(user.toString());
System.out.println(("----- selectAll method test ------"));
userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size());
userList.forEach(System.out::println);
}
@Test
public void testDelete() {
System.out.println(("----- Insert one record test ------"));
User user = new User();
user.setId(6l);
user.setName("Peter");
user.setAge(30);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
Assert.assertEquals(1,result);
System.out.println("insert 1 user");
System.out.println(user.toString());
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(6, userList.size());
userList.forEach(System.out::println);
user.setId(5l);
user = userMapper.selectById(user.getId());
result = userMapper.deleteById(user.getId());
Assert.assertEquals(1,result);
System.out.println("delete 1 user");
System.out.println(user.toString());
System.out.println(("----- selectAll method test ------"));
userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
@Test
public void testOther() {
String projectPath = System.getProperty("user.dir");
projectPath += "\\src\\main\\java";
System.out.println(projectPath);
}
@Test
public void contextLoads() {
}
}