目录
入门
1.准备工作(创建springboot工程,数据库表user,实体类user)
2.引入Mybatis 的相关依赖,配置Mybatis
3.编写SQL语句(注解/xml)
创建工程时直接引入mybatis依赖:
dao层编写mapper:
package com.chuangzhou.dao;
import com.chuangzhou.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper //在运行时,会自动生成该接口的实现类对象(代理对象),并且将对象交给IOC容器管理
public interface UserMapper {
//查询全部用户信息
@Select("select * from user")
public List<User> list();
}
application.properties 配置数据库连接信息:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.184.152:3306/mybatis
spring.datasource.username=root
spring.datasource.password=123456
运行springboot的测试类:
package com.chuangzhou;
import com.chuangzhou.dao.UserMapper;
import com.chuangzhou.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisQuickstartApplicationTests {
@Autowired
private UserMapper userDao;
@Test
public void listUser(){
List<User> list = userDao.list();
System.out.println(list);
}
}
标签:Springboot,spring,list,org,mybatis,import,com,chuangzhou
From: https://www.cnblogs.com/czzz/p/17610156.html