mybatis - XxxMapper.java接口中方法的参数 和 返回值类型,怎样在 XxxMapper.xml 中配置的问题

news/2024/10/11 13:21:45/

这个例子中的mybatis-config.xml文件,引用这个文件即可

javacomatguigupojoEmployeejava_2">实体类src/main/java/com.atguigu.pojo/Employee.java

java">package com.atguigu.pojo;public class Employee {private Integer id;private String name;private String plone;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPlone() {return plone;}public void setPlone(String plone) {this.plone = plone;}@Overridepublic String toString() {return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", plone='" + plone + '\'' + '}';}
}

javacomatguigumapperEmployeeMapperjava_47">实体类对应的接口文件src/main/java/com.atguigu.mapper/EmployeeMapper.java

java">package com.atguigu.mapper;import com.atguigu.pojo.Employee;
import org.apache.ibatis.annotations.Param;import java.util.List;
import java.util.Map;public interface EmployeeMapper {根据id查看员工信息Employee queryById(Integer id);根据id删除员工信息(单个简单类型作为参数)int deleteById(Integer id);根据 plone 查询员工的信息(单个简单类型作为参数)List<Employee> queryByPlone(String plone);插入员工数据(单个实体对象作为参数)int insertEmp(Employee employee);传入多个简单类型的参数List<Employee> queryByNameAndPlone(@Param("xname") String name, @Param("xplone") String plone);传入 map类型的参数int insertEmpMap(Map data);-----------------------------------------------------------------------------查询工资高于传入值的员工姓名们List<String> queryNamesBySalary(@Param("salary") Double salary);返回集合类型,查询全部员工信息List<Employee> queryAll();返回map类型,查询城市的最高工资和平均工资Map<String, Object> selectEmpNameAndMaxSalary();自增长主键回显,自动提交事务int insertUser(Employee employee);-----------------------------------------------------------------------------mybatis自己维护非自增主键int insertEmployee(Employee employee);Employee queryByIdd(String tId);
}

javaxml_srcmainresourcesmappersEmployeeMapperxml_103">EmployeeMapper.java接口文件对应的xml文件 src/main/resources/mappers/EmployeeMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTO Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">namespace = 接口的权限定符
<mapper namespace="com.atguigu.mapper.EmployeeMapper"><select id="queryById" resultType="com.atguigu.pojo.Employee">select id, name, plone from my_user where id = #{id}</select>场景1:传入参数,是单个简单类型,key的值随便写,一般推荐使用参数名<delete id="deleteById"><!-- key的值可以这样写:delete from my_user where id = #{ergouzi} --><!-- 推荐还是使用参数名,如下 -->delete from my_user where id = #{id}</delete><select id="queryByPlone" resultType="com.atguigu.pojo.Employee">select id, name, plone from my_user where plone = #{plone}</select>场景2:传入的参数,是单个实体对象,key的值怎么写呢? key = (实体类的每个)属性名,即可<insert id="insertEmp">insert into my_user (name, plone) values (#{name},#{plone});</insert>场景3:传入多个简单类型的参数,key的值怎么写呢?* 方案1:使用@param注解(推荐使用注解)* 方案2:mybatis的默认机制,如下:形参arg0, arg1 ... , argn 从左到右依次对应的key值是 arg0, arg1 ... , argn<select id="queryByNameAndPlone" resultType="com.atguigu.pojo.Employee">select id, name, plone from my_user where plone = #{xplone} and name = #{xname}</select>场景4:传入的是 map类型 的参数,key的值怎么写呢? key = map的key,即可<insert id="insertEmpMap">insert into my_user (name, plone) values (#{name},#{plone});</insert>---------------------------------------------------------------------------------------------------当,返回值是,集合类型,时,resultType 怎样指定? 这里就用到了MyBatis中的别名切记:返回值是集合时,resultType不需要指定集合类型,只需要指定泛型即可,为什么呢?因为 mybatis 底层是 ibatis,虽然,selectOne是查询单个,selectList是查询集合,但是,selectOne 其实也是调用的 selectList,只不过, selectOne的时候,取的是selectList里面的第一个值而已所以,返回值是集合时,resultType不需要指定集合类型,只需要指定泛型即可。<select id="queryNamesBySalary" resultType="string">select name from my_user where salary > #{salary}</select>因为,在mybatis-config.xml文件种配置了别名,下面这一行<typeAliases> <package name="com.atguigu.pojo"/> </typeAliases>所以,这里可以 resultType="employee",而不用 resultType="com.atguigu.pojo.Employee" 了<select id="queryAll" resultType="employee">select * from my_user</select>什么时候,返回map?当没有实体类可以使用接值的时候,可以使用map接受数据,map的key,对应,查询的列名map的value,对应,查询的值<select id="selectEmpNameAndMaxSalary" resultType="map">SELECT`name` 姓名,salary 工资,(SELECT AVG(salary) FROM my_user) 城市平均工资FROMmy_userWHEREsalary = (SELECT MAX(salary) FROM my_user)</select>自增长主键回显,自动提交事务获取自增长的主键,可以使用:useGeneratedKeys="true",代表,我们想要获取数据库自增的主键idkeyColumn="id",代表,数据库表的主键列的值keyProperty="id",代表,接收主键列值的实体类的属性<insert id="insertUser" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into my_user (name, plone) values(#{name}, #{plone});</insert>---------------------------------------------------------------------------------------------------mybatis自己维护非自增主键<insert id="insertEmployee">让mybatis帮我们维护非自增的主键,我们就不在MybatisTest.java中编写相关的代码了order="BEFORE" 或者 "AFTER",意思是,在sql语句之前执行,还是之后执行resultType,表示,返回值类型keyProperty,表示,查询的结果,给实体类中的哪个属性这个 selectkey 就相当于 MybatisTest.java文件中的,下面这几行代码自己维护主键,使用uuid,这里replaceAll操作,是去掉uuid的中划线String id = UUID.randomUUID().toString().replaceAll("-", "");employee.settId(id);<selectKey order="BEFORE" resultType="string" keyProperty="tId">SELECT REPLACE(UUID(),'-','')</selectKey>insert into my_user (t_id, t_name) values(#{tId}, #{tName})</insert>列名 和 属性名 不一致如何解决?方案1:别名:select t_id tId, t_name tName from teacher where t_id = #{tId}方案2:开启mybatis的驼峰式映射配置: <setting name="mapUnderscoreToCamelCase" value="true"/>会自动把 t_id 映射成 tId方案3:使用resultMap自定义映射,注意:resultType 和 resultMap 二选一!resultType按照规则自动映射,按照是否开启驼峰式映射。自己映射属性和列名,只能映射一层结构resultMap可以深层次的映射哦!!!例如:声明 resultMap标签,定义自己的映射规则id:是某个resultMap的唯一标识,用于在<select id="queryById" resultMap="id">中,确定这个select使用哪个resultMapresult:普通列的映射关系<resultMap id="tMap" type="teacher"><id column="t_id" property="tId" /><result column="t_name" property="tName" /></resultMap><select id="queryByIdd" resultMap="tMap">select * from teacher where t_id = #{tId}</select></mapper>

javacomatguiguMybatisTestjava_259">测试文件src/test/java/com.atguigu.MybatisTest.java

java">
package com.atguigu.test;import com.atguigu.mapper.EmployeeMapper;
import com.atguigu.pojo.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;import java.io.IOException;
import java.io.InputStream;public class MybatisTest {@Testpublic void test_01() throws IOException {1、读取外部配置文件(mybatis-config.xml)使用MyBatis提供的Resources类,读取名为mybatis-config.xml的配置文件。InputStream ips = Resources.getResourceAsStream("mybatis-config.xml");2、创建sqlSessionFactory使用SqlSessionFactoryBuilder根据配置文件构建SqlSessionFactory对象。SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);3、根据sqlSessionFactory创建sqlSession对象【自动开启JDBC】【自动开启事务,但不会自动提交事务,需要sqlSession.commit()手动开启】或者使用 sqlSessionFactory.openSession(true)【会自动开启事务,自动提交事务,不需要写sqlSession.commit()SqlSession sqlSession = sqlSessionFactory.openSession();4、使用SqlSessiongetMapper()方法,获取EmployeeMapper接口的代理对象,并且调用具体的SQL方法EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);调用EmployeeMapper接口的queryById()方法,传入参数1,执行查询操作,并将结果赋值给employee对象。Employee employee = mapper.queryById(1);System.out.println(employee);// 因为我们在 EmployeeMapper.xml文件中使用了 selectkey 让mybatis帮我们维护了主键,所以这里可以注掉了// 自己维护主键,使用uuid,这里replaceAll操作,是去掉uuid的中划线// String id = UUID.randomUUID().toString().replaceAll("-", "");// employee.settId(id);5、提交事务(非DQL)和释放资源sqlSession.commit():提交事务,但是在这个查询操作的上下文中,commit是不必要的,因为查询操作不需要提交事务。通常,只有DML(如INSERTUPDATEDELETE)操作才需要提交事务。sqlSession.commit();sqlSession.close();}
}

http://www.ppmy.cn/news/1446038.html

相关文章

从递归角度串联二叉树-图论-动态规划

一、深度理解二叉树的前中后序遍历 二叉树遍历框架如下&#xff1a; void traverse(TreeNode* root) {if (root nullptr) {return;}// 前序位置traverse(root->left);// 中序位置traverse(root->right);// 后序位置 }先不管所谓前中后序&#xff0c;单看 traverse 函数…

探索Midjourney的艺术地图:常用画质关键词导航

在这个由人工智能驱动的创意世界中&#xff0c;画质的控制成为了每一位创作者追求作品完美的关键。Midjourney提供了一系列的关键词&#xff0c;让我们能够细致地调整我们心中所想象的场景与人物。从质感、明暗到风格&#xff0c;这些关键词就像是调色盘上的色彩&#xff0c;每…

前端打包过大如何解决?

前端开发完毕部署到线上是&#xff0c;执行npm run build。当打包过大时&#xff0c;部署到服务端后加载缓慢&#xff0c;如何优化&#xff1f; 我们可以通过执行npm run analyze。可以看到各个包文件大小的区别。 当打包过大时&#xff0c;通过压缩gzip的方式&#xff0c;可以…

记录一些容易遗忘的东西

文章目录 native、sync 修饰符this.$nextTick native、sync 修饰符 native &#xff1a;在对子组件使用 click 的时候若不使用该修饰符&#xff0c;那么就不能执行点击事件&#xff0c;会被判断为子向父组件传的值sync &#xff1a; 类似于 v-model 的响应式修饰符&#xff0c…

Java同时使用@RequestBody和@RequestParam传参在postman中执行请求报错:Unsupported Media Type

天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物。 每个人都有惰性&#xff0c;但不断学习是好好生活的根本&#xff0c;共勉&#xff01; 文章均为学习整理笔记&#xff0c;分享记录为主&#xff0c;如有错误请指正&#xff0c;共同学习进步。…

Nginx 搭建Web服务

题目&#xff1a; 1.web服务器的主机ip&#xff1a;192.168.78.128 2.web服务器的默认访问目录为/var/www/html 默认发布内容为default‘s page 3.站点news.timinglee.org默认发布目录 为/var/www/virtual/timinglee.org/news 默认发布内容为 news.timinglee.org 4.站点login.t…

Llama images - 记录我看到的那些羊驼

来自 &#xff1a; DREAM: Distributed RAG Experimentation Framework

基于Java-jsp的现代数字化城市公交查询系统论文(四)

⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️ ➡️点击免费下载全套资料:源码、数据库、部署教程、论文、答辩ppt一条龙服务 ➡️有部署问题可私信联系 ⬆️⬆️⬆️​​​​​​​⬆️…