1.引入依赖
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java
2.添加数据源配置
在application.properties文件中添加数据库配置
spring.datasource.url=jdbc:mysql://192.168.0.112:3306/secms_ods?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Spring Boot 会自动加载 spring.datasource.*
相关配置,数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中。
3.数据层编码
public interface SysUserMapper { User findUserByName(String userName);}
mapper文件:SysUserMapper.xml
添加mybatis基础配置:mybatis-config.xml
在application.properties中配置加载mapper文件
mybatis.config-location=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations=classpath:mybatis/mapper/*.xml
在启动类中增加@MapperScan扫描配置mapper类路径
@SpringBootApplication@ServletComponentScan@MapperScan("com.jiafeng.dao")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
至此可以在service层调用数据层房屋数据库。