之前在项目中整合了mybatis,做了一个小的查询接口,这次想要尝试一下Mybatis Plus,同时也是想了解一下其与mybatis之间的不同之处。
POM文件
首先是pom文件的配置,包括了mybatis-plus的配置以及代码生成器的配置,如果不需要使用代码生成器,可以不加
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
数据库配置
数据库的配置和mybatis一样,也是在application.properties中添加,由于近期接触的项目使用的是pgsql数据库,所以将之前的oracle数据库替换掉了
spring.datasource.driverClassName = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://127.0.0.1:5432/test
spring.datasource.username = test
spring.datasource.password = test
pgsql的驱动,需要在pom文件中配置,如下
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
</dependency>
代码编写
首先需要在启动类中增加MapperScan注解,注解内容和mybatis是一样的,因为我之前代码里使用的是mybatis,因此就不需要修改
package gx.first;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("gx.first.dao")
@EnableScheduling
public class Example {
public static void main(String[] args){
SpringApplication.run(Example.class, args);
}
}
其中WebUserInfo是一个数据表实体类
```java
package gx.first.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@TableName("WEB_USER_INFO")
@Data
public class WebUserInfo {
private Integer id;
private String userName;
private String phoneNo;
private String eMail;
private String password;
private String realName;
private String comName;
private String address;
private String status;
private Date createTime;
private Date updateTime;
}
需要注意的是,要加上@TableName注解,注解中的WEB_USER_INFO就是数据库表名,此外@Data注解是引入的lombok插件,可自动构造get、set方法,在pom文件中引入下面内容即可
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后就是dao的开发,之前使用mybatis时创建的MybatisTestDao没有动,又重新创建了一个WebUserInfoDao接口,其中最重要的是要继承BaseMapper类;dao创建好后,mybatis-plus封装的一些查询就已经是可以使用的了,具体的封装方法可参考官网说明:https://baomidou.com/pages/49cc81/#service-crud-%E6%8E%A5%E5%8F%A3
dao中我也自定义了一个查询方法
package gx.first.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import gx.first.entity.WebUserInfo;
import org.apache.ibatis.annotations.Param;
public interface WebUserInfoDao extends BaseMapper<WebUserInfo> {
WebUserInfo selectUserInfo(@Param("userName") String userName);
}
下面针对自定义的查询方法,我们需要创建Mapper文件,Mapper文件和mybatis并没有什么区别,需要注意的就是namespace要对应到我们的Dao接口上
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="gx.first.dao.WebUserInfoDao">
<select id="selectUserInfo" resultType="gx.first.entity.WebUserInfo">
select
id,
user_name userName,
phone_no phoneNo,
e_mail eMail,
password,
real_name realName,
com_name comName,
address,
status,
create_time createTime,
update_time updateTime
from web_user_info
where user_name = #{userName}
</select>
</mapper>
Dao和Mapper都做完了,还有重要的一步需要配置,就是Mapper的路径,和mybatis一样,也是在配置文件application.properties中配置
mybatis-plus.mapper-locations=classpath*:mybatis/mapper/*.xml
如果不加这个配置,mybatis-plus封装的查询方法不会报错,但是自定义的查询方法会报BindingException异常
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): gx.first.dao.WebUserInfoDao.selectUserInfo
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.10.jar:3.5.10]
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:50) ~[mybatis-plus-core-3.5.2.jar:3.5.2]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111) ~[mybatis-plus-core-3.5.2.jar:3.5.2]
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_181]
at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:115) ~[mybatis-plus-core-3.5.2.jar:3.5.2]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98) ~[mybatis-plus-core-3.5.2.jar:3.5.2]
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) ~[mybatis-plus-core-3.5.2.jar:3.5.2]
at com.sun.proxy.$Proxy73.selectUserInfo(Unknown Source) ~[na:na]
at gx.first.service.imp.MybatisTestServiceImp.getUserByName(MybatisTestServiceImp.java:31) ~[classes/:na]
at gx.first.controller.MybatisTestController.getUserByName(MybatisTestController.java:36) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.16.jar:8.5.16]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.16.jar:8.5.16]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.16.jar:8.5.16]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
测试接口
查询相关的代码都已经做完了,下面我们做controller和service层代码,对功能进行测试 MybatisTestController
package gx.first.controller;
import gx.first.entity.WebUserInfo;
import gx.first.service.MybatisTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/mybatisTestController")
public class MybatisTestController {
@Autowired
MybatisTestService mybatisTestService;
@ResponseBody
@GetMapping("/getUserByKey")
public WebUserInfo getUserByKey(@RequestParam Integer id){
WebUserInfo webUserInfo = mybatisTestService.getUserByKey(id);
return webUserInfo;
}
@ResponseBody
@GetMapping("/getUserByName")
public WebUserInfo getUserByName(@RequestParam String userName){
WebUserInfo webUserInfo = mybatisTestService.getUserByName(userName);
return webUserInfo;
}
}
MybatisTestService
package gx.first.service;
import gx.first.entity.WebUserInfo;
public interface MybatisTestService {
WebUserInfo getUserByKey(Integer id);
WebUserInfo getUserByName(String userName);
}
MybatisTestServiceImp
package gx.first.service.imp;
import gx.first.dao.MybatisTestDao;
import gx.first.dao.WebUserInfoDao;
import gx.first.entity.WebUserInfo;
import gx.first.service.MybatisTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MybatisTestServiceImp implements MybatisTestService {
@Autowired
WebUserInfoDao webUserInfoDao;
@Override
public WebUserInfo getUserByKey(Integer id){
return webUserInfoDao.selectById(id);
}
@Override
public WebUserInfo getUserByName(String userName){
return webUserInfoDao.selectUserInfo(userName);
}
}
测试mybatis-plus封装方法 测试自定义查询方法
小结
当前仅是对mybatis-plus进行了简单的使用,并未如何深入,因此谈不上对其有过多的了解,但仅从目前来看,mybatis-plus自己封装的一些方法确实是会给我们带来一些便利,但也仅限一些简单的查询,如果是对于复杂的系统或查询,mybatis-plus是否有优势,还需要进一步的学习和了解。
|