目录
2、启动类上添加@MapperScan("com.baidu.demo.dao") mapper文件扫描注解
spring创建工程
工程创建方式
使用Spring Initializr方式创建,参考改文章进行创建:https://blog.csdn.net/qq_34205356/article/details/81098354
项目依赖
<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.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
yml文件配置
刚建立工程,测试访问controller,配置文件没有写入数据源配置的情况下,需要将mybatis依赖注释掉,不然会报异常。
server:
port: 9099
spring:
application:
# 应用服务的名字,在eureka中相互调用的时候就是参照这个名字
name: 111-demo
datasource:
url: jdbc:mysql://localhost:3306/dygacontent?characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false #本地环境
username: root
password: 111 #本地数据库
mybatis:
mapper-locations: com/deyi/demo/dao/*/*.xml #映射文件路径
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com/deyi/demo/po
dao层mapper映射文件编写
1、接口要添加@mapper注解
2、启动类上添加@MapperScan("com.baidu.demo.dao") mapper文件扫描注解
3、编写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">
<!--namespace 此处与接口类路径保持一致-->
<mapper namespace="com.deyi.demo.dao.DemoMapper">
<!--id与方法名称保持一致-->
<select id="selectAll" resultType="com.deyi.demo.po.ContentAttribute">
SELECT
c.news_id newsId,
c.title title
FROM
content_attribute c
</select>
</mapper>
问题点:
报发现不了配置文件异常,查看class中是否有配置xml文件,没有则参照以下文章解决:http://www.jianshu.com/p/e9cd54163556