如Mysql,jdbc,web,mybatis 等你需要的,这是第一步,如果你需要配置tk.mybatis,你需要额外的导入 新的jar包 以及 pom.xml 的 build 中plugin generator 插件
#mybatis 配置
mybatis.type-aliases-package=com.jzj.tkdemo.domain.po
#mapper 文件存放xml
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
#启用驼峰规则将数据库 user_name 实体转化为userName
mybatis.configuration.map-underscore-to-camel-case=true
#数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=xxx
spring.datasource.password=xxx
下面是我的pom文件
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.3.RELEASE
com.jzj
tkdemo
0.0.1-SNAPSHOT
tkdemo
Demo project for Spring Boot
1.8
tk.mybatis
mapper-spring-boot-starter
1.2.4
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
org.springframework.boot
spring-boot-starter-integration
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7
mysql
mysql-connector-java
5.1.21
tk.mybatis
mapper
4.0.4
org.mybatis.generator
mybatis-generator-core
1.3.7
src/main/resources/generator/generatorConfig.xml
true
咱们先说 插件,Generator 插件最简单 ,首先你的有数据库 我的数据库 test,然后你的有 数据库的表 user, 这些创建完成后,你可以先配置插件
CREATE TABLE `user` (
`id` int(32) NOT NULL AUTO_INCREMENT COMMENT 'id',
`user_name` varchar(32) NOT NULL DEFAULT '' COMMENT '"用户姓名"',
`password` varchar(50) NOT NULL DEFAULT '' COMMENT '用户密码',
`safe` tinyint(1) NOT NULL COMMENT '是否安全',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
这个generatorConfig.xml 是 我们生成插件 所需要的配置信息 如 你的PO实体包,你的Mapper文件路径,你的实体类名称及 你的要创建sqlMapper的数据库表信息
下面是 我的 generatorConfig.xml 配置文件信息, 这个xml在 resources下面的 generator 文件夹下
package com.jzj.tkdemo.controller;
import com.jzj.tkdemo.domain.po.UserPO;
import com.jzj.tkdemo.service.ITkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 描述:
*
* @author jiazijie
* @since 2019-04-02 下午9:47
*/
@RestController
public class TkUserController {
@Autowired
private ITkUserService tkUserService;
@RequestMapping("selectUser/{id}")
private String getUser(@PathVariable String id) {
UserPO user = tkUserService.selectById(Integer.parseInt(id));
return user.toString();
}
@RequestMapping("addUser")
private int addUser() {
UserPO user = new UserPO();
user.setUserName("aaa");
user.setPassword("pwd");
user.setSafe(true);
int count = tkUserService.insertUser(user);
return count;
}
@RequestMapping("updateUser/{name}")
private void update(@PathVariable String name) {
UserPO user = new UserPO();
user.setUserName(name);
user.setPassword("pwd");
user.setSafe(true);
user.setId(1);
tkUserService.updateUser(user);
}
}
接口 ITkUserService
package com.jzj.tkdemo.service;
import com.jzj.tkdemo.domain.po.UserPO;
/**
* 描述:
*
* @author jiazijie
* @since 2019-04-03 下午10:47
*/
public interface ITkUserService {
UserPO selectById(Integer id);
int insertUser(UserPO userPO);
int updateUser(UserPO userPO);
}
实现 TKUserServiceImpl
package com.jzj.tkdemo.service;
import com.jzj.tkdemo.dao.UserPOMapper;
import com.jzj.tkdemo.domain.po.UserPO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 描述:
*
* @author jiazijie
* @since 2019-04-03 下午10:48
*/
@Service
public class TkUserServiceImpl implements ITkUserService {
@Resource
private UserPOMapper userPOMapper;
@Override
public UserPO selectById(Integer id) {
UserPO userPO = userPOMapper.selectByPrimaryKey(id);
return userPO;
}
@Override
public int insertUser(UserPO userPO) {
int count = userPOMapper.insert(userPO);
return count;
}
@Override
public int updateUser(UserPO userPO) {
int count = userPOMapper.updateByPrimaryKey(userPO);
return count;
}
}
加上Mapper 包扫描 ,扫描你自己的Mapper 文件,BaseMapper 接口千万别 写进这个包里面 @MapperScan(“com.jzj.tkdemo.dao”)
我的入口函数
package com.jzj.tkdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.jzj.tkdemo.dao")
public class TkdemoApplication {
public static void main(String[] args) {
SpringApplication.run(TkdemoApplication.class, args);
}
}
添加用户User http://localhost:8080/addUser 返回 1 添加数量
查询用户User http://localhost:8080/selectUser/1 查询 id=1 的员工信息
更新用户User http://localhost:8080/updateUser/aaabbb 将员工Id为1的 员工 姓名设置为 aaabbb
Spring bean 注入失败 Injection of resource dependencies failed NoSuchBeanDefinitionException Dependency annotations 错误:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘tkUserServiceImpl’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.jzj.tkdemo.dao.UserPOMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
这是因为 Spring 无法发现你的 mapper文件 ,在Application上加上包扫描 就可以了 @MapperScan(“com.jzj.tkdemo.dao”)
正确导入的是 import tk.mybatis.spring.annotation.MapperScan;
错误导入的是 import org.mybatis.spring.annotation.MapperScan;
报错 java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseInsertProvider.() 等信息
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL). Cause: java.lang.InstantiationException: tk.mybatis.mapper.provider.base.BaseInsertProvider] with root cause
java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseInsertProvider.()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_181]
at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_181]
at org.apache.ibatis.builder.annotation.ProviderSqlSource.createSqlSource(ProviderSqlSource.java:117) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.builder.annotation.ProviderSqlSource.getBoundSql(ProviderSqlSource.java:103) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.statement.BaseStatementHandler.(BaseStatementHandler.java:64) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.statement.PreparedStatementHandler.(PreparedStatementHandler.java:40) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.statement.RoutingStatementHandler.(RoutingStatementHandler.java:46) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:558) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) ~[mybatis-3.4.5.jar:3.4.5]
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.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) ~[mybatis-3.4.5.jar:3.4.5]
at com.sun.proxy.$Proxy83.update(Unknown Source) ~[na:na]
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) ~[mybatis-3.4.5.jar:3.4.5]
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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.1.jar:1.3.1]
at com.sun.proxy.$Proxy71.insert(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:278) ~[mybatis-spring-1.3.1.jar:1.3.1]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57) ~[mybatis-3.4.5.jar:3.4.5]
直接在你的UserPOMapper 类上加上 @Mapper 注解 也可以,但是这样 每一个Mapper都加,费力不讨好,所以还是用包扫描 比较好
这个错误比较隐晦,藏的比较深,一般大家为了方便Mapper.java 放在一起,就将BaseMapper 父类放在类Mapper 包内,自动生成的Mapper.java 也在同一个包内,方便 但是这样会导致问题,报的错误时 reflect 反射类型错误,说类转换失败 其实就是 你把BaseMapper放在类 mapper包扫描中
报错:Invocation of init method failed;
nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
tk.mybatis.mapper.MapperException 错误
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class 错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseMapper' defined in file [/Users/jiazijie/Documents/offtime/sblearn/tkdemo/target/classes/com/jzj/tkdemo/dao/BaseMapper.class]: Invocation of init method failed; nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1762) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:830) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
项目github 地址:https://github.com/jzjie007/spboot/tree/master/tkdemo