From f6d39acad22ebbb33463d5fbb5b79f0dcded9ecb Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Fri, 26 Sep 2025 13:53:33 +0800 Subject: [PATCH] =?UTF-8?q?1.Spring-mybatis-XML=E6=96=B9=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E5=85=A5=E9=97=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-mybatis-crud/pom.xml | 5 +++ .../java/com/inmind/mapper/EmpMapper.java | 37 +++++++++++++++++-- .../src/main/resources/application.properties | 5 ++- .../resources/com/inmind/mapper/EmpMapper.xml | 11 ++++++ ...SpringbootMybatisCrudApplicationTests.java | 15 ++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml diff --git a/springboot-mybatis-crud/pom.xml b/springboot-mybatis-crud/pom.xml index 2a0c3b8..89e769c 100644 --- a/springboot-mybatis-crud/pom.xml +++ b/springboot-mybatis-crud/pom.xml @@ -48,6 +48,7 @@ + @@ -58,6 +59,10 @@ 11 11 UTF-8 + + + -parameters + 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 431adcb..964b747 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 @@ -3,6 +3,9 @@ package com.inmind.mapper; import com.inmind.pojo.Emp; import org.apache.ibatis.annotations.*; +import java.time.LocalDate; +import java.util.List; + @Mapper public interface EmpMapper { @@ -24,12 +27,38 @@ public interface EmpMapper { "values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})") public void insert(Emp emp); -/* -update emp set username = '11', name = '22', gender = 1, image = '1.jpg', job = 2 - , entrydate = '2008-01-01', dept_id = 1,update_time = now() where id = 21 - */ //更新员工 @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 selectById(Integer id);*/ + + + //属性名与数据表字段名不一致,处理方案二:使用mybatis的2个注解Results和Result,直接告知mybatis哪个字段的值,赋值给哪个属性 + /*@Results({ + @Result(column = "dept_id",property = "deptId"), + @Result(column = "create_time",property = "createTime"), + @Result(column = "update_time",property = "updateTime") + }) + @Select("select id, username, password, name, gender, image, job, entrydate" + + ", dept_id , create_time, update_time from emp where id = #{id};") + public Emp selectById(Integer id);*/ + + @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} order by update_time desc ;") + //注意:#{}替换为?,?不能写到''里面,会报错,可以采用${}解决,也可以使用sql函数concat来解决 +// 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} order by update_time desc ;")*/ + public List list(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 261bda0..12ffe86 100644 --- a/springboot-mybatis-crud/src/main/resources/application.properties +++ b/springboot-mybatis-crud/src/main/resources/application.properties @@ -14,4 +14,7 @@ spring.datasource.username=root spring.datasource.password=1234 #输出mybatis的日志 -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的驼峰命名自动映射开关,将a_time ---->aTime +mybatis.configuration.map-underscore-to-camel-case=true \ No newline at end of file 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..f9beef4 --- /dev/null +++ b/springboot-mybatis-crud/src/main/resources/com/inmind/mapper/EmpMapper.xml @@ -0,0 +1,11 @@ + + + + + + \ 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 3a53d6d..94dbb46 100644 --- a/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java +++ b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java @@ -8,6 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.List; @SpringBootTest class SpringbootMybatisCrudApplicationTests { @@ -56,4 +57,18 @@ class SpringbootMybatisCrudApplicationTests { emp.setUpdateTime(LocalDateTime.now()); empMapper.update(emp); } + + + @Test + void testSelectById(){ + Emp emp = empMapper.selectById(14); + 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); + } }