说明:
(1)本篇博客合理性解释:
● 在【SSM开发书评网7:项目准备与SSM整合五:Mybatis-Plus简介;SSM整合Mybatis-Plus;】中:【介绍了Mybatis-plus】【SSM和Mybatis-Plus整合】【Mybatis-Plus开发步骤】;
● 那么本篇博客,就通过代码实际演示Mybatis-Plus的使用;
(2)通过本篇博客,目前感受到Mybatis-Plus的三个重要的点:
● 知道Mybatis-Plus的开发套路;
● BaseMapper接口中常用的方法,需要慢慢积累,等到具体业务的时候,心里清楚应该用哪个方法;
● 查询的时候,需要用到QueryWrapper查询构造器,这儿只是对其做了简要介绍,QueryWrapper很多东西可以在需要的时候,去查就是了;
目录
零:【回顾一下】:当我们在SSM项目中,使用Mybatis时:开发套路;
3.Mapper接口:然后,在(普遍采用的)接口开发方式中,我们需要创建一个【用于操作test表的接口:TestDao接口】;
4.xml实现:然后,就是创建test.xml,编写SQL语句,去实现TestDao接口中定义的方法了;
5.然后,就可以调用TestDao中的方法,愉快的操作数据库了;
一:当我们在SSM项目中,使用Mybatis-Plus:开发演示;(走了一遍Mybatis-Plus的流程)
1.数据表:为了演示Mybatis-Plus,先创建一个测试用的表:test_mp表;
2.实体类:创建与test_mp表对应的实体类:TestMp实体类:实体类使用了【@TableName,@TableId,@TableField】注解;
3.Mapper接口:然后,我们需要创建一个【用于操作test_mp表的接口:TestMpDao接口,该接口需要继承BaseMapper接口】;
4.xml:然后,就是创建test_mp.xml,以对应TestMpDao接口;
(2)Mybatis-Plus补充:Mybatis-Plus只是对Mybatis的增强, 原先Mybatis的东西自然依旧可以使用;
二:继续演示Mybatis-Plus;(其实,也是演示BaseMapper的其他方法了)
3.演示三:试一下查询的方法;(查询时,涉及到了QueryWrapper查询构造器,组织查询条件)
(3)QueryWrapper查询构造器:组织查询条件:的一点说明;(重要!!!)
零:【回顾一下】:当我们在SSM项目中,使用Mybatis时:开发套路;
当我们只使用Mybatis时,要想实现操作数据库,基本需要以下几个步骤:
1.数据表:自然,数据库中要有个待访问的表;
2.实体类:然后,需要有个实体类来对应这个表;
Test实体类与test表对应,按照比较规范的开发方式,因为设置了驼峰命名,Test类的属性应该比照test表的字段名来写;
package com.imooc.reader.entity; /** * 测试用的实体类,与test表对应; */ public class Test { private Integer id; private String content; public Test() { } public Test(Integer id, String content) { this.id = id; this.content = content; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
3.Mapper接口:然后,在(普遍采用的)接口开发方式中,我们需要创建一个【用于操作test表的接口:TestDao接口】;
TestDao接口:在这儿,我们在接口中,示意性的定义了操作test表的方法;
package com.imooc.reader.mapper; import com.imooc.reader.entity.Test; /** * 演示Mybatis的,测试用的MapperDao,操作test表; */ public interface TestDao { public Test selectById(); public void insert(Test test); }
4.xml实现:然后,就是创建test.xml,编写SQL语句,去实现TestDao接口中定义的方法了;
test.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.imooc.reader.mapper.TestDao"> <select id="selectById" parameterType="Integer" resultType="com.imooc.reader.entity.Test"> select * from test where id = #{value} </select> <insert id="insert"> insert into test(content) values (#{content}) </insert> </mapper>
5.然后,就可以调用TestDao中的方法,愉快的操作数据库了;
我们在TestService类中,编写调用代码;
package com.imooc.reader.service; import com.imooc.reader.entity.Test; import com.imooc.reader.mapper.TestDao; import com.imooc.reader.mapper.TestMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service public class TestService { @Resource private TestDao testDao; @Transactional public void testMybatis() { Test test = testDao.selectById(38); System.out.println(test.getContent()); Test test1 = new Test(); test1.setContent("hehehehe"); testDao.insert(test1); } }
然后,在TestServiceTest测试类中,去测试;
package com.imooc.reader.service; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class TestServiceTest { @Resource private TestService testService; @Test public void testMybatis() { testService.testMybatis(); } }
总结;
主要想指出的是:我们在接口中定义的方法,需要在xml中编写SQL去实现;