SpringBoot集成MyBatis(主要用来方便的进行自定义一些sql查询,主要的简单的数据库操作还是依赖于自身提供的JPA)

来源:互联网 发布:tts软件 编辑:程序博客网 时间:2024/06/06 20:51

1.配置数据源

spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=xdd123$%^spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2.定义mapper即接口,相当于dao层(这里是基于注解实现)

@Repositorypublic interface DemoMapper {@Select("select * from user where id=#{id}")User getUserById(@Param("id") int id);@Select("select * from user")List<Map<String,Object>> getUsers();}

3.配置mapper扫描

@MapperScan("com.yangle.mapper")//在启动项中开启mapper的扫描@SpringBootApplicationpublic class CommonFrameApplication {    public static void main(String[] args) {        SpringApplication.run(CommonFrameApplication.class, args);    }}

4.现在可以在service层进行调用了

@Servicepublic class UserServiceImpl implements IUserService {    @Autowired    private DemoMapper demoMapper;//自动装配mapper    @Override    public User getUsers(int id) {        return demoMapper.getUserById(id);//调用mapper里的方法,操作数据库    }    @Override    public List<Map<String, Object>> getUsers() {        return demoMapper.getUsers();    }}
阅读全文
0 0
原创粉丝点击