这里只需要引入tkmapper的依赖就可以,tkmapper帮我们
tk.mybatis
mapper-spring-boot-starter
2.0.2
引入依赖之后,我们看一下通用mapper都为我们引入了什么?
1.引入了springboot-starter-jdbc 启动器,该启动器默认使用HikariCP连接池
2.引入了mybatis和spring-mybatis整合包
3.引入tkmapper
4.引入了通用mapper自动配置包
所以整合dao层我们不需要引入其他依赖(当然如果你要使用其他的数据库连接池可以排除掉springboot-starter-jdbc依赖)
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
username: root
data-password: root
mybatis:
mapper-locations: classpath:cn/csh/mapper/*.xml
type-aliases-package: cn.csh.pojo
configuration:
map-underscore-to-camel-case: true
mapper:
identity: mysql
not-empty: true
enum-as-simple-type: true
这里我没有配置数据库驱动,tkmapper根据你所引入的mysql依赖自动注入了数据库驱动属性.
第三步:在启动类上加上tkmapper的@MapperScan(basePackages={""})注解
@MapperScan(basePackages = {"cn.csh.mapper"})
@SpringBootApplication
public class SpringbootTkmapperApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTkmapperApplication.class, args);
}
}
如果你不加MapperScan注解,通用mapper只会扫描启动类同级包下的mapper接口文件,不会加载子包下的mapper接口文件
2019-01-21 20:28:08.306 WARN 9664 --- [ main] t.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[cn.csh]' package. Please check your configuration.
--我们看一下@MapperScan注解的方法
特别注意::我们在配置文件配置的是mapper.xml文件,而在启动类上配置的是mapper借口文件.
第四部:创建mapper接口文件继承通用mapper提供的接口,从而使我们的接口文件具有通用的curd操作.
package cn.csh.mapper;
import cn.csh.pojo.TbCategoryEntity;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
/**
* @Classname CategoryMapper
* @Description TODO
* @Date 2019/1/19 15:24
* @Created by Administrator
*/
@Repository//这个注解可加可不加,建议加上.
public interface CategoryMapper extends Mapper{
}
点击运行启动成功:
"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=58199 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\intellij IDEA\IntelliJ IDEA 2018.3.3\lib\idea_rt.jar=58200:D:\intellij IDEA\IntelliJ IDEA 2018.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;D:\intellij IDEA\ideaproject\springboot-tkmapper\target\classes;D:\localRepository\org\springframework\boot\spring-boot-starter\2.1.2.RELEASE\spring-boot-starter-2.1.2.RELEASE.jar;D:\localRepository\org\springframework\boot\spring-boot\2.1.2.RELEASE\spring-boot-2.1.2.RELEASE.jar;D:\localRepository\org\springframework\spring-context\5.1.4.RELEASE\spring-context-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\spring-aop\5.1.4.RELEASE\spring-aop-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\spring-beans\5.1.4.RELEASE\spring-beans-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\spring-expression\5.1.4.RELEASE\spring-expression-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\boot\spring-boot-autoconfigure\2.1.2.RELEASE\spring-boot-autoconfigure-2.1.2.RELEASE.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-logging\2.1.2.RELEASE\spring-boot-starter-logging-2.1.2.RELEASE.jar;D:\localRepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\localRepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\localRepository\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\localRepository\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\localRepository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\localRepository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\localRepository\org\springframework\spring-core\5.1.4.RELEASE\spring-core-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\spring-jcl\5.1.4.RELEASE\spring-jcl-5.1.4.RELEASE.jar;D:\localRepository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\localRepository\org\projectlombok\lombok\1.18.4\lombok-1.18.4.jar;D:\localRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\localRepository\tk\mybatis\mapper-spring-boot-starter\2.0.0\mapper-spring-boot-starter-2.0.0.jar;D:\localRepository\org\springframework\boot\spring-boot-starter-jdbc\2.1.2.RELEASE\spring-boot-starter-jdbc-2.1.2.RELEASE.jar;D:\localRepository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;D:\localRepository\org\springframework\spring-jdbc\5.1.4.RELEASE\spring-jdbc-5.1.4.RELEASE.jar;D:\localRepository\org\springframework\spring-tx\5.1.4.RELEASE\spring-tx-5.1.4.RELEASE.jar;D:\localRepository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\localRepository\org\mybatis\mybatis-spring\1.3.1\mybatis-spring-1.3.1.jar;D:\localRepository\tk\mybatis\mapper-core\1.0.0\mapper-core-1.0.0.jar;D:\localRepository\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;D:\localRepository\tk\mybatis\mapper-base\1.0.0\mapper-base-1.0.0.jar;D:\localRepository\tk\mybatis\mapper-weekend\1.1.3\mapper-weekend-1.1.3.jar;D:\localRepository\tk\mybatis\mapper-spring\1.0.0\mapper-spring-1.0.0.jar;D:\localRepository\tk\mybatis\mapper-spring-boot-autoconfigure\2.0.0\mapper-spring-boot-autoconfigure-2.0.0.jar;D:\localRepository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar" cn.csh.SpringbootTkmapperApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)
2019-01-21 20:20:17.024 INFO 7868 --- [ main] cn.csh.SpringbootTkmapperApplication : Starting SpringbootTkmapperApplication on PC-201901161305 with PID 7868 (started by Administrator in D:\intellij IDEA\ideaproject)
2019-01-21 20:20:17.026 INFO 7868 --- [ main] cn.csh.SpringbootTkmapperApplication : No active profile set, falling back to default profiles: default
2019-01-21 20:20:17.399 WARN 7868 --- [ main] t.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[cn.csh.mapper]' package. Please check your configuration.
2019-01-21 20:20:17.498 WARN 7868 --- [ main] t.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[cn.csh]' package. Please check your configuration.
2019-01-21 20:20:17.902 INFO 7868 --- [ main] cn.csh.SpringbootTkmapperApplication : Started SpringbootTkmapperApplication in 1.221 seconds (JVM running for 0.539)
Process finished with exit code 0
注意::记得已经要加入mysql依赖否则会报找不到数据库驱动类
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at cn.csh.SpringbootTkmapperApplication.main(SpringbootTkmapperApplication.java:12) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 18 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver
at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:224) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:83) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 19 common frames omitted