zmysna 2020-07-04
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;<!-- ***修改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" />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);