1 pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2 application.properties
# Mysql数据库连接配置 : com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.101.111:3306/springbootdata?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#开启驼峰命名匹配映射
mybatis.configuration.map-underscore-to-camel-case=true
3 mapper
//标识该接口是mybatis的接口文件,并且让springboot能够扫描到该接口,生成该接口的代理对象,存到容器中
@Mapper
public interface CommentMapper {
//根据id查询对应评论信息
@Select("select * from t_comment where id = #{id}")
public Comment findById(Integer id);
}
此处也可以不用添加@Mapper 注解 在启动类上面添加@MapperScan
@MapperScan("com.demo.mapper")
@SpringBootApplication
public class SpringbootDemoDataApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoDataApplication.class, args);
}
}
4 test
@RunWith(SpringRunner.class)
@SpringBootTest
class Springboot03DataApplicationTests {
@Autowired
private CommentMapper commentMapper;
@Test
void contextLoads() {
Comment comment = commentMapper.findById(1);
System.out.println(comment);
}