javaEEday09-Mybatis-新增ID回显&条件查询&XML映射文件方式
This commit is contained in:
@@ -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<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
|
||||
// public List<Emp> 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<Emp> list1(String name,Short gender,LocalDate begin,LocalDate end);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,3 +6,4 @@ spring.datasource.password= 1234
|
||||
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
|
||||
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.inmind.mapper.EmpMapper">
|
||||
<select id="list1" resultType="com.inmind.pojo.Emp"><!--注意:resultType定义的是单条记录的java封装类型-->
|
||||
select * from emp where
|
||||
name like concat('%',#{name},'%')
|
||||
and gender = #{gender}
|
||||
and entrydate between #{begin} and #{end}
|
||||
</select>
|
||||
</mapper>
|
||||
+55
@@ -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<Emp> list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testList1() {
|
||||
List<Emp> list = empMapper.list1("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user