代码结构图:
核心的是BootApplication.java,CostMapper.java
BootApplication.java
package org.boot.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("org.boot.test.mapper")
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
CostMapper.java
package org.boot.test.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import org.boot.test.entity.Cost;
public interface CostMapper {
@Select("SELECT * FROM cost")
@Results({
@Result(property="id", column="id"),
@Result(property="dateTime" , column="date" ,javaType=String.class , jdbcType=JdbcType.TIME),
@Result(property="costFood",column="food_cost"),
@Result(property="costExtra", column="service_cost"),
@Result(property="costTotal", column="total_cost")
})
List<Cost> getAll();
}
maven settings先配置国内镜像:
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
application.properties配置
mybatis.type-aliases-package:com.boot.test.entity
spring.datasource.url=jdbc:mysql://localhost:3307/mydb
spring.datasource.username=root
spring.datasource.password=root
maven
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>