提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
心血来潮,想根据官网从头搭建一个最简易版全注解spring整合mybatis(仅此而已,无web等),接连几个大坑,记录一下
最后附上成功Demo代码
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in TestMabatis.MyBatisConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [TestMabatis.UserDao]: Factory method 'userDao' threw exception; nested exception is org.apache.ibatis.binding.BindingException: Type interface TestMabatis.UserDao is not known to the MapperRegistry.
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:645)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:475)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:874)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:778)
... 14 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [TestMabatis.UserDao]: Factory method 'userDao' threw exception; nested exception is org.apache.ibatis.binding.BindingException: Type interface TestMabatis.UserDao is not known to the MapperRegistry.
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640)
... 28 more
Caused by: org.apache.ibatis.binding.BindingException: Type interface TestMabatis.UserDao is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:763)
at org.mybatis.spring.SqlSessionTemplate.getMapper(SqlSessionTemplate.java:311)
at TestMabatis.MyBatisConfig.userDao(MyBatisConfig.java:60)
at TestMabatis.MyBatisConfig$$EnhancerBySpringCGLIB$$9681e4af.CGLIB$userDao$1(<generated>)
at TestMabatis.MyBatisConfig$$EnhancerBySpringCGLIB$$9681e4af$$FastClassBySpringCGLIB$$6fa77d83.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at TestMabatis.MyBatisConfig$$EnhancerBySpringCGLIB$$9681e4af.userDao(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 29 more
The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to
//URL拼接上?serverTimezone=UTC
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/testMyBatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
3.原因:原因是因为使用了Mysql Connector/J 6.x以上的版本,然后就报了时区的错误遇到的问题 servertime=UTC导致时间差8个小时(MySQL jdbc 6.0 版本以上必须配置此参数)
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>6.0.2version>
dependency>
dependencies>
package TestMabatis;
/**
* 〈功能简述〉
* 〈〉
*
* @author LXN
* @create 2021/3/31
* @since 1.0.0
*/
public class User {
private int age;
private String name;
private int id;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* 〈功能简述〉
* 〈接口〉
*
* @author LXN
* @create 2021/3/31
* @since 1.0.0
*/
public interface UserDao {
@Select("SELECT * FROM sys_user WHERE id = #{id}")
User getUser(@Param("id") int id);
}
import org.springframework.stereotype.Component;
/**
* 〈功能简述〉
* 〈〉
*
* @author LXN
* @create 2021/3/31
* @since 1.0.0
*/
@Component
public class UserServiceImpl{
private final UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public User getUser(int id) {
User user = userDao.getUser(id);
return user;
}
}
package TestMabatis;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
/**
* 〈功能简述〉
* 〈〉
*
* @author LXN
* @create 2021/3/31
* @since 1.0.0
*/
@ComponentScan("TestMabatis")
@MapperScan(value="TestMabatis")
@Configuration
public class MyBatisConfig {
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource());
SqlSessionFactory object = factoryBean.getObject();
//不加这一句,找不到Dao
object.getConfiguration().addMapper(UserDao.class);
return object;
}
@Bean
public UserDao userDao() throws Exception {
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
return sqlSessionTemplate.getMapper(UserDao.class);
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("root");
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/testMyBatis?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8");
return driverManagerDataSource;
}
}
package TestMabatis;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 〈功能简述〉
* 〈〉
*
* @author LXN
* @create 2021/3/31
* @since 1.0.0
*/
public class TestMybatisMain {
public static void main(String[] args) throws NoSuchFieldException {
AnnotationConfigApplicationContext ac =
new AnnotationConfigApplicationContext(MyBatisConfig.class);
UserServiceImpl bean = ac.getBean(UserServiceImpl.class);
User user = bean.getUser(1);
System.out.println("获取到"+user.getName());
}
}
待补充