mybatisPlus就是mybatis的一个增强工具,在不影响原mybatis功能的情况下,提供更多高效的功能来帮助使用mybatis。
一:使用
springboot+mybatis:
创建springboot工程,导入依赖:
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简化代码的?具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis-plus的springboot?持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?
useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL
=false
spring.datasource.username=root
spring.datasource.password=root
pojo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper
之前介绍过,mybatis中有个mapper插件,这里使用mybatis-plus后,使用BaseMapper即可。
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lagou.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.lagou.mp.mapper")
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
?测试类
import com.lagou.mp.mapper.UserMapper;
import com.lagou.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
}
?BaseMapper有很多基本方法:
二:常用属性:
1、configLocation 和?mapperLocations?
这个时候,如果要自定义全局配置文件或者mapper.xml,应该放哪里
全局:
mybatis-plus.config-location
=
classpath
:
mybatis-config.xml
mapper.xml两种常用方式:
1、在resources创建Mapper接口同名文件夹用来存放Mapper.xml文件,比如:
?
2、resources下随便定义,在配置文件中配置:
?
然后在application.properties中:?
第二种配置:
mybatis-plus.mapper-locations = classpath*:mapper/*.xml
2、?typeAliasesPackage
在mapper.xml中就只需写实体的类名,如User
mybatis-plus.type-aliases-package
=
com.lagou.mp.pojo
3、mapUnderscoreToCamelCase
是否开启驼峰命名映射,默认true,db中就会以下划线分割。
该参数不能和
mybatis-plus.config-location
同时存在
mybatis-plus.configuration.map-underscore-to-camel-case
=
false
4、cacheEnabled
是否开启二级缓存
mybatis-plus.configuration.cache-enabled=false?
注意,true的时候还需要在mapper.xml中开启
<cache></cache>?
5、idType
全局默认主键类型,设置后,即可省略实体对象中的@TableId(type = IdType.AUTO)配置?
mybatis-plus.global-config.db-config.id-type=auto?
6、tablePrefix
表名前缀,全局配置后可省略@TableName()配置?
mybatis-plus.global-config.db-config.table-prefix=tb_?
?
|