整合前的基本过程
创建数据表
创建实体类
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;
}
}
创建mapper接口
/**
* 演示Mybatis的,测试用的MapperDao,操作test表;
*/
public interface TestDao {
public Test selectById();
public void insert(Test test);
}
创建mapper.xml,编写SQL语句,实现接口定义的方法
<?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>
调用Dao接口中的方法,操作数据库
@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);
}
}
编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestServiceTest {
@Resource
private TestService testService;
@Test
public void testMybatis() {
testService.testMybatis();
}
}
MyBatisPlus的使用
创建实体类
@TableId注解当中type代表主键的生成方式, IdType.AUTO表示利用数据库底层的自增 主键来完成数据的插入工作。
如果实体类的属性名和数据表的字段名相同,或者符合其符合驼峰命名规则的话:实体类属性上的@TableField注解可以省略。
package com.imooc.reader.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("test_mp")//说明这个实体类对应于哪张表;
public class TestMp {
@TableId(type = IdType.AUTO) //说明这个属性对应了表的主键;
@TableField("id")//说明属性对应于哪个字段;
private Integer id;
@TableField("content")
private String content;
public TestMp() {
}
public TestMp(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;
}
}
创建Mapper接口
BaseMapper接口作为父接口,提供如 新增、修改、删除、查询 等方法的声明,在定义的时候需要传入一个泛型,说明对应的是哪个实体类;TestMpDao接口继承了BaseMapper接口,自然也继承了BaseMapper接口中的方法。
BaseMapper接口中,定义了一系列select、insert、update、delete方法;这些方法需要慢慢熟悉和记忆;自己写的接口继承了BaseMapper接口,自然也继承了BaseMapper接口中的方法。
package com.imooc.reader.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imooc.reader.entity.TestMp;
public interface TestMpDao extends BaseMapper<TestMp>{
}
创建mapper.xml,对应TestMpDao接口
在这个xml文件当中并没有编写SQL语句实现TestMpDao接口中的方法,按照Mybatis的套路, 我们需要在xml文件中编写SQL语句,来实现DAO接口中的方法,但是因为现在使用MyBatisPlus,我们在xml中没有编写任何SQL语句去实现接口中的方法。
虽然在xml文件中并没有编写SQL去实现insert()方法,但是随着loC容器初始化的过程中,MybatisPlus会根据我们在TestMp实体类中定义的映射关系,自动的去生成insert()方法的SQL语句。
mybatisplus并没有修改mybatis的基础,所以原先mybatis的东西还可以继续使用,我们仍然可以在接口当中自己定义方法,然后在xml文件去编写sql语句实现方法,不过需要注意自己定义的方法,不要和BaseMapper接口中方法重名。
<?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.TestMpDao">
</mapper>
测试用例:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Test;
import com.imooc.reader.mapper.TestMapper;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MyBatisPlusTest {
@Resource
private TestMapper testMapper;
@org.junit.Test
public void testInsert(){
Test test = new Test();
test.setContent("MyBatis Plus测试");
testMapper.insert(test);
}
@org.junit.Test
public void testUpdate(){
Test test = testMapper.selectById(9);
test.setContent("MyBatis Plus测试1");
testMapper.updateById(test);
}
@org.junit.Test
public void testDelete(){
testMapper.deleteById(9);
}
@org.junit.Test
public void testSelect(){
//QueryWrapper是查询条件构造器,其作用就是组织查询所需的条件;
//这儿需要传入实体类的泛型;
QueryWrapper<Test> queryWrapper = new QueryWrapper<Test>();
//然后通过QueryWrapper的一系列的方法,去组织SQL查询的条件;
queryWrapper.eq("id", 7); //eq(方法就代表等值比较;比如这儿eq()的方法的意思是:查询id=7的记录;(字段名,字段值)
queryWrapper.gt("id", 5);//gt是大于的意思,该语句的意思是查询所有id大于5的数据
/*
默认情况下,如果写了多条子句的话,他会用and符号进行连接
*/
List<Test> list = testMapper.selectList(queryWrapper);
System.out.println(list.get(0));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99469.html