1.Spring-mybatis-XML方式的入门
This commit is contained in:
@@ -48,6 +48,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<!--构建工具-->
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -58,6 +59,10 @@
|
|||||||
<source>11</source>
|
<source>11</source>
|
||||||
<target>11</target>
|
<target>11</target>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
|
<!--作用:在maven编译时,保留源文件.java中的方法参数名-->
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>-parameters</arg>
|
||||||
|
</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@@ -3,6 +3,9 @@ package com.inmind.mapper;
|
|||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import org.apache.ibatis.annotations.*;
|
import org.apache.ibatis.annotations.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface EmpMapper {
|
public interface EmpMapper {
|
||||||
|
|
||||||
@@ -24,12 +27,38 @@ public interface EmpMapper {
|
|||||||
"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 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}" +
|
@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}")
|
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")
|
||||||
public void update(Emp emp);
|
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);
|
||||||
}
|
}
|
||||||
|
@@ -14,4 +14,7 @@ spring.datasource.username=root
|
|||||||
spring.datasource.password=1234
|
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的驼峰命名自动映射开关,将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.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class SpringbootMybatisCrudApplicationTests {
|
class SpringbootMybatisCrudApplicationTests {
|
||||||
@@ -56,4 +57,18 @@ class SpringbootMybatisCrudApplicationTests {
|
|||||||
emp.setUpdateTime(LocalDateTime.now());
|
emp.setUpdateTime(LocalDateTime.now());
|
||||||
empMapper.update(emp);
|
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