本节内容扩展介绍下针对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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。