zoukankan      html  css  js  c++  java
  • 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下建目录不要用点“.”不然建的是一个文件夹名字。要用/分开

  • 相关阅读:
    c# 操作数据库
    dataview findrows
    C++:gethostname,gethostbyname获取IP地址和计算机名
    MQTT
    STM32操作外部SRAM
    JAVA中最常用的快捷键总结
    Zstack中End Device设备失去父节点时的重新入网处理方法(转)
    VC++ 重叠窗口
    (转载)PLC内部电路常见的几种形式
    VS2005 DoModal函数
  • 原文地址:https://www.cnblogs.com/flz-0429/p/9863490.html
Copyright © 2011-2022 走看看