MyBatis Plus介紹

          MyBatis Plus?(簡稱MP)是國內人員開發(fā)的 MyBatis 增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生。

          特征

          無侵入:Mybatis-Plus 在 Mybatis 的基礎上進行擴展,只做增強不做改變,引入 Mybatis-Plus 不會對您現(xiàn)有的 Mybatis
          構架產生任何影響,而且 MP 支持所有 Mybatis 原生的特性
          依賴少:僅僅依賴 Mybatis 以及 Mybatis-Spring
          損耗?。簡蛹磿詣幼⑷牖綜URD,性能基本無損耗,直接面向對象操作
          預防Sql注入:內置Sql注入剝離器,有效預防Sql注入攻擊
          通用CRUD操作:內置通用 Mapper、通用 Service,僅僅通過少量配置即可實現(xiàn)單表大部分 CRUD 操作,更有強大的條件構造器,滿足各類使用需求
          多種主鍵策略:支持多達4種主鍵策略(內含分布式唯一ID生成器),可自由配置,完美解決主鍵問題

          框架結構



          Mybatis Plus使用

          1.導入依賴
          <dependency> <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus</artifactId> <version>2.0.1</version> </dependency>
          2.MP整合的配置
          <!-- 0.連接池屬性設置讀取指定的properties文件 --> <context:property-placeholder
          location="classpath:db.properties" ignore-unresolvable="true"/> <!--
          1.將連接池放入spring容器 --> <bean name="dataSource"class
          ="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl"
          value="${jdbc.url}"></property> <property name="driverClass"
          value="${jdbc.driver}"></property> <property name="user"
          value="${jdbc.username}"></property> <property name="password"
          value="${jdbc.password}"></property> </bean> <bean id="sqlSessionFactory"class
          ="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> <property
          name="dataSource" ref="dataSource"/> <!-- 配置實體掃描路徑,多個package可以用分號; 逗號, 分隔,
          支持通配符*--> <!-- com.a.b.entity;com.a.c.entity;com.d.*.entity--> <property
          name="typeAliasesPackage" value="cn.xm.jwxt.bean.*"/> <property
          name="configuration" ref="mybatisConfig"/> <!-- MP 全局配置注入 --> <property
          name="globalConfig" ref="globalConfig"/> <property name="plugins"> <array> <!--
          分頁插件配置 --> <bean id="paginationInterceptor"class
          ="com.baomidou.mybatisplus.plugins.PaginationInterceptor"/> <!--
          性能攔截器,兼打印sql,不建議生產環(huán)境配置--> <bean id="performanceInterceptor"class
          ="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"/> </array>
          </property> </bean> <bean id="mybatisConfig"class
          ="com.baomidou.mybatisplus.MybatisConfiguration"> <property
          name="mapUnderscoreToCamelCase" value="true"/> </bean> <!-- 定義 MP 全局策略 -->
          <bean id="globalConfig"class
          ="com.baomidou.mybatisplus.entity.GlobalConfiguration"> <property name="idType"
          value="2"/> <property name="dbType" value="mysql"/> <!-- 全局表為下劃線命名設置true -->
          <property name="dbColumnUnderline" value="true"/> </bean> <!-- 配置mybatis
          掃描mapper接口的路徑,
          相當于注解@MapperScan,@MapperScan("com.baomidou.mybatisplus.test.h2.entity.mapper")-->
          <bean id="mapperScannerConfigurer"class
          ="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property
          name="basePackage" value="cn.xm.jwxt.mapper"/> </bean>
          3.實體
          @TableName(value="employee")
          public class Employee { private Integer id ; private String lastName; private
          String email ;private Integer gender; private Integer age ; public Integer
          getId() {return id; } public void setId(Integer id) { this.id = id; } public
          String getLastName() {return lastName; } public void setLastName(String
          lastName) {this.lastName = lastName; } public String getEmail() { return email;
          }public void setEmail(String email) { this.email = email; } public Integer
          getGender() {return gender; } public void setGender(Integer gender) { this
          .gender = gender; } public Integer getAge() { return age; } public void
          setAge(Integer age) {this.age = age; } @Override public String toString() {
          return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email +
          ", gender=" + gender + ", age=" + age + "]"; } }
          4.mapper 接口
          public interface EmployeeMapper extends BaseMapper<Employee> {}
          5.新增
          @Test public void testCommonInsert() { //初始化Employee對象 Employee employee = new
          Employee(); employee.setLastName("MP"); employee.setEmail("[email protected]");
          employee.setGender(1); //insert方法在插入時,會根據實體類的每個屬性進行非空判斷,只有非空的屬性對應的字段才會出現(xiàn)到SQL語句中
          Integer result = employeeMapper.insert(employee); System.out.println("result: "
          + result ); //insertAllColumn方法在插入時,不管屬性是否非空, 屬性所對應的字段都會出現(xiàn)到SQL語句中. Integer
          result1 = employeeMapper.insertAllColumn(employee); System.out.println("result:
          " + result1 ); //獲取當前數據在數據庫中的主鍵值 Integer key = employee.getId();
          System.out.println("key:" + key ); }
          6.修改
          @Test public void testCommonUpdate() { //初始化修改對象 Employee employee = new
          Employee(); employee.setId(7); employee.setLastName("王五"); employee.setEmail(
          "[email protected]"); employee.setGender(0); //
          updateById方法在修改時,會根據實體類的每個屬性進行非空判斷,只有非空的屬性對應的字段才會出現(xiàn)到SQL語句中 Integer result =
          employeeMapper.updateById(employee); System.out.println("result: " + result );
          //updateAllColumnById方法在修改時,不管屬性是否非空, 屬性所對應的字段都會出現(xiàn)到SQL語句中. Integer result1 =
          employeeMapper.updateAllColumnById(employee); System.out.println("result: " +
          result1 ); }
          7.查詢
          @Test public void testCommonSelect() { //1. 通過id查詢 Employee employee =
          employeeMapper.selectById(7); System.out.println(employee); //2.
          通過多個列進行查詢。selectOne查詢結果只能是一條,否則報錯 Employee employee1 = new Employee();
          employee.setLastName("王五"); employee.setGender(0); Employee result =
          employeeMapper.selectOne(employee1); System.out.println("result: " +result ); //
          3. 通過多個id進行查詢 List<Integer> idList = new ArrayList<>(); idList.add(4);
          idList.add(5); idList.add(6); idList.add(7); List<Employee> emps =
          employeeMapper.selectBatchIds(idList); System.out.println(emps);//4.
          通過Map封裝條件查詢。map中的key是數據庫中字段名 Map<String,Object> columnMap = new HashMap<>();
          columnMap.put("last_name", "Tom"); columnMap.put("gender", 1); List<Employee>
          emps1 = employeeMapper.selectByMap(columnMap); System.out.println(emps1); //5.
          分頁查詢 List<Employee> emps2 = employeeMapper.selectPage(new Page<>(3, 2), null);
          System.out.println(emps2); }
          8.刪除
          @Test public void testCommonDelete() { //1 .根據id進行刪除 Integer result =
          employeeMapper.deleteById(13); System.out.println("result: " + result ); //2.
          根據條件進行刪除 Map<String,Object> columnMap = new HashMap<>(); columnMap.put(
          "last_name", "MP"); columnMap.put("email", "[email protected]"); Integer result1 =
          employeeMapper.deleteByMap(columnMap); System.out.println("result: " + result1
          );//3. 批量刪除 List<Integer> idList = new ArrayList<>(); idList.add(3); idList.add(
          4); idList.add(5); Integer result2 = employeeMapper.deleteBatchIds(idList);
          System.out.println("result: " + result2 ); }

          友情鏈接
          ioDraw流程圖
          API參考文檔
          OK工具箱
          云服務器優(yōu)惠
          阿里云優(yōu)惠券
          騰訊云優(yōu)惠券
          京東云優(yōu)惠券
          站點信息
          問題反饋
          郵箱:[email protected]
          QQ群:637538335
          關注微信

                中文字幕精品视频在线 | 伊人在线观看视频 | www.iepai.com | 国产成人综合久久久久99 | 天天综合爱爱网 |