1、Mybatis-Plus介绍:
Mybatis-Plus并没有修改Mybatis的任何特性。
2、示例:
2.1 需求:使用Mybatis-Plus对实现用户的crud操作。
2.2 配置步骤说明:
(1)搭建环境(创建项目,导入包)
(2)配置Mybatis-plus(基于Spring实现)
(3)编写测试代码
2.3 配置步骤
2.3.1 第一步:搭建环境
前提:已经创建好了数据库

说明:
(1)Mybatis-Plus并没有提供单独的jar包,而是通过Maven(或者gradle)来管理jar依赖。本教程需要使用Maven构建项目。
(2)Mybatis-Plus是基于Spring框架实现的,因此使用Mybatis-Plus,必须导入Spring相关依赖。
2.3.2、创建项目
2.3.3、添加依赖
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <!-- Mybatis-plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus</artifactId>
- <version>2.3</version>
- </dependency>
- <!-- Spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.2.6.RELEASE</version>
- </dependency>
- <!-- MySQL -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.16</version>
- </dependency>
- <!-- 阿里巴巴连接池 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.21</version>
- </dependency>
- <!-- Spring整合JDBC -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>5.2.6.RELEASE</version>
- </dependency>
- <!-- Spring AOP -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>5.2.6.RELEASE</version>
- </dependency>
- <!-- Spring测试-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>5.2.6.RELEASE</version>
- <scope>test</scope>
- </dependency>
2.3.4、 创建Book实体类
说明:使用Mybatis-Plus可以不使用xml文件,而是基于一组注解来解决实体类和数据库表的映射问题。
| @TableName(value="ssm_book") | 指定对应的表,表名和类名一致时,可以省略value属性。 |
| @TableId | 指定表的主键。Value属性指定表的主键字段,和属性名一致时,可以省略。Type指定主键的增长策略。 |
| @TableField | 指定类的属性映射的表字段,名称一致时可以省略该注解。 |
- @Data
- @TableName("ssm_book")
- public class Book {
- @TableId(value = "book_id",type = IdType.AUTO)
- private Integer bookId;
- @TableField(value = "book_name")
- private String bookName;
- @TableField(value = "book_author")
- private String bookAuthor;
- @TableField(value = "book_date")
- private Date bookDate;
- @TableField(value = "book_price")
- private String bookPrice;
- }
2.3.5、创建BookMapper接口(继承BaseMapper接口)
- public interface BookMapper extends BaseMapper<Book> {
- }
2.3.6、Spring整合Mybatis-Plus
- <context:component-scan base-package="com.plus"/>
-
- <!--1、配置数据源-->
- <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/mvc?serverTimezone=UTC"/>
- <property name="username" value="root"/>
- <property name="password" value="123456"/>
-
- <property name="maxActive" value="30"/>
- <property name="minIdle" value="5"/>
- </bean>
- <!--2、Spring整合Mybatis-Plus-->
- <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
- <!--2.1 加载数据源-->
- <property name="dataSource" ref="druidDataSource"/>
- <!--2.2 指定pojo包-->
- <property name="typeAliasesPackage" value="com.plus.pojo"/>
- <!--2.3 配置mybatis-plus插件-->
- <property name="plugins">
- <list>
- <!--配置分页插件-->
- <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"/>
- <!--配置拦截器属性-->
- <bean class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
- <property name="maxTime" value="1000"/>
- <property name="format" value="true"/>
- </bean>
- </list>
- </property>
- <property name="globalConfig" ref="globalConfiguration"/>
- </bean>
- <!--3、配置Mybatis-Plus全局属性-->
- <bean id="globalConfiguration" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
- <!--2.3以后的版本中dbColumnUnderline默认为true,即开启驼峰命名-->
- <property name="dbColumnUnderline" value="true"/>
- <!--全局主键策略
- 0 :AUTO(主键自增)
- 1 :INPUT(用户输入ID)
- 2 :ID_WORKER(全局唯一ID)
- 3 :UUID(全局唯一ID)
- -->
- <property name="idType" value="0"/>
- <property name="tablePrefix" value="ssm_"/>
- </bean>
-
- <!--4、配置Mybatis动态代理-->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
- <property name="basePackage" value="com.plus.mapper"/>
- </bean>
-
- <!--5、配置事务管理器-->
- <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="druidDataSource"/>
- </bean>
-
- <!--6、开启注解声明式事务-->
- <tx:annotation-driven/>
2.4 测试:
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = {"classpath:spring-data.xml"})
- @ComponentScan(basePackages = "com.plus")
- public class BookMapperTest {
- @Autowired
- private BookMapper bookMapper;
-
- //查询所有记录
- @Test
- public void selectAll(){
- List<Book> bookList = bookMapper.selectList(null);
- for(Book book : bookList){
- System.out.println(book);
- }
- }
-
- //插入记录
- @Test
- public void insert(){
- Book book = new Book(null,"Mybatis-Plus","李刚",new Date(),78.5);
- bookMapper.insert(book);
- }
-
- //更加id查询
- @Test
- public void selectById(){
- Book book = bookMapper.selectById(57);
- System.out.println(book);
- }
-
- //按图书名查找
- @Test
- public void selectByName(){
- EntityWrapper<Book> wrapper = new EntityWrapper<>();
- wrapper.eq("book_name","平凡的世界");
- List<Book> bookList = bookMapper.selectList(wrapper);
- for(Book book :bookList){
- System.out.println(book);
- }
- }
-
- //分页查询,第1页,每页显示5条
- @Test
- public void pageList(){
- RowBounds rowBounds = new RowBounds(0,5);
- List<Book> books = bookMapper.selectPage(rowBounds,null);
- for(Book book:books){
- System.out.println(book);
- }
- }
-
- //模糊查询
- @Test
- public void selectByLike(){
- EntityWrapper<Book> wrapper = new EntityWrapper<>();
- wrapper.like("book_author","%陈%");
- List<Book> books = bookMapper.selectList(wrapper);
- for(Book book:books){
- System.out.println(book);
- }
- }
- }