SpringBoot集成mybaties

1.pom.xml

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.6</version>
		</dependency>
	</dependencies>

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<Article> 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下
两者的目录要保持一致

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值