mybatis--新增ID回显&条件查询&XML映射文件方式
This commit is contained in:
@@ -58,6 +58,9 @@
|
|||||||
<source>11</source>
|
<source>11</source>
|
||||||
<target>11</target>
|
<target>11</target>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>-parameters</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package com.inmind.mapper;
|
package com.inmind.mapper;
|
||||||
|
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import org.apache.ibatis.annotations.Delete;
|
import org.apache.ibatis.annotations.*;
|
||||||
import org.apache.ibatis.annotations.Insert;
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper//生成接口的实现类对象,交给spring容器管理,DI自动注入
|
@Mapper//生成接口的实现类对象,交给spring容器管理,DI自动注入
|
||||||
public interface EmpMapper {
|
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)" +
|
@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});")
|
"values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime});")
|
||||||
public void insert(Emp emp);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,3 +18,5 @@ spring.datasource.password=1234
|
|||||||
|
|
||||||
#输出mybatis的日志
|
#输出mybatis的日志
|
||||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
|
#mybatis 驼峰自动映射
|
||||||
|
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>
|
||||||
@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class SpringbootMybatisCrudApplicationTests {
|
class SpringbootMybatisCrudApplicationTests {
|
||||||
@@ -30,7 +31,7 @@ class SpringbootMybatisCrudApplicationTests {
|
|||||||
@Test
|
@Test
|
||||||
void testInsert() {
|
void testInsert() {
|
||||||
Emp emp = new Emp();
|
Emp emp = new Emp();
|
||||||
emp.setUsername("zhangsan");
|
emp.setUsername("zhangsan2");
|
||||||
emp.setName("张三");
|
emp.setName("张三");
|
||||||
emp.setGender((short) 1);
|
emp.setGender((short) 1);
|
||||||
emp.setImage("1.jpg");
|
emp.setImage("1.jpg");
|
||||||
@@ -39,6 +40,42 @@ class SpringbootMybatisCrudApplicationTests {
|
|||||||
emp.setCreateTime(LocalDateTime.now());
|
emp.setCreateTime(LocalDateTime.now());
|
||||||
emp.setUpdateTime(LocalDateTime.now());
|
emp.setUpdateTime(LocalDateTime.now());
|
||||||
empMapper.insert(emp);
|
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