数据库
驱动(yml文件)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db2019?useUnicode=true&characterEncoding=utf-8
username: root
password: fawde0615
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
实例
package com.fawde.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data//注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
@AllArgsConstructor
@NoArgsConstructor
//实体类
@TableName("user")
public class User {
//指定主键
@TableId(value = "id",type = IdType.AUTO)
private int id;
private String name;
private String email;
private int age;
public User(String name,String email,int age){
this.name = name;
this.email=email;
this.age =age;
}
}
//Dept类似,但是继承了Model
@Data//注解在类上, 为类提供读写属性, 此外还提供了 equals()、hashCode()、toString() 方法
@AllArgsConstructor
@NoArgsConstructor
@TableName("dept")
public class Dept extends Model<Dept> {
//指定主键
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private String name;
private String mobile;
private Integer manager;
public Dept(String name,String mobile,Integer manager){
this.name= name;
this.mobile=mobile;
this.manager=manager;
}
}
接口
继承BaseMapper
package com.fawde.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fawde.mybatisplus.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
public interface DeptMapper extends BaseMapper<Dept> {
}
使用
@RunWith(SpringRunner.class)
@SpringBootTest
class MybatisplusApplicationTests {
@Autowired
private UserMapper userDao ;
@Test
void contextLoads() {
}
@Test
public void testInsert(){
User user =new User("zhangsan22","zhangsan@126.com",20);
int rows = userDao.insert(user);
System.out.println("insert user result:"+rows);
}
@Test
public void testARInsert(){
Dept dept = new Dept("事业部","010-12346",2);
boolean result = dept.insert();
System.out.println("AR Insert:"+result);
}
}