1.Spring-mybatis-XML方式的入门
This commit is contained in:
@@ -48,6 +48,7 @@
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<!--构建工具-->
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -58,6 +59,10 @@
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<!--作用:在maven编译时,保留源文件.java中的方法参数名-->
|
||||
<compilerArgs>
|
||||
<arg>-parameters</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
@@ -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<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} order by update_time desc ;")*/
|
||||
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
|
||||
}
|
||||
|
@@ -15,3 +15,6 @@ spring.datasource.password=1234
|
||||
|
||||
#输出mybatis的日志
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
#开启mybatis的驼峰命名自动映射开关,将a_time ---->aTime
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
@@ -0,0 +1,11 @@
|
||||
<?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="list" resultType="com.inmind.pojo.Emp">
|
||||
select * from emp where name like concat('%',#{name},'%')
|
||||
and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc
|
||||
</select>
|
||||
</mapper>
|
@@ -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<Emp> list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user