一、选择依赖
二、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据驱动 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
三、配置文件 application.properties
server.port=8081
# mybatis
# mybatis 扫描此路径下的pojo(实体类) 供Mapper层使用
mybatis.type-aliases-package = com.wt.mybatis.pojo
mybatis.mapper-locations=classpath:mapper
#spring
# 数据库配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=132qwe
项目目录:
demo
controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wt.mybatis.Dao.TestDao;
import org.wt.mybatis.pojo.HeroPojo;
import java.util.List;
@Controller
public class TestController {
@Autowired
private TestDao testDao;
@RequestMapping("/a")
@ResponseBody
public String Hellow(){
List<HeroPojo> result = testDao.getHero();
return "a";
}
}
Dao
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.wt.mybatis.pojo.HeroPojo;
import java.util.List;
@Mapper
public interface TestDao {
List<HeroPojo> getHero();
}
TestDaoMapper.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="org.wt.mybatis.Dao.TestDao">
<select id="getHero" resultType="org.wt.mybatis.pojo.HeroPojo">
SELECT * FROM hero
</select>
</mapper>