SpringBoot整合通用Mapper

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/didi7696/article/details/80164360

Mapper的作用:自动生成增删改查的SQL语句 大大减化对单表的操作

项目启动类:

@SpringBootApplication
@MapperScan(basePackages = {"yiche.com.mapper"})
/*

@MapperScan仅扫描业务接口包,不能扫描本地通用Mapper接口包,
  否则报java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl
        cannot be cast to java.lang.Class异常
*/

public class MapperApplication {
    public static void main(String[] args){

        SpringApplication.run(MapperApplication.class,args);
    }
}

}

application.properties配置

  #数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#mybatis&&通用Mapper
#扫描实力类的所在包
mybatis.type-aliases-package=yiche.com.entity
#如果不是纯注解 mapper接口对应的.xml文件
#mybatis.mapper-locations=classpath:mapper/*.xml
#通用mapper的所在接口名称 不只是包名
mapper.mappers=yiche.com.mappers.MyMapper
#数据库
mapper.identity=MYSQL

#分页插件
#pagehelper.helperDialect=mysql
#pagehelper.reasonable=true
#pagehelper.supportMethodsArguments=true
#pagehelper.params=count=countSql




#在格式:logging.level.Mapper类的包=debug  会在控制台打印出sql语句
logging.level.yiche.com.mapper=debug

项目目录
这里写图片描述

mappers是通用mapper的所在包 mapper是自己mapper的所在包 mapper继承通用mapper

通用mapper需要继承这两个接口
通用mapper接口

/*
        Mapper接口:基本的增、删、改、查方法
        MySqlMapper:针对MySQL的额外补充接口,支持批量插入*/
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T> {

}

自己 的mapper接口

@Mapper
@Component
public interface EmployeeMapper extends MyMapper<Employee>{

     //在使用通用Mapper的基础上 下面的都是mybatis注解的方法
     @Select("select * from employee")
     List<Employee> getAll();
}

Controller:

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        System.out.println("ok");
        return "ok";

    }

    /*添加 employee*/
    @RequestMapping("/add")
    @ResponseBody
    public String add() {
        Employee employee = new Employee("jack", 11000.0, 22);
        employeeMapper.insert(employee);
        return "ok";

    }

    /*批量插入 employee*/
    @RequestMapping("/bachAdd")
    @ResponseBody
    public String bachAdd() {

        Employee employee = new Employee("jack", 11000.0, 22);
        List<Employee> list = new ArrayList<>();
        Employee e = new Employee();
    /*插入五条*/
        for (int i = 0; i < 5; i++) {
            e.setName("cici");
            e.setSalary(13000.0);
            e.setAge(i + 20);
            list.add(e);
        }
        employeeMapper.insertList(list);
        return "ok";

    }


    /*更据唯一主键查询*/
    @RequestMapping("/getById")
    @ResponseBody
    public Employee getById() {
        //更据唯一主键 或者唯一编号的查询只返回一个结果
        Employee employee = new Employee();
        employee.setId(1);


        employee = employeeMapper.selectOne(employee);
        return employee;

    }


    /*更据name查询*/
    @RequestMapping("/getByName")
    @ResponseBody
    public List<Employee> selectByName() {
        //普通条件查询可含有多条件   查询name=cc age=100的employee
        Employee employee = new Employee();
        employee.setName("cici");
        employee.setAge(100);
        List<Employee> list = employeeMapper.select(employee);

        return list;

    }


    /*更新 employee*/
    @RequestMapping("/update")
    @ResponseBody
    public String update() {
        //Selective 如果有这个值的话就更新  否则不更新

        Employee employee = new Employee(1, "mm", 1.0, 1);

        //根据主键更新
        int i = employeeMapper.updateByPrimaryKeySelective(employee);
        return i + "";


    }


    /*QBC查询*/
    @RequestMapping("/queryByExample1")
    @ResponseBody
    public List<Employee> queryByExample1(){

        Example example=new Example(Employee.class);
        Example.Criteria criteria=example.createCriteria();
        //名字中含有c的员工  传入实体类的属性值
        criteria.andLike("name","%c%");
        //传入criteria
        return employeeMapper.selectByExample(example);

    }


     /*QBC查询   传入两个criterial*/
     @RequestMapping("/queryByExample2")
     @ResponseBody
    public List<Employee> queryByExample2(){

        Example example=new Example(Employee.class);
        Example.Criteria criteria=example.createCriteria();

        Example.Criteria criteria2=example.createCriteria();
        criteria.andLike("name","%c%");
        //名字中含有c的员工  传入实体类的属性值

         criteria2.andLike("name","%m%");
        //加入第二个criterial
         example.or(criteria2);

        //传入criteria
        return employeeMapper.selectByExample(example);

    }




    /*QBC查询  去重*/
    @RequestMapping("/queryByExample3")
    @ResponseBody
    public List<Employee> queryByExample3(){

        Example example=new Example(Employee.class);
        Example.Criteria criteria=example.createCriteria();
        //id 大于4的员工
        criteria.andGreaterThan("id","4");
        //排序
        example.orderBy("name").asc();
        //去重
        example.setDistinct(true);
        //select name,age from employee
        example.selectProperties("id","name","age");
        //传入criteria
        return employeeMapper.selectByExample(example);

    }


  /*以上是通用Mapper的方法*/
  /*......................................................................*/
  /*下面是mybatis自定义sql语句的方法*/

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Employee> getAll() {
       /*select * from employee*/
        return employeeMapper.getAll();

    }

}

pom.xml

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!--mapper-->
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>1.1.0</version>
    </dependency>
    <!--pagehelper-->


    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.25</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
展开阅读全文

没有更多推荐了,返回首页