pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.5.12</ version>
< relativePath/>
</ parent>
< groupId> com.ishangu</ groupId>
< artifactId> SpringBoot_Mybatis_plus</ artifactId>
< version> 0.0.1-SNAPSHOT</ version>
< name> SpringBoot_Mybatis_plus</ name>
< description> Demo project for Spring Boot</ description>
< properties>
< java.version> 1.8</ java.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.3.2</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid-spring-boot-starter</ artifactId>
< version> 1.1.22</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
</ plugin>
</ plugins>
</ build>
</ project>
application.properties
#server.port=8888
#由于boot默认使用的是tomcat的数据源所以,这里我们需要制定数据源的类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 配置数据源
spring.datasource.url=jdbc:mysql:///house
spring.datasource.password=123456
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定小mapper文件的位置
#mybatis.mapper-locations=mappers/*.xml
@SpringBootApplication
package com.ishangu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.ishangu.dao")
@SpringBootApplication
public class SpringBootMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusApplication.class, args);
}
}
model
package com.ishangu.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
//@Entity
//@Table(name = "good")
@TableName("good")
public class Good {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer parentid;
private String typeno;
private String typename;
public Good(Integer id, Integer parentid, String typeno, String typename) {
this.id = id;
this.parentid = parentid;
this.typeno = typeno;
this.typename = typename;
}
public Good() {
super();
}
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return parentid
*/
public Integer getParentid() {
return parentid;
}
/**
* @param parentid
*/
public void setParentid(Integer parentid) {
this.parentid = parentid;
}
/**
* @return typeno
*/
public String getTypeno() {
return typeno;
}
/**
* @param typeno
*/
public void setTypeno(String typeno) {
this.typeno = typeno;
}
/**
* @return typename
*/
public String getTypename() {
return typename;
}
/**
* @param typename
*/
public void setTypename(String typename) {
this.typename = typename;
}
}
dao
package com.ishangu.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ishangu.model.Good;
public interface GoodMapper extends BaseMapper<Good> {
//dao层可以为空
}
service
package com.ishangu.service;
import com.ishangu.model.Good;
import java.util.List;
public interface GoodService {
void save(Good good);
void update(Good good);
void delete(Integer id);
Good login(Integer id,Integer parentid);
Good login2(Integer id,Integer parentid);
List<Good> findByTypenameLike(String typename);
List<Good> findAll();
}
serviceimpl
package com.ishangu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ishangu.dao.GoodMapper;
import com.ishangu.model.Good;
import com.ishangu.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GoodServiceImpl implements GoodService {
@Autowired(required = false)
GoodMapper repository;
@Override
public void save(Good good) {
//repository.insert(good);
//repository.save(good);
repository.insert(good);
}
@Override
public void update(Good good) {
//repository.updateByExample(good,null);
//repository.save(good);
repository.updateById(good);
}
@Override
public void delete(Integer id) {
//repository.deleteById(id);
repository.deleteById(id);
}
@Override
public Good login(Integer id, Integer parentid) {
QueryWrapper<Good> query = new QueryWrapper<>();
query.eq("id",id);
query.eq("parentid",parentid);
repository.selectOne(query);
//return repository.findByIdAndParentid(id,parentid);
return null;
}
@Override
public Good login2(Integer id, Integer parentid) {
/*Example example = new Example(Good.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id",id);
criteria.andEqualTo("parentid",parentid);
return repository.selectOneByExample(example);*/
return null;
}
@Override
public List<Good> findByTypenameLike(String typename) {
/*Example example = new Example(Good.class);
Example.Criteria criteria = example.createCriteria();
criteria.andLike("typename",typename);
return repository.selectByExample(example);*/
QueryWrapper<Good> wrapper = new QueryWrapper<>();
wrapper.like("typename",typename);
return repository.selectList(wrapper);
}
@Override
public List<Good> findAll() {
//return (List<Good>) repository.findAll();
//return repository.selectAll();
return repository.selectList(null);
}
}
controller
package com.ishangu.controller;
import com.ishangu.model.Good;
import com.ishangu.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("good/")
public class GoodController {
@Autowired
GoodService service;
//@RequestMapping("save")
@PostMapping("save")
public String save(Good good){
service.save(good);
return "ok";
}
//@RequestMapping("update")
@PutMapping("update")
public String update(Good good){
service.update(good);
return "ok";
}
//@RequestMapping("delete")
@DeleteMapping("delete")
public String delete(Integer id){
service.delete(id);
return "ok";
}
//@RequestMapping("login")
@GetMapping(value = "{id}/{parentid}")
public Good login(@PathVariable("id") Integer id,@PathVariable("parentid") Integer parentid){
return service.login(id,parentid);
}
/*@RequestMapping("login2")
public Good login2(Integer id,Integer parentid){
return service.login2(id,parentid);
}*/
//@RequestMapping("like")
@GetMapping("{typename}")
public List<Good> findByTypenameLike(@PathVariable("typename") String typename){
System.out.println("like");
return service.findByTypenameLike(typename);
}
//@RequestMapping("findAll")
@GetMapping
public List<Good> findAll(){
System.out.println("all");
return service.findAll();
}
}
使用到了新软件Postman