From 619600e09ad7abe73550cb7e038406209024f568 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Sat, 24 Oct 2026 11:07:18 +0800 Subject: [PATCH] =?UTF-8?q?javaEEday09-Mybatis-=E6=96=B0=E5=A2=9EID?= =?UTF-8?q?=E5=9B=9E=E6=98=BE&=E6=9D=A1=E4=BB=B6=E6=9F=A5=E8=AF=A2&XML?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E6=96=87=E4=BB=B6=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/inmind/mapper/EmpMapper.java | 52 +++++++++++++++++- .../src/main/resources/application.properties | 3 +- .../resources/com/inmind/mapper/EmpMapper.xml | 12 ++++ ...SpringbootMybatisCrudApplicationTests.java | 55 +++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml diff --git a/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java b/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java index c25c349..f03a795 100644 --- a/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java +++ b/springboot-mybatis-crud/src/main/java/com/inmind/mapper/EmpMapper.java @@ -1,7 +1,10 @@ package com.inmind.mapper; -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Mapper; +import com.inmind.pojo.Emp; +import org.apache.ibatis.annotations.*; + +import java.time.LocalDate; +import java.util.List; @Mapper public interface EmpMapper { @@ -10,4 +13,49 @@ public interface EmpMapper { // @Delete("delete from emp where id = ${id};")//${}拼接的方式,有sql注入问题 int delete(Integer id);//想返回删除数据的个数 // void delete(Integer id);//不想获得删除数据的个数 + + + /* + 新增员工 + */ + @Options(keyProperty = "id",useGeneratedKeys = true) + @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 as deptId, create_time as createTime, update_time as updateTime from emp where id = #{id}") + //数据封装,方案二:mybatis注解 + /*@Results( + { + @Result(column = "dept_id",property = "deptId"), + @Result(column = "create_time",property = "createTime"), + @Result(column = "update_time",property = "updateTime") + } + )*/ + @Select("select * from emp where id = #{id}") + public Emp selectById(Integer id); + + /* + 条件查询 + */ +// @Select("select * from emp where name like '%${name}%' and gender = #{gender} and entrydate between #{begin} and #{end}") + @Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and entrydate between #{begin} and #{end}") + public List list(String name, Short gender, LocalDate begin, LocalDate end); +// public List list(@Param("name") String name,@Param("gender") Short gender,@Param("begin") LocalDate begin,@Param("end")LocalDate end); + + + // @Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender} and entrydate between #{begin} and #{end}") + public List list1(String name,Short gender,LocalDate begin,LocalDate end); + } diff --git a/springboot-mybatis-crud/src/main/resources/application.properties b/springboot-mybatis-crud/src/main/resources/application.properties index 9737b62..ffb8fdc 100644 --- a/springboot-mybatis-crud/src/main/resources/application.properties +++ b/springboot-mybatis-crud/src/main/resources/application.properties @@ -5,4 +5,5 @@ spring.datasource.username= root spring.datasource.password= 1234 spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver -mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl \ No newline at end of file +mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl +mybatis.configuration.map-underscore-to-camel-case=true diff --git a/springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml b/springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml new file mode 100644 index 0000000..7103195 --- /dev/null +++ b/springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java index fc492da..f36dc93 100644 --- a/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java +++ b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java @@ -1,11 +1,16 @@ package com.inmind; import com.inmind.mapper.EmpMapper; +import com.inmind.pojo.Emp; import org.junit.jupiter.api.AutoClose; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; + @SpringBootTest class SpringbootMybatisCrudApplicationTests { @@ -23,4 +28,54 @@ class SpringbootMybatisCrudApplicationTests { System.out.println("删除数据的条数:"+deleteCount); } + @Test + void testInsert() { + Emp emp = new Emp(); + emp.setUsername("zhangsan2"); + emp.setName("张三"); + emp.setGender((short) 1); + emp.setImage("1.jpg"); + emp.setEntrydate(LocalDate.of(2025,10,26)); + emp.setDeptId(1); + emp.setCreateTime(LocalDateTime.now()); + emp.setUpdateTime(LocalDateTime.now()); + empMapper.insert(emp); + System.out.println(emp); + } + + @Test + void testUpdate() { + Emp emp = new Emp(); + emp.setId(21); + emp.setUsername("zhangsan3"); + emp.setName("张三666"); + emp.setGender((short) 1); + emp.setImage("2.jpg"); + emp.setEntrydate(LocalDate.of(2025,10,26)); + emp.setDeptId(1); + emp.setCreateTime(LocalDateTime.now()); + emp.setUpdateTime(LocalDateTime.now()); + empMapper.update(emp); + System.out.println(emp); + } + + @Test + void testSelect() { + Integer id = 21; + Emp emp = empMapper.selectById(id); + System.out.println(emp); + } + + @Test + void testList() { + List list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1)); + System.out.println(list); + } + + @Test + void testList1() { + List list = empMapper.list1("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1)); + System.out.println(list); + } + }