学习笔记之springBoot小工程()(逆向工程中的动态sql)

第一步:在数据库新建一张表

1,选择数据库,新建一张表

create table users(
id int(50),
name char(50),
sex char(50),
age int(50)
);

2,插入数据

insert into users values(1,"张三","male",22);
insert into users values(2,"王三","male",20);
insert into users values(3,"张五","female",22);
insert into users values(4,"李三","female",25);

表如图:

学习笔记之springBoot小工程()(逆向工程中的动态sql)_第1张图片

(一)动态SQl:if语句

第二步:在工程里建一个controller

DynamicController.class

package com.neimin.test.controller;

import com.neimin.test.model.DynamicUser;
import com.neimin.test.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    DynamicService dynamicService;
    @RequestMapping("/")
    @ResponseBody
    public List aaa(){
        DynamicUser dynamicUser = new DynamicUser();
        dynamicUser.setUsername("张三");
        dynamicUser.setSex("male");
        System.out.println("...dynamic"+dynamicUser);
        List dynamic = dynamicService.getDynamic(dynamicUser);

        return dynamic;
    }
}

第三步:在工程里建一个service

DynamicService.class

package com.neimin.test.service;

import com.neimin.test.dao.DynamicMapper;
import com.neimin.test.model.DynamicUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DynamicService {


    @Autowired
    DynamicMapper dynamicMapper;
    public List getDynamic(DynamicUser dynamicUser){
        return dynamicMapper.getDynamic(dynamicUser);
    }
}

第四步:在工程里建一个接口Mapper

DynamicMapper.class
package com.neimin.test.dao;

import com.neimin.test.model.DynamicUser;
import org.springframework.stereotype.Repository;

import java.util.List;



@Repository
public interface DynamicMapper {
    
    List getDynamic(DynamicUser dynamicUser);


}

第五步:在工程里建一个实体类

DynamicUser.class
package com.neimin.test.model;

public class DynamicUser {
    private int id;
    private String username;
    private String sex;
    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "DynamicUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

第六步:在工程里建一个Mapper.xml

mapper/DynamicMapper.xml




    

 

你可能感兴趣的:(学习笔记之springBoot小工程()(逆向工程中的动态sql))