mybatis基本操作环境准备
需要在下面的文件里连接数据库
配置sql语句提示,在此过程中右侧要连接数据库
下面是一个查询的小案例
你需要在实现sql语句的接口这么写相应代码
点击查看代码
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface EmpMapper {
@Select("select name,gender,password from tb_emp")
public List<Emp> select();
}
再写一个实现类
点击查看代码
package com.itheima.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
Integer id;
String userName;
String name;
String password;
Short gender;
String image;
Short possition;
LocalDate pDate;
LocalDateTime createTime;
LocalDateTime updateTime;
Short dept;
}
点击查看代码
package com.itheima;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
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 SpringBootMabitsApplicationTests {
@Autowired
EmpMapper empMapper;
@Test
public void selectTest(){
List<Emp> empList=empMapper.select();
empList.forEach(user->{
System.out.println(user);
});
}
}