技术标签: Java项目 spring java mybatis
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。–来自官网解释
无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作
一个开发了30年的程序员,业务相当熟练,但是为了完成业务也需要写特别简单的SQL语句.
需求:能否有效的提高开发的效率.
对象关系映射(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,**用于实现面向对象编程语言里不同类型系统的数据之间的转换。**从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。 —— [ 百度百科 ]
知识铺垫:
sql语句是面向过程的语言 sql:select * from user ResultSet结果集对象
ORM方式:以对象的方法操作数据库。可以实现结果集与对象的自动映射,不需要手动写
1.对象与那张表要完成映射 可以自定义注解进行标识
2.对象的属性与表中的字段如何一一对应 起名时应该写成一样的 不一样利用特定的注解指定。
3.工具方法如何简化 MP动态生成CRUD操作的接口,只要其他的Mapper继承接口即可
4.对象如何转化为sql语句 利用对象中的属性及属性的值动态拼接sql,之后交给Mybatis(jdbc)即可
eg.userMapper.insert(user对象)
eg:deptMapper.insert(dept对象)
sql: insert into 表名(字段信息…) values(属性值…)
说明:MybatisPlus包已经包含了Mybatis的信息,所以需要把Mybatis依赖删掉!!!
<!--spring整合mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
username: root
password: tarena
mybatis-plus:
#别名包的定义 定义了后xml文件中的resultType就只用写类名 之后自动拼接
type-aliases-package: com.jt.pojo
#加载指定的xml映射文件
mapper-locations: classpath:/mybatis/mappers/*.xml
使用MP的机制进行查询
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
}
logging:
level:
com.jt.mapper: debug
package com.jt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
class SpringbootDemo2ApplicationTests {
@Autowired
private UserMapper userMapper;
/**
* 根据主键查询
*/
@Test
public void select01(){
User user = userMapper.selectById(21);
System.out.println(user);
int count = userMapper.selectCount(null);
System.out.println("总记录数:"+count);
}
/**
* 需求: 查询年龄=18用户 同时要求性别为女
* 条件构造器的作用 用来拼接where条件
* sql: xxxx where age>18 and age <2000 and sex="女"
* 逻辑运算符: = eq, > gt , < lt , >= ge , <= le
* */
@Test
public void select02(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age", 18)
.lt("age", 2000)
.or()
.eq("sex", "女");
List<User> userList = userMapper.selectList(queryWrapper);
System.out.println(userList);
}
/**
* 查询ID= 1 ,3 ,5 ,6的数据
* 单表查询: or in
* sql: select * from user where id in (1,3,5,6)
*/
@Test
public void select03(){
Integer[] ids = {1,3,5,7}; //模拟用户参数
List<Integer> idList = Arrays.asList(ids);
List<User> userList = userMapper.selectBatchIds(idList);
System.out.println(userList);
//如果需要获取表中的第一列主键信息
List list = userMapper.selectObjs(null);
System.out.println(list);
}
/**
* 完成用户数据入库
*/
@Test
public void testInsert(){
User user = new User();
user.setName("名媛")
.setAge(80).setSex("女");
userMapper.insert(user);
}
/**
* 更新操作
* 个人建议: 但凡写更新操作时,最好自己手写
* 需求: 需要将id=65的用户name="名媛" 改为 "北京大爷"
* 用户name="名媛" 改为 "北京大爷"
*/
@Test
public void testUpdate(){
//根据对象中不为null的属性 当做set条件
/*User user = new User();
user.setId(65).setName("北京大爷");
userMapper.updateById(user);*/
//1.参数1: 需要修改的数据 参数2:修改的where条件
User user = new User();
user.setName("北京大爷");
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "名媛");
userMapper.update(user,updateWrapper);
}
/**
* 删除操作:
* 1.根据ID删除用户信息 65
* 2.删除name="名媛"的数据
*/
@Test
public void testDelete(){
userMapper.deleteById(65); //根据主键删除
//userMapper.deleteBatchIds("id集合信息");
//根据除主键之外的数据删除信息
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("name", "名媛");
userMapper.delete(queryWrapper);
}
}
2.1.2 编辑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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jt</groupId>
<artifactId>springboot_demo3</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定项目打包方式-->
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!--spring整合mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--springBoot整合JSP添加依赖 -->
<!--servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--使jsp页面生效 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>您好Springboot</title>
</head>
<body>
<table border="1px" width="65%" align="center">
<tr>
<td colspan="6" align="center"><h3>学生信息</h3></td>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th></th>
</tr>
<c:forEach items="${userList}" var="u">
<tr>
<th>${u.id}</th>
<th>${u.name}</th>
<th>${u.age}</th>
<th>${u.sex}</th>
</tr>
</c:forEach>
</table>
</body>
</html>
@TableName
@Data
@Accessors(chain = true)
public class User {
@TableId(type = IdType.AUTO)
private Integer id; //主键自增
private String name;
private Integer age;
private String sex;
}
2.1.5 编辑YML配置文件
server:
port: 8090
servlet:
context-path: /
spring:
datasource:
#驱动版本问题 高版本需要添加cj关键字 一般可以省略
#driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: tarena
mvc: #引入mvn配置
view:
prefix: /WEB-INF/ # /默认代表根目录 src/main/webapp
suffix: .jsp
mybatis-plus:
#别名包定义 Mapper的resultType中只需要写类名 之后自动拼接即可
type-aliases-package: com.jt.pojo
#加载指定的xml映射文件
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
# 实现sql打印
logging:
level:
com.jt.mapper: debug
服务器能跑通,但是页面加载显示404,查看工作目录的配置匹配。
package com.jt.controller;
import com.jt.pojo.User;
import com.jt.service.UserService;
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 java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
/**
* 跳转页面: userList.jsp
* url:localhost:8090/findAll
* 步骤:
* 1.pojo 2.mapper 3.service 4.controller
* 5.YML配置前后缀
* 6.jar包问题 3个包
* 7.导入页面 userList.jsp
* */
@RequestMapping("/findAll")
public String findAll(Model model){
//1.查询用户列表信息
List<User> userList = userService.findAll();
//2.将数据保存到request域中 之后返回给用户 视图渲染过程
model.addAttribute("userList",userList);
return "userList";
}
}
package com.jt.service;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectList(null);
}
}
特点:局部刷新 异步调用(加载)
调用原理说明:
1.用户将请求发给AJAX引擎进行处理.之后等待引擎返回数据.
2.ajax引擎接收到用户的请求之后.,代替用户访问后端服务器,
3.后端服务器接收请求之后,执行业务处理. 并且将返回值返回.
4.ajax引擎收到返回值结果之后,要在第一时间通知给用户. 利用回调函数将数据传给客户端.ajax调用成功.
说明:将原来的userList.jsp页面 复制一份,改名为ajax.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>您好Springboot</title>
<!-- 1.导入函数类库 -->
<script src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
//让JS页面加载完成,之后执行JS
$(function(){
/*
需求: 利用ajax方式动态获取user数据信息
请求网址: /findAjax
知识点:
返回值类型: 可以根据数据自动匹配类型,所以可以不写.
1. $.get(url地址,传递的参数,回调函数,返回值类型)
2. $.post(.....)
3. $.getJSON(.....)
*/
$.get("/findAjax",function(result){
//1.可以使用js中的for循环
/* for(let i=0; i<result.length;i++){
} */
/* for(let index in result){
console.log(index);
} */
for(let user of result){
let id = user.id;
let name = user.name;
let age = user.age;
let sex = user.sex;
let tr = "<tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td></tr>"
$("#tab1").append(tr);
}
})
})
</script>
</head>
<body>
<table id="tab1" border="1px" width="65%" align="center">
<tr>
<td colspan="6" align="center"><h3>学生信息</h3></td>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</table>
</body>
</html>
/**
* 实现页面的跳转
* url地址: localhost:8090/ajax
* 跳转的页面: ajax.jsp
*/
@RequestMapping("/ajax")
public String ajax(){
return "ajax";
}
/**
* 实现ajax业务调用
* url: http://localhost:8090/findAjax?id=40
* 参数: id
* 返回值:List<User>
*/
@RequestMapping("/findAjax")
@ResponseBody //ajax结束标识
public List<User> findAjax(){
return userService.findAll();
}
对于Linux初学者,查看帮助文档对于提升自己有很大的帮助。特别对于usage来说,只有看懂了用法,才能够学会它。1.在usage中,option有如下几个符号及其表示含义:[] 可选内容 <> 必选内容 {} 分组 | 二选一 … 同一个内容可多次出现 不在方括号和大括号中的内容是必选项2.options中的命令选项有两种形式:长选项 - -:后面跟完整的单词短选项 -:后面跟单个的字符,可多个组合使用,但长选项则不行黑.
图书简介:本书是计算机科学方面的经典名著。书的内容围绕程序设计人员面对的一系列实际问题展开。作者Jon Bentley 以其独有的洞察力和创造力,引导读者理解这些问题并学会解决方法,而这些正是程序员实际编程生涯中至关重要的。本书的特色是通过一些精心设计的有趣而又颇具指导意义的程序,对实用程序...
Taylor Formula泰勒公式,它的一般形式如下:一般希望将复杂形式的函数用较为简单的方式来表示,另一种表述便是,用量上的复杂来解决质上的困难。那为什么泰勒展开式是这种形式的?上面说要用简单的形式表示复杂函数,那么如何选择表达式呢?类比切圆法,是不是可以用局部的线性近似来表示整体,假如有函数y=x3y=x^3y=x3,自变量的变化量为ΔxΔxΔx,则:Δy=(x+Δx)3−x3=...
11. (译)Python魔法方法指南原文:http://www.rafekettler.com/magicmethods.html原作者:Rafe Kettler翻译:hit9原版(英文版) Repo:https://github.com/RafeKettler/magicmethodsContents(译)Pyth...
mail IN CNAME www //mail.hello.com是www.hello.com的另一个名称。vi /etc/named.rfc1912.zones //所配置文件中其它全删除。vi /etc/named.rfc1912.zones //区域配置文件。vi /etc/named.rfc1912.zones //主服务器操作。-----------以下编辑区域配置文件----------------------以下是反向区----------------
" size="small" type="success" @click="submitUpload">上传到服务器选取文件responseType: "blob" // 表明返回服务器返回的数据类型。
记得上几篇博客有提到用tensorflow 中保存模型,然后用tensorflow serving中启动服务再用java调用,实际这样绕了很多,今天发现在java中也能直接加载调用TensorFlow serving中调用的格式,实际在java中也可以直接调用pb文件的模型,前面也提到,这也算是另外一种方式吧直接看代码:Python 生成模型代码,可以用在TensorFlow serving ...
基于Arduino UNO的楼道人体感应灯文章目录前言一、认识人体热释电红外传感器二、模块连接图及程序1.setup()初始化程序2.loop()主函数总结前言生活中经常看到一些自动化设备,例如感应水龙头、自动开关门等。这些设备都通过传感器来检测来环境进而实现的,当环境要求满足是时候,便启动执行程序开始工作。提示:以下是本篇文章正文内容,下面案例可供参考一、认识人体热释电红外传感器人体热释电红外传感器是一种对人体辐射出的红外线敏感的传感器。当无人在其检测范围内运动时,模块保持输出低电平;当
1. 怎样使用MFC发送一个消息用MFC发送一个消息的方法是,首先,应获取接收消息的CWnd类对象的指针;然后,调用CWnd的成员函数SendMessage( )。LRESULT Res=pWnd->SendMessage(UINT Msg, WPARAM wParam, LPARAM lParam);pWnd指针指向目标CWnd类对象。变量Msg是消息,wPara...
点击安装vm-tool,挂载/dev/cdrom然后一直回车即可。
问题描述在适配某平台的时候遇到 kni_open 函数调用的时候内核 oops,oops 的主要信息见下图:前期得到的输入信息是内核的 config 文件换过,换了之后重新编译测试出现了这个问题,可能是 config 文件影响,排查 config 文件的区别,没有发现怀疑点,只能怼 oops 了!从 oops 中获取到的关键信息上述 oops 内容将问题指向如下位置:kni_open 函数 0x16 偏移量处objdump -d rte_kni.ko 获取到如下信息:00000000000
属性描述器的使用1.新