2022-10-1812:21:43.430 INFO 19204---[ main]com.wd.SpringBootApplication:StartingSpringBootApplication on DESKTOP-6RBSD56 withPID19204(E:\MySelfProject\SpringBoot\target\classes started by 32929 in E:\MySelfProject\SpringBoot)2022-10-1812:21:43.434 INFO 19204---[ main]com.wd.SpringBootApplication:No active profile set, falling back todefault profiles:default2022-10-1812:21:44.154 INFO 19204---[ main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat initialized withport(s):8989(http)2022-10-1812:21:44.162 INFO 19204---[ main]o.apache.catalina.core.StandardService:Starting service [Tomcat]2022-10-1812:21:44.162 INFO 19204---[ main]org.apache.catalina.core.StandardEngine:StartingServlet engine:[ApacheTomcat/9.0.29]2022-10-1812:21:44.226 INFO 19204---[ main]o.a.c.c.C.[.[.[/springboot_test]:InitializingSpring embedded WebApplicationContext2022-10-1812:21:44.226 INFO 19204---[ main]o.s.web.context.ContextLoader:RootWebApplicationContext: initialization completed in 742 ms
2022-10-1812:21:44.376 INFO 19204---[ main]o.s.s.concurrent.ThreadPoolTaskExecutor:InitializingExecutorService 'applicationTaskExecutor'
2022-10-1812:21:44.522 INFO 19204---[ main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat started on port(s):8989(http)withcontext path '/springboot_test'
2022-10-1812:21:44.524 INFO 19204---[ main]com.wd.SpringBootApplication:StartedSpringBootApplication in 1.553 seconds (JVM running for2.533)2022-10-1812:23:13.427 INFO 19204---[nio-8989-exec-1]o.a.c.c.C.[.[.[/springboot_test]:InitializingSpringDispatcherServlet 'dispatcherServlet'
2022-10-1812:23:13.427 INFO 19204---[nio-8989-exec-1]o.s.web.servlet.DispatcherServlet:InitializingServlet 'dispatcherServlet'
2022-10-1812:23:13.434 INFO 19204---[nio-8989-exec-1]o.s.web.servlet.DispatcherServlet:Completed initialization in 7 ms
use mybatis_plus;createtable t_user(
id bigint(20)notnullprimarykey,
name varchar(30),
sex varchr(20)
age int(11),
height double(20),
weihgt double(20));
<?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"><mapper namespace="com.wd.mapper.UserMapper"><!--UserselectByUserId(@Param("id")Integer id);--><select id="selectByUserId" resultType="User">
select * from t_user where id = #{id}</select></mapper>
importcom.wd.SpringBootApplication;importcom.wd.domains.User;importcom.wd.mapper.UserMapper;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjavax.annotation.Resource;importjava.util.*;@RunWith(SpringRunner.class)//入口类对象作为该注解的参数@SpringBootTest(classes =SpringBootApplication.class)publicclassTestSpringBoot{//@Autowired Spring提供 根据类型查找容器中的对象注入@Resource//JDK提供 根据名称查找容器中的对象注入privateUserMapper userMapper;@TestpublicvoidtestSelect(){//SELECT id,name,sex,age,height,weihgt FROM t_userList<User> users = userMapper.selectList(null);//SELECT id,name,sex,age,height,weihgt FROM t_user WHERE id=?User user = userMapper.selectById(10);//SELECT id,name,sex,age,height,weihgt FROM t_user WHERE id IN ( ? , ? , ? )List<User> users1 = userMapper.selectBatchIds(Arrays.asList(7,8,9));Map<String,Object> nameMap =newHashMap<>();
nameMap.put("name","李四");
nameMap.put("sex","男");//SELECT id,name,sex,age,height,weihgt FROM t_user WHERE sex = ? AND name = ?List<User> users2 = userMapper.selectByMap(nameMap);
users.forEach(System.out::println);System.out.println("------------------");
users1.forEach(System.out::println);System.out.println("------------------");
users2.forEach(System.out::println);System.out.println("------------------");System.out.println(user);User user1 = userMapper.selectByUserId(2);System.out.println(user1);}@TestpublicvoidtestInsert(){//mybatis-plus默认通过雪花算法生成id,所以一般id为Long类型User user =newUser(null,"七十二","男",18,185.5,115.3);//INSERT INTO t_user ( id, name, sex, age, height, weihgt ) VALUES ( ?, ?, ?, ?, ?, ? )int result = userMapper.insert(user);System.out.println("result:"+ result);System.out.println("id:"+ user.getId());}@TestpublicvoidtestDelete(){User user =newUser(1,"七十一","男",18,58.2,15.3);//DELETE FROM t_user WHERE id=?int result = userMapper.deleteById(2066276353);//DELETE FROM t_user WHERE id=?int result1 = userMapper.deleteById(user);//DELETE FROM t_user WHERE id IN ( ? , ? )int result2 = userMapper.deleteBatchIds(Arrays.asList(10,11));Map<String,Object> nameMap =newHashMap<>();
nameMap.put("name","李四");
nameMap.put("sex","男");//DELETE FROM t_user WHERE sex = ? AND name = ?int result3 = userMapper.deleteByMap(nameMap);System.out.println("result:"+ result);System.out.println("result1:"+ result1);System.out.println("result2:"+ result2);System.out.println("result:"+ result3);}@TestpublicvoidtestUpdate(){User user =newUser(8,"七十一","男",18,58.2,15.3);//UPDATE t_user SET name=?, sex=?, age=?, height=?, weihgt=? WHERE id=?int result = userMapper.updateById(user);System.out.println("result:"+ result);System.out.println("id:"+ user.getId());}}
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.like("name","七").eq("sex","男").isNotNull("age");//SELECT id,name,sex,age,height,weihgt AS weight FROM t_user WHERE (name LIKE ? AND sex = ? AND age IS NOT NULL)List<User> users = userMapper.selectList(userQueryWrapper);System.out.println(users);}
2.组装排序查询
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.orderByDesc("age").orderByAsc("height");//SELECT id,name,sex,age,height,weihgt AS weight FROM t_user ORDER BY age DESC,height ASCList<User> users = userMapper.selectList(userQueryWrapper);System.out.println(users);/*
[
User(id=-85364734, name=手动阀, sex=男, age=25, height=null, weight=null),
User(id=8, name=七十一, sex=男, age=18, height=58.2, weight=15.3),
User(id=12, name=萨达, sex=女, age=18, height=165.0, weight=80.0),
User(id=-2111225855, name=七十二, sex=男, age=18, height=185.5, weight=115.3),
User(id=-72781822, name=萨达时, sex=女, age=null, height=null, weight=null)
]
*/}
3.组装删除查询
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.isNull("age");//DELETE FROM t_user WHERE (age IS NULL)int result = userMapper.delete(userQueryWrapper);System.out.println("result:"+ result);}
4.组装修改查询
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.isNull("age");User user =newUser();
user.setName("赵六");
user.setAge(18);
user.setHeight(185.0);
user.setSex("男");
user.setWeight(65.5);//UPDATE t_user SET name=?, sex=?, age=?, height=?, weihgt=? WHERE (age IS NULL)int result = userMapper.update(user,userQueryWrapper);System.out.println("result:"+ result);}
5.条件优先查询
@TestpublicvoidtestWrapperMethods(){//and()和or()可以提高优先级QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.like("name","赵").between("age",10,40);//SELECT id,name,sex,age,height,weihgt AS weight FROM t_user WHERE (name LIKE ? AND age BETWEEN ? AND ?)//userQueryWrapper.like("name","赵")//.and(wrapper -> wrapper.between("age",10,40));//SELECT id,name,sex,age,height,weihgt AS weight FROM t_user WHERE (name LIKE ? AND (age BETWEEN ? AND ?))List<User> users = userMapper.selectList(userQueryWrapper);
users.forEach(System.out::println);}
6.查询指定字段
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.select("name","sex","age").between("age",10,40);//SELECT name,sex,age FROM t_user WHERE (age BETWEEN ? AND ?)List<Map<String,Object>> maps = userMapper.selectMaps(userQueryWrapper);
maps.forEach(System.out::println);}
7.组装子查询
@TestpublicvoidtestWrapperMethods(){QueryWrapper<User> userQueryWrapper =newQueryWrapper<>();
userQueryWrapper.inSql("id","select id from t_user where age <= 100");//SELECT id,name,sex,age,height,weihgt AS weight FROM t_user WHERE (id IN (select id from t_user where age <= 100))List<User> users = userMapper.selectList(userQueryWrapper);
users.forEach(System.out::println);}
3.UpdateWrapper
1.组装条件修改
@TestpublicvoidtestWrapperMethods(){UpdateWrapper<User> userUpdateWrapper =newUpdateWrapper<User>();
userUpdateWrapper.like("name","赵").and(wrapper -> wrapper.between("age",10,20));
userUpdateWrapper.set("name","王五");//UPDATE t_user SET name=? WHERE (name LIKE ? AND (age BETWEEN ? AND ?))int result = userMapper.update(null, userUpdateWrapper);System.out.println("result:"+ result);}
@TestpublicvoidtestWrapperMethods(){LambdaUpdateWrapper<User> userUpdateWrapper =newLambdaUpdateWrapper<>();
userUpdateWrapper.like(User::getName,"王").and(wrapper -> wrapper.between(User::getAge,10,20));
userUpdateWrapper.set(User::getName,"Nissan");//UPDATE t_user SET name=? WHERE (name LIKE ? AND (age BETWEEN ? AND ?))int result = userMapper.update(null, userUpdateWrapper);System.out.println("result:"+ result);}
//数据表和实体类中都添加version字段//取出记录时,获取当前versionSELECT id,name,price,version FROM t_product WHERE id =1//更新时,version+1,如果where语句中的version版本不对,则更新失败
UPDATE product SET proce=price+50,version=version+1WHERE id =1AND version =1
1.建表
createtable t_product(
id bigint(20)notnull,
name varchar(30)nulldefaultNULL,
price int(11)default0,
version int(11)default0,primarykey(id));