diff --git a/springboot-mybatis-crud/pom.xml b/springboot-mybatis-crud/pom.xml index 33feccf..d4845d6 100644 --- a/springboot-mybatis-crud/pom.xml +++ b/springboot-mybatis-crud/pom.xml @@ -58,6 +58,9 @@ 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 fa02cb1..698f6db 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,9 +1,10 @@ package com.inmind.mapper; import com.inmind.pojo.Emp; -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.*; + +import java.time.LocalDate; +import java.util.List; @Mapper//生成接口的实现类对象,交给spring容器管理,DI自动注入 public interface EmpMapper { @@ -17,7 +18,44 @@ public interface EmpMapper { /* 新增员工 */ + @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 e96af6d..9d92687 100644 --- a/springboot-mybatis-crud/src/main/resources/application.properties +++ b/springboot-mybatis-crud/src/main/resources/application.properties @@ -18,3 +18,5 @@ spring.datasource.password=1234 #输出mybatis的日志 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl +#mybatis 驼峰自动映射 +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..a322d73 --- /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 58b8a51..a528221 100644 --- a/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java +++ b/springboot-mybatis-crud/src/test/java/com/inmind/SpringbootMybatisCrudApplicationTests.java @@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.List; @SpringBootTest class SpringbootMybatisCrudApplicationTests { @@ -30,7 +31,7 @@ class SpringbootMybatisCrudApplicationTests { @Test void testInsert() { Emp emp = new Emp(); - emp.setUsername("zhangsan"); + emp.setUsername("zhangsan2"); emp.setName("张三"); emp.setGender((short) 1); emp.setImage("1.jpg"); @@ -39,6 +40,42 @@ class SpringbootMybatisCrudApplicationTests { 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); } }