mybatis-plus配置
引入依赖
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
使用(启动类上配置mapper所在包的位置)
@SpringBootApplication @MapperScan(basePackages = "com.example.demo.mapper")//mapper所在包的位置 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
实体类配置(TableName:表名;Data:自动生成该实体类的各种方法,例如:get,set,tostring,student等方法)
@TableName("students")
@Data//自动生成get,set,tostring等方法
public class student {
@TableId
private String id;
@TableField("name")
private String name;
@TableField("sex")
private int sex;
@TableField("address")
private String address;
@TableField("company")
private String company;
/**
* 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入到数据库中
* 使用 transient 、 static 修饰的属性也不会插入数据库中
*/
@TableField(exist = false)
private String phone;
}
studentMapper
@Mapper @Component public interface studentMapper extends BaseMapper<student> { }
调用
@CrossOrigin @RestController @RequestMapping("/student") @Api(tags = "Mybatis-Plus测试类") public class studentController { @Autowired private studentMapper mapper; @RequestMapping(value = "/query",method = RequestMethod.GET) public Map<String,Object> query(){ Map<String,Object> map=new HashMap(); List<student> list=mapper.selectList(null);//输入null,即代表查询全部 map.put("null",list); return map; } @RequestMapping(value = "/queryById",method = RequestMethod.GET) public Map<String,Object> queryById(String id){ Map<String,Object> map=new HashMap(); student s=new student(); s=mapper.selectById(id);// map.put("根据"+id+"查询",s); return map; } }
个人见解:如有不对,请见谅!
1、lombok节省了大量编写基础代码的时间
2、mybatis-plus节省了编写sql语言的时间
3、比mybatis更加便捷,更加好用

浙公网安备 33010602011771号