推荐使用intellij idea作为开发工具,使用intellij idea创建spring boot框架,如下图所示,添加maven依赖,代码如下 org.mybatis.spring.bootmybatis-spring-boot-starter1.3.2mysqlmysql-connector-javaruntime
创建一个TestMapper.java文件,代码如下/*** Created by zuli on 2018/10/10.*/public interface TestMapper { int test();}
在resources目录下创建TestMapper.xml文件,代码如下 SELECT 1 FROM DUAL
添加@MapperScan()注解,配置basePackages属性,代码如下@SpringBootApplication@MapperScan(basePackages = "com.zuli.mapper")public class SpringBootMybatisApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringBootMybatisApplication.class, args); TestMapper testMapper = context.getBean(TestMapper.class); System.out.println(testMapper.test()); }}
修改application.properties配置文件,配置数据源,mapper xml文件路径等代码如下spring.datasource.url=jdbc:mysql://localhost/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.mapper-locations=classpath:*Mapper.xml
启动应用程序,调用TestMapper的test()方法,打印日志,如下图所示
整体demo的项目结构如下图所示
标签:spring,boot,mybatis