Spring boot 集成Mybatis遇到的一些坑
最近在学习spring boot 集成Mybatis 做一个小的demo,按照网上的方法,开始开工了。
首先开始进行依赖,在pom.xml添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <!-- spring boot 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring boot 单元测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- spring boot 和 mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- mysql的依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> </dependencies>
OK,开始添加一个properties文件,网上大部分用的是yml,不过个人喜欢properties。那么问题来了,这个properties文件应该放在哪里?
关于这个问题我就无语了,我网上浏览的那些网站论坛上都没有提到,只是说properties文件内容应该如何配置,默认你知道放在哪里了。
可是我不知道啊,后来又查了查发现,哦,应该放在src/main/resources路径下,真想揍死他们,说也不说清楚,如果我会配置还用看你们的啊。
好,接下来就是配置内容了
application.properties,这个名字不要随便该(大神就算了),spring boot会自动扫描的。
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapperLocations=classpath*:mapper/*.xml //这个是扫描哪个包下的mapper.xml文件,个人比较喜欢用mapper.xml写sql语句
好,写一个程序的入口
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
写一个和数据库表映射的bean类,
public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public User() { super(); } }
dao层的接口
import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserDao { void addUser(User user); }
注意,这个注解@Mapper是必须要添加的,不然启动的时候会报错
mapper.xml文件
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.mts.UserDao"> <insert id="addUser" parameterType="com.mts.User"> insert into User values(#{id},#{name},#{age}) </insert> </mapper>
controller层
@RestController @RequestMapping("/springboot") public class HelloWorldController { @Autowired private UserDao dao; @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String sayWorld(@PathVariable("name") String name) { User user = new User(1, name, 20); dao.addUser(user); return "Hello " + name; } }
我就没有写service层,因为这个只是一个demo,直接调用
好,基本都写好了,我们来启动一下
Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:70) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:231) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:183) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:42) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat.dataSource(DataSourceConfiguration.java:56) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 57 common frames omitted
o,no,为什么会不能加载mysql的驱动类,大神写的博客都没有提到,这个怎么办?找了半天原因,原来是mysql驱动的问题,就5.1.41这个版本不能用,可以换到其他版本,如38,39,40等版本都能用,
就41版本不能用,同学们请注意了。
我遇到的坑就这些了,然后启动,发现没问题,
其实坑不多,但是遇到坑了之后要学会自己解决问题,因为这些问题前辈们都遇到过了。