步骤
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
- 选择当前模块需要使用的技术集(MyBatis\MySQL)
- 设置数据源参数
#2.配置相关信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm
username: root
password: 9999
- 定义数据层接口与映射配置
@Mapper
public interface BookDao {
@Select("select * from book where id=#{id}")
public Book getById(Integer id);
}
- 测试类注入dao接口,测试功能
@SpringBootTest
class Springboot03MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
Book book = bookDao.getById(2);
System.out.println(book);
}
}