Spring Boot 中引入 MyBatisPlus 的常規(guī)流程
閱讀本文大概需要 5 分鐘。
一 前言:

二、通用 CRUD
通過本項(xiàng)目(表少,數(shù)據(jù)量大,非常適合) 發(fā)現(xiàn) MyBatisPlus 在單表 CRUD方面 比原來的的 MyBatis 的有著絕對優(yōu)勢:


@RunWith(SpringRunner.class)
@SpringBootTest
public?class?PlusApplicationTests?{
????@Autowired
????private?UserMapper?userMapper;
????/**
?????*?1、增加?insert
?????*/
????@Test
????public?void?insertTest()?{
????????User?user?=?new?User();
????????user.setUsername("綠茶");
????????user.setPassword("lvcha");
????????//?返回對數(shù)據(jù)庫影響操作數(shù):1
????????Integer?insert1?=?userMapper.insert(user);?//?非空屬性才會出現(xiàn)在?sql?中
????????//?可以立刻獲取插入新記錄的?id
????????System.out.println(user.getId());
????????//?同上
????????Integer?insert2?=?userMapper.insertAllColumn(user);?//?所有屬性都會出現(xiàn)在?sql?中
????????//?同上
????????System.out.println(user.getId());
????}
????/**
?????*?2、修改?update
?????*/
????@Test
????public?void?updateTest()?{
????????User?user?=?new?User();
????????user.setId(45);
????????user.setUsername("cun");
????????//user.setPassword("666");
????????//?返回對數(shù)據(jù)庫影響操作數(shù):1
????????Integer?integer1?=?userMapper.updateById(user);?//?屬性空則不修改
????????System.out.println(integer1);
????????//?同上
????????Integer?integer2?=?userMapper.updateAllColumnById(user);?//?屬性空則字段空
????????System.out.println(integer2);
????}
????/**
?????*?3、查詢?select
?????*/
????@Test
????public?void?selectTest()?{
????????//?根據(jù)id?查詢一條記錄
????????User?user?=?userMapper.selectById(46);
????????System.out.println(user.getUsername());
????????ArrayList?idList?=?new?ArrayList<>();
????????idList.add(61);
????????idList.add(63);
????????//?根據(jù)多個id?批量查詢
????????List?users?=?userMapper.selectBatchIds(idList);
????????System.out.println(users.get(0).getUsername()?+?users.get(1).getUsername());
????????User?user1?=?new?User();
????????user1.setId(61);
????????user1.setUsername("cun");
????????//?根據(jù)多個條件返回一個對象,若有多個符合條件的記錄則將報(bào)錯
????????User?user2?=?userMapper.selectOne(user1);
????????System.out.println(user2.getPassword());
????????//?根據(jù)多個條件返回對象組,注意Map?中的key?必須和數(shù)據(jù)庫表字段一直
????????HashMap?columnMap?=?new?HashMap<>();
????????columnMap.put("username",?"cun");
????????List?users1?=?userMapper.selectByMap(columnMap);
????????System.out.println(users1.size());
????}
????/**
?????*?4、刪除?delete
?????*/
????@Test
????public?void?deleteTest()?{
????????//?根據(jù)一個id 刪除,返回對數(shù)據(jù)庫影響操作數(shù):1
????????Integer?integer1?=?userMapper.deleteById(65);//?根據(jù)id刪除一條記錄
????????System.out.println(integer1);
????????ArrayList?idList?=?new?ArrayList<>();?//?根據(jù)id集合批量刪除
????????idList.add(64);
????????idList.add(66);
????????//?根據(jù)多個id 批量刪除,返回對數(shù)據(jù)庫影響操作數(shù):2
????????Integer?integer2?=?userMapper.deleteBatchIds(idList);
????????System.out.println(integer2);
????????HashMap?columnMap?=?new?HashMap<>();
????????columnMap.put("username",?"cun");
????????//?根據(jù)多個條件刪除,返回對數(shù)據(jù)庫影響操作數(shù)
????????Integer?integer3?=?userMapper.deleteByMap(columnMap);
????????System.out.println(integer3);
????}
????/**
?????*?5、偽分頁(獲取全部數(shù)據(jù)再分頁)
?????*/
????@Test
????public?void?pageTest()?{
????????//?分頁查詢,注意如果設(shè)置了?PaginationInterceptor?分頁插件則會報(bào)錯,
????????List?users2?=?userMapper.selectPage(new?Page (1,?2),?null);?//當(dāng)前頁、每頁大小
????????System.out.println(users2.get(0).getUsername()?+?users2.get(1).getUsername());
????????System.out.println(users2.size());
????}
????/**
?????*?6、條件構(gòu)造器
?????*/
????@Test
????public?void?wrapperTest(){
????????List?users?=?userMapper.selectList(new?EntityWrapper ()
????????????????.eq("username",?"linhongcun")
????????);
????????System.out.println(users.size());
????}
}
三、代碼生成器
public?class?MpG?{
????public?static?void?main(String[]?args)?{
????????//1.?全局配置
????????GlobalConfig?config?=?new?GlobalConfig();
????????config.setActiveRecord(false)?//?是否支持AR模式
????????????????.setAuthor("linhongcun")?//?作者
????????????????.setOutputDir("C:\\data\\mp")?//?生成路徑
????????????????.setFileOverride(true)?//?文件覆蓋
????????????????.setIdType(IdType.AUTO)?//?主鍵策略
????????????????.setServiceName("%sService")?//?設(shè)置生成的service接口的名字的首字母是否為I
????????????????//?IUserService
????????????????.setBaseResultMap(true)
????????????????.setBaseColumnList(true);
????????//2.?數(shù)據(jù)源配置
????????DataSourceConfig?dsConfig?=?new?DataSourceConfig();
????????dsConfig.setDbType(DbType.MYSQL)?//?設(shè)置數(shù)據(jù)庫類型
????????????????.setDriverName("com.mysql.jdbc.Driver")
????????????????.setUrl("jdbc:mysql://120.79.197.130:3307/testspring?useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8")
????????????????.setUsername("root")
????????????????.setPassword("123");
????????//3.?策略配置
????????StrategyConfig?stConfig?=?new?StrategyConfig();
????????stConfig.setCapitalMode(true)?//全局大寫命名
????????????????.setDbColumnUnderline(true)?//?指定表名?字段名是否使用下劃線
????????????????.setNaming(NamingStrategy.underline_to_camel)?//?數(shù)據(jù)庫表映射到實(shí)體的命名策略
????????????????.setTablePrefix("tb_")
????????????????.setInclude("tb_user");?//?生成的表
????????//4.?包名策略配置
????????PackageConfig?pkConfig?=?new?PackageConfig();
????????pkConfig.setParent("com.cun.plus")
????????????????.setMapper("mapper")
????????????????.setService("service")
????????????????.setController("controller")
????????????????.setEntity("entity")
????????????????.setXml("mapper");
????????//5.?整合配置
????????AutoGenerator?ag?=?new?AutoGenerator();
????????ag.setGlobalConfig(config)
????????????????.setDataSource(dsConfig)
????????????????.setStrategy(stConfig)
????????????????.setPackageInfo(pkConfig);
????????//6.?執(zhí)行
????????ag.execute();
????}
}
四、相關(guān)依賴
//?選擇?freemarker?引擎,默認(rèn)?Veloctiy
//?mpg.setTemplateEngine(new?FreemarkerTemplateEngine());
????????<dependency>
????????????<groupId>com.baomidougroupId>
????????????<artifactId>mybatis-plus-boot-starterartifactId>
????????????<version>2.3version>
????????dependency>
????????
????????<dependency>
????????????<groupId>org.apache.velocitygroupId>
????????????<artifactId>velocity-engine-coreartifactId>
????????????<version>2.0version>
????????dependency>
推薦閱讀:
Google 開源的依賴注入庫,比 Spring 更小更快!
微信掃描二維碼,關(guān)注我的公眾號
朕已閱?

