-
创建项目模块,直接对mybatis和mysql进行勾选
勾选后,项目已经导入了mybatis相关的坐标。 -
编写配置文件:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
username: root
password: root
- 写代码测试
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
private String sex;
private String email;
}
@Mapper
public interface UserDao {
@Select("select * from t_user where id = #{id}")
public User getBookById(Integer id);
}
@Autowired
private UserDao userDao;
@Test
void contextLoads() {
User user = userDao.getBookById(6);
System.out.println(user);
}