springboot与mybatis配置及练习

创建springboot项目并添加依赖:lombok注解(减少代码量)、mybatis框架、mysql驱动


创建完成之后先创建一个数据库,我这里命名为mybatis,里面设置两张表emp和user(表结构及数据均为自行编造)

emp:

user:

之后,在source->main->resources->applicaton文件中配置数据库相关信息
其中最后两行分别是mybatis日志和开启数据库驼峰命名自动映射

回到java文件夹,在edu.wust文件夹下分别创建两个文件夹mapper和pojo,mapper用于存放接口文件,pojo存放类文件

在mapper文件中创建一个接口,用于控制数据的增删改查:

package edu.wust.mapper;

import edu.wust.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface EmpMapper {
    //根据id删除数据
    @Delete("delete  from emp where id = #{id}")
    public void delete(Integer id);



    //新增员工

    @Insert("insert into emp (username, name, gender, image, job, entryDate, dept_id, create_time, update_time)" +
            "values (#{username},#{name},#{gender},#{image},#{job},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    public void insert(Emp emp);


    //更新员工
    @Update("update emp set username=#{username},name=#{name},gender=#{gender},image=#{image},"+
    "job=#{job},entryDate=#{entryDate},dept_id=#{deptId},update_time=#{updateTime} where id=#{id}")
    public void update(Emp emp);



    //根据id查询员工
    @Select("select id, username, password, name, gender, image, job, entryDate, "+
            "dept_id deptId, create_time createTime, update_time updateTime from emp where id=#{id}")
    public Emp getById(Integer id);



    //条件查询员工
    @Select("select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and "+
            " entryDate between #{begin} and #{end} order by  update_time desc")
    public List<Emp> list(String name, Short gender, LocalDate begin,LocalDate end);


}

在test->java->edu.wust中的test文件中编写测试类:
 

package edu.wust;

import edu.wust.mapper.EmpMapper;
import edu.wust.pojo.Emp;
import org.apache.ibatis.annotations.Insert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cglib.core.Local;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@SpringBootTest
class SpringbootMybatisCrudApplicationTests {
    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testDelete() {
        empMapper.delete(11);
    }

    @Test
    public void testInsert(){
        Emp emp=new Emp();
        emp.setUsername("jerry");
        emp.setName("杰瑞");
        emp.setImage("11.jpg");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntryDate(LocalDate.of(2000,1,1));
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(1);

        empMapper.insert(emp);
    }

    @Test
    public void testUpdate(){
        Emp emp=new Emp();
        emp.setId(12);
        emp.setUsername("tom");
        emp.setName("汤姆");
        emp.setImage("12.jpg");
        emp.setGender((short)1);
        emp.setJob((short)1);
        emp.setEntryDate(LocalDate.of(2000,1,1));
        emp.setUpdateTime(LocalDateTime.now());
        emp.setDeptId(1);

        //执行更新员工操作
        empMapper.update(emp);
    }


    //根据id精确查询
    @Test
    public void testGetById(){
        Emp emp=empMapper.getById(10);
        System.out.println(emp);
    }

    
    //模糊查询
    @Test
    public void testList(){
        List<Emp> empList = empMapper.list("张", (short) 1, LocalDate.of(2000, 1, 1), LocalDate.of(2020, 1, 1));
        System.out.println(empList);
    }

}

测试功能是否能够正常运行

运行结果(以查询为例):

在运行复杂条件的时候使用注解会显得繁琐,容易出错,因此可以使用xml文件来代替注解:

通常在resources文件夹中导入xml文件,并且xml文件要与接口同包同名

创建xml文件之后,在mybatis中文网中找到约束,导入到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">

添加包,对象,接口,执行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="edu.wust.mapper.EmpMapper">

    <select id="list" resultType="edu.wust.pojo.Emp">
        select * from emp where name like concat('%',#{name},'%') and gender=#{gender} and
            ntryDate between #{begin} and #{end} order by  update_time desc
    </select>

</mapper>

注意:xml文件中的sql语句只能使用单引号

 下载mybatisx插件后,点击sql语句旁边的蓝色小鸟,会自动跳转到测试类的程序处:

这里将注解注释掉,会发现程序仍然能继续运行:



 

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值