MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。
1、加載依賴
<!--mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.3.2</version> </dependency>
需要將 mybatis的jar 、mybatis與spring整合的jar 刪除
2、在applicationContext.xml中 改造sqlsessionFactoryBean, class修改為:
com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> </bean>
3、 修改Mapper接口, 讓項目的mapper接口繼承 MybatisPlus提供的父接口: BaseMapper
public interface UserMapper extends BaseMapper<User>{ }
4、在實體類, 進行表與類的映射
@Data public class House { private Integer id; private String name; }
插入數據時,要是使用序列,需要進行映射
在applicationContext.xml中 對 Oracle序列配置:
<!-- 序列生成策略 --> <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property name="dbConfig" ref="dbConfig" /> </bean> <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig"> <property name="keyGenerator" ref="keyGenerator" /> </bean> <bean id="keyGenerator" class="com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator" />
4、特別注意: 需要在selSessionFactoryBean加上globalConfig屬性
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!-- 添加oracle序列的全局配置 --> <property name="globalConfig" ref="globalConfig" /> </bean>
在實體類中指定序列屬性
@Data
@KeySequence("seq_id") //序列名
public class House {
@TableId(value = "id",type = IdType.INPUT) //指定屬性
private Integer id;
MybatisPlus中在applicationContext.xml中所有需要的配置,包括分頁插件 ***
<!-- ***修改SqlSessionFactory --> <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- TODO 加載sql映射文件 --> <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" /> <!--別名設置 -->
<property name="typeAliasesPackage" value="com.zl.house.entity" /> <!-- 添加oracle序列的全局配置 --> <property name="globalConfig" ref="globalConfig" />
<!-- 配置分頁插件 --> <property name="plugins"> <array> <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"> <!-- <property name="sqlParser" ref="自定義解析類、可以沒有" /> <property name="dialectClazz" value="自定義方言類、可以沒有" /> COUNT SQL 解析.可以沒有--> <property name="countSqlParser" ref="countSqlParser" /> </bean> </array> </property>
</bean> <bean id="countSqlParser" class="com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize"> <!-- 設置為 true 可以優化部分 left join 的sql --> <property name="optimizeJoin" value="true"/> </bean> <!-- 序列生成策略 --> <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <property name="dbConfig" ref="dbConfig" /> </bean> <bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig"> <property name="keyGenerator" ref="keyGenerator" /> </bean> <bean id="keyGenerator" class="com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator" />
獲取BaseMapper對象的兩種方式 ***
1、直接獲取:
getBaseMapper();
2、通過注入的方式
@Autowired
private IUserMapper userMapper;
private IUserMapper userMapper;
插入數據
實體類映射數據了表
@Data @AllArgsConstructor @NoArgsConstructor //對應數據庫表中的自增系列名 @KeySequence(value = "seqhouse" ) public class Users implements Serializable{ private static final long serialVersionUID = -809025169738892344L; //給指定類加上序列,input通過自己注冊自動填充插件進行填充 @TableId(value = "id",type = IdType.INPUT) private Integer id; //id編號 private String name; //用戶名 private String password; //密碼 private String telephone; //電話 private String username; //姓名 // 如果屬性名是駝峰命名法, 對應列, is_admin //@TableField("isAdmin") private String isadmin; //是否為管理員
mapper、service不用寫代碼
@TableId(value = "id",type = IdType.INPUT) -->綁定id列序列
如果不寫會使用雪花算法默認生成一個id(id列上加:@TableId(type = IdType.AUTO))
雪花算法: snowflake是Twitter開源的分布式ID生成算法,結果是一個long型的ID。其核心思想是:使用41bit作為 毫秒數,10bit作為機器的ID(5個bit是數據中心,5個bit的機器ID),12bit作為毫秒內的流水號(意味 着每個節點在每毫秒可以產生 4096 個 ID),最后還有一個符號位,永遠是0。可以保證幾乎全球唯 一!
//測試注冊 @Test public void testReg() throws Exception { Users user = new Users(); user.setName("qq"); user.setPassword("666666"); user.setTelephone(""); user.setUsername("小武");
查詢
// 測試查詢 @Test public void testSelectById(){ User user = userMapper.selectById(1L); System.out.println(user); } // 測試批量查詢! @Test public void testSelectByBatchId(){ List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3)); users.forEach(System.out::println); } // 按條件查詢之一使用map操作 @Test public void testSelectByBatchIds(){ HashMap<String, Object> map = new HashMap<>(); // 自定義要查詢 map.put("name","狂神說Java"); map.put("age",3); List<User> users = userMapper.selectByMap(map); users.forEach(System.out::println);
}
分頁查詢
// 測試分頁查詢 @Test public void testPage(){ // 參數一:當前頁 // 參數二:頁面大小 // 使用了分頁插件之后,所有的分頁操作也變得簡單的! Page<User> page = new Page<>(2,5); userMapper.selectPage(page,null); page.getRecords().forEach(System.out::println); System.out.println(page.getTotal()); }
刪除
// 測試刪除 @Test public void testDeleteById(){ userMapper.deleteById(1240620674645544965L); } // 通過id批量刪除 @Test public void testDeleteBatchId(){ userMapper.deleteBatchIds(Arrays.asList(1240620674645544961L,124062067464554496 2L)); } // 通過map刪除 @Test public void testDeleteMap(){ HashMap<String, Object> map = new HashMap<>(); map.put("name","狂神說Java"); userMapper.deleteByMap(map);