springboot关于mybatis的mapper.xml文件问题
springboot关于mybatis的mapper.xml文件问题
例如有建一张user表
create table user(
id bigint auto_increment prmary key,
username varchar(50) not null,
password varchar(50) not null
)
实体类
Class User implements Serializable{
private long id;
private String username;
private String password;
//省略get set方法
}
- java注解形式
springboot整合mybatis网上教程很多省略
UserMapper.java根据id查询用户信息
@Select(select username,password from user where id=(#{id}))
User selectById(long id);
mapper文件写完后只需在springboot里添加一个扫描注解。
@MapperScan("cy.cms.application.dx.mapper")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
优点:简单快速不需要写xml注解
缺点:sql语句过长
- xml文件形式
使用****生成所需要的mapper.java mapper.xml以及pojo导入springboot项目中,在springboot的配置文件添加
mybatis.mapper-locations= classpath:mapper/*.xml!
项目内容