依賴配置pom.xml
<!--父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<!--web工程所需依賴包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis集成依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!--MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
配置數(shù)據(jù)源
在application.properties中加入如下配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/pinyougoudb
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:com/itheima/mapper/*Mapper.xml
mybatis.type-aliases-package=com.itheima.domain
創(chuàng)建UserMapper.java接口
接口上需要加上@Mapper注解鲤看,標(biāo)注該接口屬于MyBatis接口缘揪。
@Mapper
public interface UserMapper {
List<User> list();
}
創(chuàng)建UserMapper.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" >
<mapper namespace="com.itheima.mapper.UserMapper" >
<!--查詢所有-->
<select id="list" resultType="User">
select * from tuser
</select>
</mapper>
引導(dǎo)類加上包掃描
引導(dǎo)類上需要加上@MapperScan(basePackages = “com.itheima.mapper”)
@SpringBootApplication
@MapperScan(basePackages = "com.itheima.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}