springboot和mybatis整合2(mapper接口动态代理)
1.启动类
@SpringBootApplication
public class Applications {
public static void main(String[] args) {
SpringApplication.run(Applications.class, args);
}
}
2.controller
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private IuserService userService ;
@RequestMapping("/login")
public User login(Integer id) {
User user = userService.selectOne(id);
System.out.println(user);
return user;
}
3service
@Service
public class UserImpl implements IuserService {
@Autowired
private Idao dao;
public User selectOne(Integer id) {
return dao.selectOne(id);
}
4-dao(mapper接口)
@Mapper
public interface Idao {
//@Select("SELECT * FROM USERS WHERE id = #{id}") 这种就不需要xxxmapper.xml了
public User selectOne(Integer id);
}
5:-
spring.datasource.url=jdbc:mysql://localhost:3306/test2
spring.datasource.username=root
spring.datasource.password=0000
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations= classpath:mapper/*.xml
注意点:接口方式在mapper.xml中命名空间可sql的id要和接口的全限定名和方法名对应,需要引入mapper.xml 就加上mybatis.mapper-locations= classpath:mapper/*.xml,但是注意如果是多层目录(com/mayi/xxxmapper.xml)在resource下建目录不要用点“.”不然建的是一个文件夹名字。要用/分开