今天为了整合mybatis到我的graphql项目里面,真的是搞得我焦头烂额啊,踩坑让我成长,怎么说,这也是宝贵的学习经验,希望以后处理问题的时候可以更高效一点,先放出整合后的源代码镇帖:
https://github.com/NathanPiggy/graphql-demo-with-spring-boot
接着来总结一下今天的经验,首先我把appliction.yml配置了一下:
#Change the port of the server
server:
port: 8080
#Laod the mybatis properties
mybatis:
mapper-locations:
- classpath: mapper/UserMapper.xml
config-location: mapper/config/mybatis-config.xml
#Defind the alias
type-aliases-package: com.graphql.study.demo.bean
#Data source
spring:
datasource:
url: jdbc:mysql://localhost:3306/graphql_demo
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
添加了mybatis-config.xml和UserMapper.xml文件
UserMapper.xml
<?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="com.graphql.study.demo.dao.UserMapper">
<resultMap id="user" type="User" >
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
<result column="balance" property="balance" />
</resultMap>
<select id="getUser" parameterType="int" resultType="user">
select * from user where id=${id}
</select>
</mapper>
再建个表,插入点数据
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` smallint(6) DEFAULT NULL,
`balance` bigint(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('0', 'kity', '2', '12');
INSERT INTO `user` VALUES ('1', 'lilei', '32', '82927');
INSERT INTO `user` VALUES ('2', 'nathan', '23', '999999');
INSERT INTO `user` VALUES ('3', 'terrance', '33', '23213143');
配个Mapper
@Mapper
public interface UserMapper{
/**
* Check the user
*/
@Select("select * from user where id=#{id}")
public User getUser(@Param("id") int id);
}
在serevice那里注入一下
@Service
public class UserServiceImp implements UserService {
//Inject the usermapper proxy instance
@Autowired
private UserMapper userMapper;
@Override
public User getUser(int id) {
return userMapper.getUser(id);
}
}
再轻巧地加个依赖maven包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
天真的我以为这样就大功告成了….
没想到一启动就:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userMapper in com.graphql.study.demo.service.imp.UserServiceImp required a bean of type 'com.graphql.study.demo.dao.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.graphql.study.demo.dao.UserMapper' in your configuration.
从此就开始了我长达4个小时的研究之旅…
我开始到处搜索答案,得到的答案有:
- 1.
添加@MapperScan(basePackages="com.graphql.study.demo.dao")
嗯,的却有效,不会报找不到bean了,可是另外一个问题接踵而来
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImp': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [G:\java-eclipse(neno)\workspace_one\graphql-java-server-demo\target\classes\com\graphql\study\demo\dao\UserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
似乎变得更复杂了…需要配置,貌似方向对,暂时放弃这条路。
Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
- 接着网上又有人说包路径有问题,扫描不到mapper,好吧,我就加
@SpringBootApplication(scanBasePackages = {"controller","dao","service"})
很好,又不报错了,可是…只要mapper能被扫到,requestmapping就不起作用,晕死,换包路径也不行,放弃这条路
- 下载了几个demo到本地运行,发现同样的配置,他们启动项目的时候都不会有问题,心想不会这么邪门吧,和spring-boot-starter-parent的版本有关?于是我从
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
降到
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
运行正常…. 血吐了一地….
于是记录一下苦逼的过程,接下来要看下怎么在高版本的spring boot 上整合mybatis…不过这样看来,用jpa貌似会省事很多….
对于之前的demo,大家可以参考下
http://blog.csdn.net/gaoyib6/article/details/76088903