1:目录结构
2:依赖
<?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> <groupId>com.springplus</groupId> <artifactId>springboot-mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mybatisplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> <!-- <dependency>--> <!-- <groupId>com.baomidou</groupId>--> <!-- <artifactId>mybatis-plus-boot-starter</artifactId>--> <!-- <version>3.5.1</version>--> <!-- </dependency>--> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--引入MybatisPlus核心配置文件--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--代码生成器--> <!--当前包未传递依赖 MP 包,需要自己引入!--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <!-- 模板生成器引擎配合代码生成器一起使用 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-spring-boot-starter</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.1</version> </dependency> <!-- 添加jedis依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- <dependency>--> <!-- <groupId>org.apache.poi</groupId>--> <!-- <artifactId>poi</artifactId>--> <!-- <version>4.1.2</version>--> <!-- </dependency>--> <!-- <!–xlsx格式excel依赖包–>--> <!-- <dependency>--> <!-- <groupId>org.apache.poi</groupId>--> <!-- <artifactId>poi-ooxml</artifactId>--> <!-- <version>3.17</version>--> <!-- </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--引入MybatisPlus核心配置文件--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--代码生成器--> <!--当前包未传递依赖 MP 包,需要自己引入!--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <!-- 模板生成器引擎配合代码生成器一起使用 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.springplus.springbootmybatisplus.SpringbootMybatisplusApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
3:工具(util)
package com.springplus.springbootmybatisplus.util; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; @Configuration //连接redis 获取application.yml里的数据以spring.redis开头的方式 @ConfigurationProperties(prefix = "spring.redis") public class JedisUtil { //属性名字和配置文件中必须一致,还要提供get和set方法 private String host; private int port; @Bean public JedisPool jedisPool(){ JedisPool jedisPool=new JedisPool(host,port); System.out.println("已连接:"+host+"上的redis,端口号为:"+port); return jedisPool; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } }
package com.springplus.springbootmybatisplus.util; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; public class JsonUtil { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */ public static String objectToJson(Object data) { try { String string = MAPPER.writeValueAsString(data); return string; } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsonData json数据 * @param class 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } }
3:Controller层
package com.springplus.springbootmybatisplus.Controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.springplus.springbootmybatisplus.Mapper.PlusMapper; import com.springplus.springbootmybatisplus.Pojo.Goods; import com.springplus.springbootmybatisplus.server.PlusService; import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.FileNotFoundException; import java.util.LinkedList; import java.util.List; @Controller public class GoodController { @Autowired private PlusService plusService; @Autowired private PlusMapper plusMapper; @RequestMapping("redis1") @ResponseBody public Object demo1(){ Object o = plusService.selectGood(); return o; } @RequestMapping("redis3") public Object demo13(Model model,HttpServletRequest request) throws FileNotFoundException { String id = request.getParameter("id"); Integer id2=Integer.parseInt(id); QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.eq("id", id2); List list1 = plusMapper.selectList(queryWrapper); // Goods goods = (Goods) list1; // List list=new LinkedList(); list.add(goods); model.addAttribute("update",list1); return "add"; } @RequestMapping("redis5") public Object demo5(HttpServletRequest request) throws FileNotFoundException { String name = request.getParameter("name"); String price = request.getParameter("price"); String id = request.getParameter("id"); Integer id2=Integer.parseInt(id); Integer price2=Integer.parseInt(price); String typed = request.getParameter("typed"); Integer typed2=Integer.parseInt(typed); Goods goods=new Goods(); goods.setName(name); goods.setPrice(price2); goods.setTyped(typed2); Object o = plusService.updateGoods(goods, id2); return o; } @RequestMapping("redis4") public Object demo4() throws FileNotFoundException { Integer integer = plusService.insertGoods(); return integer; } @RequestMapping("redis2") public Object demo2(Model model){ List<Goods> goods1 = plusMapper.selectList(null); model.addAttribute("list",goods1); //Goods goods = /goods1; return "allList"; } }
5:Mapper接口
package com.springplus.springbootmybatisplus.Mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.springplus.springbootmybatisplus.Pojo.Goods; import org.apache.ibatis.annotations.Select; import java.util.List; public interface PlusMapper extends BaseMapper<Goods> { List<Goods> selectGood (); @Select("select * from goods ") List selectListTypedea (); }
6:bean
package com.springplus.springbootmybatisplus.Pojo; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.Serializable; @Data @Component @ExcelIgnoreUnannotated public class Goods { public Goods () { } public Goods (Integer id, String name, Integer price, Integer typed) { this.id = id; this.name = name; this.price = price; this.typed = typed; } //不导出列 //ExcelIgnoreUnannotated private Integer id; @ExcelProperty("姓名") private String name; @ExcelProperty("状态") private Integer price; @ExcelProperty("分类id") private Integer typed; //和数据有关,不需要关联 //@ExcelProperty("") //private String tyname; // @Autowired // private Typedea typedea; }
6:plus方法解读
package com.springplus.springbootmybatisplus.Controller; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.springplus.springbootmybatisplus.Mapper.*; import com.springplus.springbootmybatisplus.Pojo.Combination; import com.springplus.springbootmybatisplus.Pojo.Goods; import com.springplus.springbootmybatisplus.Pojo.StudentEntity; import com.springplus.springbootmybatisplus.server.ConformityService; import com.springplus.springbootmybatisplus.server.GoodsService; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private ConformityService conformityService; @Autowired private Conformity conformity; @Autowired private GoodsService goodsService; @Autowired private GoodsMapper goodsMapper; @Autowired private GoodsMapper goods; @Autowired private sdasdasd sdasdasd; @RequestMapping("demo66") public void demo66(){ List<StudentEntity> listgoods = goodsService.selectStudentEntity(); Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生","学生"), StudentEntity.class,listgoods); } @RequestMapping("demo111") public List<Goods> demo111(){ //Goods goods =conformity.selectList(null); List<Goods> goods = goodsMapper.selectGoods(); return goods; } @RequestMapping("demo1") public List<Goods> demo1(){ //Goods goods =conformity.selectList(null); List<Goods> goods = sdasdasd.selectList(null); return goods; } @RequestMapping("demo2") public Goods demo2(){ //Goods goods =conformity.selectList(null); Goods goods1 = sdasdasd.selectById("2"); // List<Goods> goods = goods1; return goods1; } @RequestMapping("demo3") public String demo3(){ //Goods goods =conformity.selectList(null); // Goods goods1 = sdasdasd.selectById("2"); QueryWrapper wrapper=new QueryWrapper(); //eq等值查询 lt小于 le小于等于 gt大于 ge大于等于 wrapper.eq("id", 3); List list = sdasdasd.selectList(wrapper); // List<Goods> goods = goods1; return "add"; } @RequestMapping("demo4") public List demo4(){ //Goods goods =conformity.selectList(null); // Goods goods1 = sdasdasd.selectById("2"); QueryWrapper wrapper=new QueryWrapper(); //like %?% likeLeft%? likeRight?% wrapper.like("name", "1"); //eq等值查询 lt小于 le小于等于 gt大于 ge大于等于 // wrapper.eq("id", 3); List list = sdasdasd.selectList(wrapper); // List<Goods> goods = goods1; return list; } @RequestMapping("demo5") public Integer demo5(){ Goods goods=new Goods(); goods.setId(11); goods.setName("张三"); goods.setPrice(0); goods.setTyped(2); int insert = sdasdasd.insert(goods); return insert; } @RequestMapping("demo6") public int demo6(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.eq("price", "0"); Goods goods=new Goods(); goods.setId(1); int update = sdasdasd.update(goods, queryWrapper); int i = sdasdasd.updateById(goods); return update; } @RequestMapping("demo7") public int demo7(){ QueryWrapper queryWrapper=new QueryWrapper(); Goods goods = sdasdasd.selectById(1); goods.setName("王五"); int i = sdasdasd.updateById(goods); return i; } //删除 @RequestMapping("demo8") public int demo8(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.ge("price", 10); // int delete = sdasdasd.deleteById("5"); int delete = sdasdasd.delete(queryWrapper); return delete; } @RequestMapping("demo9") public IPage demo9(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.like("name", "1"); IPage<Goods> page=new Page(1,1); IPage page1=sdasdasd.selectPage(page, queryWrapper); return page1; } @RequestMapping("demo11") public Goods selectStudentAndMajorXmlPage(){ // List<Combination> studentAndMajorVOS = conformityService.selectStudentAndMajorXmlPage(1, 4); Goods goods = goodsService.selectByPrimaryKey(2); return goods; } @RequestMapping("demo12") public Goods demo12(){ // List<Combination> studentAndMajorVOS = conformityService.selectStudentAndMajorXmlPage(1, 4); Goods byselect1 = sdasdasd.getByselect(); // Integer byselect = (Integer) byselect1; return byselect1; } }