3、MybatisPlus
1.Spring 補充
1.1 Spring內部數據結構
1.數據的結構: Map 理解為容器
2.數據內容: KEY-VALUE -KEY:一般都是類名的首字母小寫 helloController -Value: 一般存儲的是經過反射等機制實例化的對象 Map<helloController,controller對象>
3.單例: 容器中只保存一個對象, spring容器管理對象的生命周期 和容器幾乎同生共死
默認:單例對象
多例: 如果用戶需要使用,則新創建一個對象交給用戶使用,由用戶管理對象的生命周期.
2.關于環境代碼報錯
2.1 數據庫問題
Mysql數據庫版本: 教師機 5.5 高版本的數據庫6.0以上.
2.2 IDEA版本問題
修改mybatis的路徑
3. MybatisPlus
3.1 MP介紹
MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。
3.2 特點說明
重點說明: MP適用于單表操作 如果遇到多表關聯,則手寫Sql效率更高.
3.3 MP入門案例
3.3.1 導入jar包
說明: 由于MP內部兼容了Mybatis 則引入MP之后將原來的mybatis的包刪除.
<!--spring整合mybatis-plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency>3.3.2 對象與表映射
3.3.3 繼承公共的Mapper接口
3.3.4 修改YML配置文件
說明: springBoot應該由原來的Mybatis操作數據庫,現在換成MP的機制,所以應該修改SpringBoot整合MP的操作.
3.3.4 編輯MP入門案例
對比Sql寫法/MP寫法的區別…
@SpringBootTest public class TestMybatis {// sprign容器 <userMapper,代理對象>// 面向接口編程 擴展性好@Autowiredprivate UserMapper userMapper;//JDK動態代理//報錯說明:com.jt.mapper.UserMapper.findAll 不匹配!!!!//關于測試類代碼說明: 要求: public 返回值void 方法名稱不能叫test@Testpublic void test01(){System.out.println(userMapper.getClass());List<User> userList = userMapper.findAll(); //接口的方法 數據庫只能識別Sql語句System.out.println(userList);}//查詢 用戶的全部記錄 面向對象的方式操作數據庫.//只能用于單表查詢@Testpublic void testFind(){//暫時不需要任何where條件 查詢的是全部記錄.List<User> userList = userMapper.selectList(null);System.out.println(userList);} }3.4 Mybatis CURD操作練習
3.4.1 編輯測試代碼
@Testpublic void insert(){User user = new User();user.setName("星期五").setAge(18).setSex("男");userMapper.insertUser(user);System.out.println("新增用戶成功");}//根據name="星期五",將name="星期六"@Testpublic void update(){String oldName = "星期五";String newName = "星期六";userMapper.updateByName(oldName,newName);System.out.println("更新用戶成功");}//刪除用戶信息 根據name屬性刪除數據@Testpublic void delete(){String name = "星期六";userMapper.deleteByName(name);System.out.println("刪除用戶成功");}3.4.2 編輯Mapper接口
@Insert("insert into demo_user(id,name,age,sex) " +"value (null,#{name},#{age},#{sex})")void insertUser(User user);@Update("update demo_user set name = #{newName} where name=#{oldName}")void updateByName(String oldName, String newName);//Mybatis中如果傳遞的參數只有一個,則名稱任意 一般不用.@Delete("delete from demo_user where name=#{name}")void deleteByName(String name);3.5 MP的工作原理
1).通過注解 實現對象與表一一映射.
2).通過屬性注解 實現對象的屬性與表中的字段一一映射.
3).將公共的方法進行抽取,抽取到BaseMapper接口中
4).將用戶操作的方法對象,轉化為數據庫能夠識別的Sql語句.
demo1: userMapper.insert(user對象)
Sql1: insert into 表名(字段名…) value (屬性值…)
拼接過程:
insert into 表名(字段名…) value (屬性值…)
1).通過userMapper 查找父級接口BaseMapper
2).根據BaseMapper 查找泛型對象 User對象.
3).根據user對象 查找指定的注解 @TableName,獲取表名
4).根據user對象的屬性,動態獲取表中的字段.@TableField
5).在獲取字段的同時,獲取屬性的值,最后進行sql拼接
6).MP將拼接好的Sql交給Mybatis框架處理執行.
3.6 MP練習
3.6.1 根據ID查詢
/*** 1.根據Id=23數據*/@Testpublic void select01(){User user = userMapper.selectById(23);System.out.println(user);}3.6.2 對象查詢
配置日志:
/*** 查詢 name="潘鳳" sex="男"* 結果: 1項 userMapper.selectOne()* 多項 userMapper.selectList()* Sql: where name="xxx" and sex="xxx"* queryWrapper: 條件構造器 拼接where條件* 如果遇到多條件查詢,則默認的連接符and* 方式1: 可以通過對象的方式進行控制*/@Testpublic void select02(){User user = new User();user.setName("潘鳳").setSex("男");QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.3 特殊字符練習
/*** 要求: age>18歲 or sex=男的用戶* 轉義字符: > gt, < lt , = eq* >= ge , <= le*/@Testpublic void select03(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();//編輯的是數據庫字段信息queryWrapper.gt("age", 18).or().eq("sex","男");//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.4 like關鍵字
/*** 要求: 查詢name中包含"精" 并且按照age 降序排列* Sql: like "%精%" 包含精* like "精%" 以精開頭* like "%精" 以精結尾*/@Testpublic void select04(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.likeRight("name","精").orderByDesc("age");//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.5 In關鍵字
/*** 查詢多個數據* 查詢ID= 1 3 6 7的數據* where id in (xx,xx,xx,xx)* 如果遇到多值傳參,一般采用對象的方式封裝數據*/@Testpublic void select05(){Integer[] ids = {1,3,6,7};QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.in("id", ids);//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.6 不為null查詢
/*** 需求: 查詢name為null的數據*/@Testpublic void select06(){QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.isNull("name");//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.7 動態Sql語句查詢
/*** 動態sql查詢:* 要求: 根據 age 屬性與sex屬性進行查詢.* 如果其中數據為null 則不參與where條件的拼接* where age>18 and sex="男"* 錯誤Sql:* SELECT id,name,age,sex FROM demo_user WHERE (age > ? AND sex = ?)* 18(Integer), null* MP實現動態查詢:* 參數1: condition boolean類型數據 true 拼接條件* false 不拼接條件* 參數2: 字段名稱* 參數3: 字段值*/@Testpublic void select07(){Integer age = 18;String sex = "男";//boolean flag = sex != null && sex.length()>0;//條件判斷boolean flag = StringUtils.hasLength(sex);QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.gt(age>0, "age", age).eq(flag,"sex",sex);//根據對象中不為null的屬性 拼接where條件List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}3.6.8 挑選字段查詢
/*** demo1:只查詢 name,age字段信息* 挑選查詢的字段信息* queryWrapper.select("name","age");*/@Testpublic void select08(){QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.select("name","age");//沒有查詢的數據以null返回List<User> userList = userMapper.selectList(queryWrapper);System.out.println(userList);}//* demo2: 只要求返回name,age字段@Testpublic void select09(){QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.select("name","age");List<Map<String,Object>> list = userMapper.selectMaps(queryWrapper);System.out.println(list);}//* 要求返回第一列的數據// 如果包含了條件數據 則需要通過條件構造器封裝@Testpublic void select10(){List<Object> list = userMapper.selectObjs(null);System.out.println(list);}總結
以上是生活随笔為你收集整理的3、MybatisPlus的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 封锁ppstream,pplive,qq
- 下一篇: 连续被爆押金退款难 共享汽车会否走向和