时间:2021-05-20
本节内容扩展介绍下针对mybatis的增强工具mybatis-plus,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二话不多说,我们先写编写个简单的例子,让大家先初步的了解下mybatis-plus。
1.mybatis-plus初步实例
(1)创建一个spring boot web工程(具体创建过程就不再演示了,还不会的同学去看看spring boot专题第一节内容)
(2)引入依赖
<!--web项目依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--validation表单校验--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--thymeleaf依赖包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>(3)配置文件application.yml
# DataSource Config spring: datasource: username: root password: tx@mysql@2020 url: jdbc:mysql://188.131.233.55:3306/spring_boot_topic driver-class-name: com.mysql.cj.jdbc.Driver(4)(4)实体类User
package com.kinglead.demo.domain; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName(value = "t_user") //指明数据库表名 public class User { private Long id; private String name; private Integer age; private String email; }(5)创建Mapper接口
package com.kinglead.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kinglead.demo.domain.User; //未来使用mybatis-plus的公共接口,必须继承BaseMapper public interface UserMapper extends BaseMapper<User> { }(6)创建Service接口
UserService
package com.kinglead.demo.service; import com.kinglead.demo.domain.User; import java.util.List; public interface UserService { List<User> queryUserList(); }UserServiceImpl
package com.kinglead.demo.service.impl; import com.kinglead.demo.domain.User; import com.kinglead.demo.mapper.UserMapper; import com.kinglead.demo.service.UserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public List<User> queryUserList() { //使用mybatis-plus公共查询接口完成列表查询 return userMapper.selectList(null); } }(7)创建controller
package com.kinglead.demo.controller; import com.kinglead.demo.domain.User; import com.kinglead.demo.service.UserService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Resource private UserService userService; @RequestMapping("/userList") public ModelAndView queryUserList(ModelAndView modelAndView){ List<User> userList = userService.queryUserList(); modelAndView.addObject("userList", userList); modelAndView.setViewName("userList"); return modelAndView; } }(8)用户列表页面
<!DOCTYPE html> <html xmlns:th="http:///kinglead2012/myblog以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
已有的springcloud+mybatis项目升级为mybatis-plus项目模块目录将mybatis依赖替换为mybatis-plus修改配置文件实体类如
1、使用mybatis-plus自身的查询构造去,只需要在全局配置中添加如下配置mybatis-plus:mapper-locations:classpath:
Mybatis-Plus的配置1.Maven插件velocity-engine-core是mybatis-plus自动生成代码所依赖的模板(不用自动生成代码功能
1.新建一个springboot工程2.需要导入mybatis和mybatis-plus的依赖文件com.baomidoumybatis-plus-boot-s
springboot+mybatis-plus实现内置的CRUD使用详情,具体修改删除操作内容后文也有详细说明mybatis-plus的特性无侵入:只做增强不做