1 学习目标
- 重点掌握#{}占位符的使用
2 MyBatis的占位符介绍
-
#{}占位符: 相当于JDBC中的问号(?)占位符,是为SQL语句中的值进行占位,如果参数值是字符串或者日期类型,会进行转义处理
-
我们使用#{}写的SQL语句:
<update id="deleteJobs">DELETE FROM jobs WHERE job_id = #{jobId}
</update>
- 实际MyBatis执行的SQL
DELETE FROM jobs
WHERE job_id = ?
- #{}占位符用来设置参数,参数的类型能够有3种:基本类型,自定义类型,Map
- 如果只传递单个参数,只需要使用#{参数名}即可将参数取出,其中参数名可以起任意值
- 如果传递多个参数,那么#{}的名字需要互不相同,所以一般情况我们都使用传入的字段名
3 XML方式使用#{}
3.1 入门案例
- 查询指定jobId的信息
以下SQL查询代码仅供参考,可根据你本地的数据库字段进行调整,并未采用我的,如有不懂的问题可以在评论区留言,我会一一解答。
①**jobsMapper.xml
**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.JobsMapper"><select id="getJobsAll" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs</select><update id="insertJobs">INSERT INTO jobs VALUE ('6666', 'TEST', 10000, 30000)</update><update id="updateJobs">UPDATE jobs SET job_title='TEST666' WHERE job_id = '6666'</update><update id="deleteJobs">DELETE FROM jobs WHERE job_id = '6666'</update><select id="getJobsById" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_id = #{jobId}</select>
</mapper>
②**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;import java.util.List;public interface JobsMapper {public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
③**TestPlaceHolder
**
/*** 用于测试基于MyBatis的占位符的案例*/
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}
}
3.2 基本类型
3.2.1 单个参数
- 如果只传入单个参数时,#{}占位符的参数名可以起任意名
- 查询指定jobTitle的信息
①**JobsMapper.xml
**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.JobsMapper"><select id="getJobsAll" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs</select><update id="insertJobs">INSERT INTO jobs VALUE ('6666', 'TEST', 10000, 30000)</update><update id="updateJobs">UPDATE jobs SET job_title='TEST666' WHERE job_id = '6666'</update><update id="deleteJobs">DELETE FROM jobs WHERE job_id = '6666'</update><select id="getJobsById" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_id = #{jobId}</select><select id="getJobsByTitle" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_title = #{hello}</select>
</mapper>
②**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;import java.util.List;public interface JobsMapper {public Jobs getJobsByTitle(String jobTitle);public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
③**TestPlaceHolder
**
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}@Testpublic void testGetJobsByTitle() {Jobs jobs = jobsMapper.getJobsByTitle("Public Accountant");System.out.println(jobs);}
}
3.2.2 多个参数
-
MyBatis如果传入多个参数,会对参数做特殊处理,会自动将多个参数封装成一个map,#{}会根据参数名作为key,去map中取对应的值,而MyBatis在封装时,会自动以对应的属性名来匹配对应的值
-
查询指定的薪资范围内的记录
①**JobsMapper.xml
**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.JobsMapper"><select id="getJobsAll" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs</select><update id="insertJobs">INSERT INTO jobs VALUE ('6666', 'TEST', 10000, 30000)</update><update id="updateJobs">UPDATE jobs SET job_title='TEST666' WHERE job_id = '6666'</update><update id="deleteJobs">DELETE FROM jobs WHERE job_id = '6666'</update><select id="getJobsById" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_id = #{jobId}</select><select id="getJobsByTitle" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_title = #{hello}</select><select id="getJobsBySalary" resultType="cn.tedu.pojo.Jobs"><![CDATA[SELECT * FROM jobs WHERE min_salary > #{minSalary} AND max_salary < #{maxSalary}]]></select>
</mapper>
②**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;import java.util.List;public interface JobsMapper {public List<Jobs> getJobsBySalary(Double minSalary, Double maxSalary);public Jobs getJobsByTitle(String jobTitle);public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
③**TestPlaceHolder
**
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}@Testpublic void testGetJobsByTitle() {Jobs jobs = jobsMapper.getJobsByTitle("Public Accountant");System.out.println(jobs);}@Testpublic void testGetJobsBySalary() {List<Jobs> jobs = jobsMapper.getJobsBySalary(3000.0, 10000.0);for (Jobs job : jobs) {System.out.println(job);}}
}
3.3 自定义类型
-
如果在传入多个参数时,正好这些参数都是属于业务中数据模型,比如Jobs这个实体类的内容,那么可以直接把POJO作为参数即可,那此时使用#{}只需要用属性名即可取出参数
-
向jobs表中插入一条记录
①**JobsMapper.xml
**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.JobsMapper"><select id="getJobsAll" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs</select><update id="insertJobs">INSERT INTO jobs VALUE ('6666', 'TEST', 10000, 30000)</update><update id="updateJobs">UPDATE jobs SET job_title='TEST666' WHERE job_id = '6666'</update><update id="deleteJobs">DELETE FROM jobs WHERE job_id = '6666'</update><select id="getJobsById" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_id = #{jobId}</select><select id="getJobsByTitle" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_title = #{hello}</select><select id="getJobsBySalary" resultType="cn.tedu.pojo.Jobs"><![CDATA[SELECT * FROM jobs WHERE min_salary > #{minSalary} AND max_salary < #{maxSalary}]]></select><update id="addJobs">INSERT INTO jobs values (#{jobId}, #{jobTitle}, #{minSalary}, #{maxSalary})</update>
</mapper>
②**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;import java.util.List;public interface JobsMapper {public int addJobs(Jobs jobs);public List<Jobs> getJobsBySalary(Double minSalary, Double maxSalary);public Jobs getJobsByTitle(String jobTitle);public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
③**TestPlaceHolder
**
/*** 用于测试基于MyBatis的占位符的案例*/
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}@Testpublic void testGetJobsByTitle() {Jobs jobs = jobsMapper.getJobsByTitle("Public Accountant");System.out.println(jobs);}@Testpublic void testGetJobsBySalary() {List<Jobs> jobs = jobsMapper.getJobsBySalary(3000.0, 10000.0);for (Jobs job : jobs) {System.out.println(job);}}@Testpublic void testAddJobs() {Jobs job = new Jobs();job.setJobId("8888");job.setJobTitle("IT");job.setMinSalary(1000.0);job.setMaxSalary(10000.0);int rows = jobsMapper.addJobs(job);System.out.println(rows > 0 ? "新增成功!!" : "新增失败!!");}
}
3.4 Map类型
-
如果在传入多个参数时,这些参数不属于业务模型中的数据,没有对应的实体类,那么此时为了方便,也可以将这些参数封装到一个Map中
-
但是这种方式只适合用于这个Map不经常使用,临时需要传入多个数据的情况
-
修改jobs表中的一条记录
①**JobsMapper.xml
**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.mapper.JobsMapper"><select id="getJobsAll" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs</select><update id="insertJobs">INSERT INTO jobs VALUE ('6666', 'TEST', 10000, 30000)</update><update id="updateJobs">UPDATE jobs SET job_title='TEST666' WHERE job_id = '6666'</update><update id="deleteJobs">DELETE FROM jobs WHERE job_id = '6666'</update><select id="getJobsById" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_id = #{jobId}</select><select id="getJobsByTitle" resultType="cn.tedu.pojo.Jobs">SELECT job_id, job_title, min_salary, max_salary FROM jobs WHERE job_title = #{hello}</select><select id="getJobsBySalary" resultType="cn.tedu.pojo.Jobs"><![CDATA[SELECT * FROM jobs WHERE min_salary > #{minSalary} AND max_salary < #{maxSalary}]]></select><update id="addJobs">INSERT INTO jobs values (#{jobId}, #{jobTitle}, #{minSalary}, #{maxSalary})</update><update id="updateJobsById">UPDATE jobs SET min_salary = #{minSalary}, max_salary = #{maxSalary} WHERE job_id = #{jobId}</update>
</mapper>
②**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;import java.util.List;
import java.util.Map;public interface JobsMapper {public int updateJobsById(Map<String,Object> map);public int addJobs(Jobs jobs);public List<Jobs> getJobsBySalary(Double minSalary, Double maxSalary);public Jobs getJobsByTitle(String jobTitle);public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
③**TestPlaceHolder
**
package cn.tedu;import cn.tedu.mapper.JobsMapper;
import cn.tedu.pojo.Jobs;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.HashMap;
import java.util.List;/*** 用于测试基于MyBatis的占位符的案例*/
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}@Testpublic void testGetJobsByTitle() {Jobs jobs = jobsMapper.getJobsByTitle("Public Accountant");System.out.println(jobs);}@Testpublic void testGetJobsBySalary() {List<Jobs> jobs = jobsMapper.getJobsBySalary(3000.0, 10000.0);for (Jobs job : jobs) {System.out.println(job);}}@Testpublic void testAddJobs() {Jobs job = new Jobs();job.setJobId("8888");job.setJobTitle("IT");job.setMinSalary(1000.0);job.setMaxSalary(10000.0);int rows = jobsMapper.addJobs(job);System.out.println(rows > 0 ? "新增成功!!" : "新增失败!!");}@Testpublic void testUpdateJobsById() {HashMap<String, Object> map = new HashMap<>();map.put("jobId", "8888");map.put("minSalary", 3333);map.put("maxSalary", 9999);int rows = jobsMapper.updateJobsById(map);System.out.println(rows > 0 ? "修改成功!!" : "修改失败!!");}
}
4 注解方式使用#{}
4.1 入门案例: 查询指定jobId的信息
①**JobsMapper接口
**
package cn.tedu.mapper;import cn.tedu.pojo.Jobs;
import org.apache.ibatis.annotations.Select;import java.util.List;
import java.util.Map;public interface JobsMapper {@Select("SELECT * FROM jobs WHERE job_id = #{jobId}")public Jobs getJobsById02(String jobId);public int updateJobsById(Map<String,Object> map);public int addJobs(Jobs jobs);public List<Jobs> getJobsBySalary(Double minSalary, Double maxSalary);public Jobs getJobsByTitle(String jobTitle);public Jobs getJobsById(String jobId);public List<Jobs> getJobsAll();public int insertJobs();public int updateJobs();public int deleteJobs();
}
②**TestPlaceHolder
**
/*** 用于测试基于MyBatis的占位符的案例*/
@SpringBootTest
public class TestPlaceHolder {@Autowiredprivate JobsMapper jobsMapper;@Testpublic void testGetJobsById() {Jobs jobs = jobsMapper.getJobsById("AC_MGR");System.out.println(jobs);}@Testpublic void testGetJobsByTitle() {Jobs jobs = jobsMapper.getJobsByTitle("Public Accountant");System.out.println(jobs);}@Testpublic void testGetJobsBySalary() {List<Jobs> jobs = jobsMapper.getJobsBySalary(3000.0, 10000.0);for (Jobs job : jobs) {System.out.println(job);}}@Testpublic void testAddJobs() {Jobs job = new Jobs();job.setJobId("8888");job.setJobTitle("IT");job.setMinSalary(1000.0);job.setMaxSalary(10000.0);int rows = jobsMapper.addJobs(job);System.out.println(rows > 0 ? "新增成功!!" : "新增失败!!");}@Testpublic void testUpdateJobsById() {HashMap<String, Object> map = new HashMap<>();map.put("jobId", "8888");map.put("minSalary", 3333);map.put("maxSalary", 9999);int rows = jobsMapper.updateJobsById(map);System.out.println(rows > 0 ? "修改成功!!" : "修改失败!!");}@Testpublic void testGetJobsById02() {Jobs jobs = jobsMapper.getJobsById02("AC_MGR");System.out.println(jobs);}
}
5 日志配置
5.1 什么是日志
- 日志的作用是用来追踪和记录我们的程序运行中的信息,我们可以利用日志很快定位问题,追踪分析。
- 如果没有日志,程序一旦出现问题,很难一下子就能定位问题。尤其是访问第三方接口、随机或偶尔出现的问题、很难再现的问题。
5.2 日志级别
SpringBoot的日志框架提供了如下几种日志级别(从高到低):
-
ERROR:错误级别,指出发生错误事件,需要立即处理;
-
WARN:警告级别,表明可能发生错误的情况,需要注意和防止;
-
INFO:信息级别,描述应用程序的正常运行信息,但不会打印太多的信息;(SpringBoot默认级别)
-
DEBUG:调试级别,用于调试应用程序,打印详细的调试信息;
-
TRACE:跟踪级别,记录应用程序中每个步骤的详细信息。
5.3 设置日志级别
-
为了方便查看打印的SQL日志,所以可以为我们项目添加日志配置,只需要在application.yml中添加如下内容:
注意: cn.tedu为我们项目的根包
#日志设置
logging:level:cn:tedu: debug
上一篇文章:MyBatis的详细大全,结合项目(Day35)-CSDN博客https://blog.csdn.net/Z0412_J0103/article/details/142969437下一篇文章: