您的位置:首页 > 编程语言 > Java开发

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

2017-09-24 20:47 836 查看

1.配置数据源

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


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

@Repository
public 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的扫描
@SpringBootApplication
public class CommonFrameApplication {

public static void main(String[] args) {
SpringApplication.run(CommonFrameApplication.class, args);
}
}


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

@Service
public 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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springboot