mybaitsPlus

master
Yin Yuyang 2024-03-24 20:48:29 +08:00
parent eda20169a0
commit 234bcae970
8 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.muyu.test.common;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @ClassName Job
* @Description TODO
* @Author YinYuYang
* @Date 2024/3/24 10:33
* Version 1.0
*/
@Data
@TableName("t_job")
public class Job {
@TableField(exist = false)
private Integer jobId;
// @TableField(exist = false)
private String jobName;
}

View File

@ -0,0 +1,37 @@
package com.muyu.test.common;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
* @ClassName People
* @Description TODO
* @Author YinYuYang
* @Date 2024/3/24 10:33
* Version 1.0
*/
@Data
@TableName("t_people")
public class People extends Job{
@TableId
private Integer peopleId;
private String peopleName;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date peopleBirth;
private Integer jobId;
private String peopleImg;
private String peopleIntroduce;
}

View File

@ -3,6 +3,7 @@ package com.muyu;
import com.muyu.common.security.annotation.EnableCustomConfig;
import com.muyu.common.security.annotation.EnableMyFeignClients;
import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableCustomSwagger2
@EnableMyFeignClients
@SpringBootApplication
@MapperScan(basePackages = "com.muyu.test.mapper")
public class MuYuTestApplication {
public static void main (String[] args) {
SpringApplication.run(MuYuTestApplication.class, args);

View File

@ -0,0 +1,49 @@
package com.muyu.test.controller;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.test.common.People;
import com.muyu.test.service.PeopleService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName PeopleController
* @Description TODO
* @Author YinYuYang
* @Date 2024/3/24 10:44
* Version 1.0
*/
@RestController
@RequestMapping("/people")
public class PeopleController extends BaseController {
@Autowired
private PeopleService peopleService;
@GetMapping("/list")
public Result<TableDataInfo<People>> list(People people) {
List<People> list = peopleService.selectAll(people);
return getDataTable(list);
}
@PostMapping("/insert")
public Result add(@RequestBody People people){
int i = peopleService.insert(people);
return Result.success(i);
}
@PostMapping("/update")
public Result update(@RequestBody People people){
int i = peopleService.updatePeople(people);
return Result.success(i);
}
@PostMapping("/delete/{peopleId}")
public Result remove(@PathVariable Integer peopleId) {
int i = peopleService.delete(peopleId);
return Result.success(i);
}
}

View File

@ -0,0 +1,16 @@
package com.muyu.test.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.test.common.People;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
public interface PeopleMapper extends BaseMapper<People> {
@Select("select * from t_people left join t_job on t_job.job_id = t_people.job_id ")
List<People> selectAll();
List<People> selectPeople(People people);
}

View File

@ -0,0 +1,16 @@
package com.muyu.test.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.test.common.People;
import java.util.List;
public interface PeopleService extends IService<People> {
List<People> selectAll(People people);
int insert(People people);
int updatePeople(People people);
int delete(Integer peopleId);
}

View File

@ -0,0 +1,62 @@
package com.muyu.test.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.redis.configure.RedisConfig;
import com.muyu.test.mapper.PeopleMapper;
import com.muyu.test.service.PeopleService;
import com.muyu.test.common.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* @ClassName PeopleImpl
* @Description TODO
* @Author YinYuYang
* @Date 2024/3/24 10:44
* Version 1.0
*/
@Service
public class PeopleServiceImpl extends ServiceImpl<PeopleMapper, People>
implements PeopleService {
@Autowired
private PeopleMapper peopleMapper;
@Override
public List<People> selectAll(People people) {
// LambdaQueryWrapper<People> queryWrapper = new LambdaQueryWrapper<>();
//
// if (StringUtils.isNotEmpty(people.getPeopleName())){
// queryWrapper.like(People::getPeopleName, people.getPeopleName());
// }
// if (people.getJobId() != null){
// queryWrapper.eq(People::getJobId, people.getJobId());
// }
//
// Map<String, Object> paramMap = new HashMap<>();
// String sqlSegment = queryWrapper.getCustomSqlSegment();
// paramMap.put("sqlSegment", sqlSegment);
return peopleMapper.selectPeople(people);
}
@Override
public int insert(People people) {
return peopleMapper.insert(people);
}
@Override
public int updatePeople(People people) {
return peopleMapper.updateById(people);
}
@Override
public int delete(Integer peopleId) {
return peopleMapper.deleteById(peopleId);
}
}

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
1.在mybats的开发中namespace有特殊的意思一定要是对应接口的全限定名
通过namespace可以简历mapper.xml和接口之间的关系(名字不重要,位置不重要)
-->
<mapper namespace="com.muyu.test.mapper.PeopleMapper">
<!-- 添加 -->
<resultMap id="PeopleResult" type="com.muyu.test.common.People">
<id property="peopleId" column="people_id" />
<result property="peopleName" column="people_name" />
<result property="peopleBirth" column="people_birth" />
<result property="jobId" column="job_id" />
<result property="peopleImg" column="people_img" />
<result property="peopleIntroduce" column="people_introduce" />
<result property="jobName" column="job_name" />
</resultMap>
<sql id="selectPeople">
SELECT
p.people_id,
p.people_name,
p.people_birth,
p.job_id,
p.people_img,
p.people_introduce,
j.job_name
FROM
t_people p
LEFT JOIN t_job j ON p.job_id = j.job_id
</sql>
<select id="selectPeople" resultType="com.muyu.test.common.People" resultMap="PeopleResult">
<include refid="selectPeople" />
<where>
<if test="peopleName != null and peopleName != ''">
AND p.people_name like concat('%', #{peopleName}, '%')
</if>
<if test="jobId != null">
AND p.job_id = #{jobId}
</if>
</where>
</select>
</mapper>