api代码测试
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yby.mapper.UserMapper;
import com.yby.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Autowired
private UserMapper userMapper;
@org.junit.jupiter.api.Test
public void findAllTest(){
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void insertTest(){
User user = new User();
user.setAge(18L);
user.setMail("141518@qq.com");
user.setUsername("yby");
user.setPassword("520");
//result 受影响的行数
int result = this.userMapper.insert(user);
System.out.println(result);
System.out.println("id = " + user.getId());
}
@org.junit.jupiter.api.Test
public void updateByIdTest(){
User user = new User();
user.setId(1L);
user.setUsername("ypf");
int result = this.userMapper.updateById(user);
System.out.println(result);
}
@org.junit.jupiter.api.Test
public void updateTest(){
//设置修改后的参数
User user = new User();
user.setAge(22L);
user.setUsername("ccy");
//第一种方法
//设置修改条件
QueryWrapper<User> wrapper1 = new QueryWrapper<>();
wrapper1.eq("username","upf");
int result = this.userMapper.update(user, wrapper1);
System.out.println(result);
//第二种方法
//设置修改条件 & 修改后的参数
UpdateWrapper<User> wrapper2 = new UpdateWrapper<>();
wrapper2.eq("id", 5).set("age", 23);
int result2 = this.userMapper.update(user, wrapper2);
System.out.println(result2);
}
@org.junit.jupiter.api.Test
public void deleteByIdTest(){
int result = this.userMapper.deleteById(9L);
System.out.println(result);
}
@org.junit.jupiter.api.Test
public void deleteByMapTest(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("username","yby");
map.put("password","520");
//Map中存放的条件是And关系
int result = this.userMapper.deleteByMap(map);
System.out.println(result);
}
@org.junit.jupiter.api.Test
public void deleteTest(){
//方法一
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("username","yby").eq("password","520");
//方法二
User user = new User();
user.setAge(18L);
user.setPassword("520");
QueryWrapper<User> wrapper2 = new QueryWrapper<>(user);
int result = this.userMapper.delete(wrapper);
System.out.println(result);
}
@org.junit.jupiter.api.Test
public void deleteBatchIdsTEst(){
int result = this.userMapper.deleteBatchIds(Arrays.asList(10L, 11L));
System.out.println(result);
}
@org.junit.jupiter.api.Test
public void selectByIdTest(){
User user = this.userMapper.selectById(1L);
System.out.println(user);
}
@org.junit.jupiter.api.Test
public void selectBatchIdsTest(){
List<User> userList = this.userMapper.selectBatchIds(Arrays.asList(1L,2L,3L));
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void SelectOneTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","lisi");
User user = this.userMapper.selectOne(wrapper);
System.out.println(user);
}
@org.junit.jupiter.api.Test
public void SelectCountTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",18);
Integer count = this.userMapper.selectCount(wrapper);
System.out.println(count);
}
@org.junit.jupiter.api.Test
public void SelectListTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age",18);
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
/**
* 条件构造器
*/
@org.junit.jupiter.api.Test
public void Test1(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("password","123")
.ge("age",18)
.in("name","lisi","yby");
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void LikeTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.like("name","颜");
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void OrderTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("age");
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void OrTest() {
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","yby").or().eq("age",18);
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
@org.junit.jupiter.api.Test
public void SelectTest(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.select("id","name","age")
.eq("name","yby");
List<User> userList = this.userMapper.selectList(wrapper);
for (User user : userList) {
System.out.println(user);
}
}
}
UserMapper 接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yby.pojo.User;
import org.mybatis.spring.annotation.MapperScan;
public interface UserMapper extends BaseMapper<User> {
}
User 实体类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String password;
@TableField(value = "email")
private String mail;
private String name;
private Long age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMail() {
return mail;
}
public void setMail(String email) {
this.mail = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
application.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/db1?
useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL =false
jdbc.username=root
jdbc.password=密码
日志文件引入
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.Conver