SpringBoot集成mybaties

1.pom.xml


		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.1.0
		

		
			org.springframework.boot
			spring-boot-devtools
			runtime
			true
		
		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.projectlombok
			lombok
			1.18.6
		
	

2.application.properties

#MySQL
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=FALSE
spring.datasource.username=root
spring.datasource.password=1234

3.mapper.xml

mapper.xml的namespace要写所映射接口的全称类名,而且要和Mapper类对应好!
mapper.xml中的每个statement的id要和接口方法的方法名相同
mapper.xml中定义的每个sql的parameterType要和接口方法的形参类型相同
mapper.xml中定义的每个sql的resultType要和接口方法的返回值的类型相同
mapper.xml要和对应的mapper接口在同一个包下
mapper.xml的命名规范遵守: 接口名+Mapper.xml

4.mapper接口

@Mapper   //记得加上注解
public interface ArticleMapper {
    List
listAllNotWithContent(); }

5.启动类

@SpringBootApplication(scanBasePackages = {"com.lemon.vueblog.*"})
@MapperScan("com.lemon.vueblog.mapper")
public class VueblogApplication {

	public static void main(String[] args) {
		SpringApplication.run(VueblogApplication.class, args);
	}

}

6.关于idea注入mapper报错的解决方法

//在autowire中添加如下属性
@Autowired(required = false)

7.在JDBC使用的时候有时候会出现java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents........的错误解决办法

在url后加上?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=FALSE
示例如下
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/forest_blog?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=FALSE

8.报错:Invalid bound statement (not found)的解决办法

mapper接口放在src下
mapper.xml放在resource下
两者的目录要保持一致

SpringBoot集成mybaties_第1张图片

你可能感兴趣的:(springboot学习)