-
2022-04-13 11:04:18
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xxx.mapper.XXXMapper"> </mapper>
更多相关内容 -
Mybatis框架 mapper.xml文件中parameterType传递参数常用的几种方式.pdf
2020-10-18 08:13:48本文详细介绍了在Mybatis框架 mapper.xml文件中parameterType传递参数常用的几种方式,以及如何实现的案列,同时#和$传参的区别。 -
idea工具中直接从mapper.Java文件中跳转到mapper.xml文件的插件,挺不错的
2018-08-21 15:20:21idea工具中直接从mapper.Java文件中跳转到mapper.xml文件的插件,挺不错的 -
GeneratorMapper.xml
2020-05-20 21:06:11-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 --> <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类... -
springboot mybatis mapper.xml 配置
2017-06-22 15:13:21springboot mybatis mapper.xml 配置,里面包含了新增,修改,删除,查询,分页查询例子以及通过 网页访问的例子 -
mybatis联表查询mapper.xml
2018-03-09 14:11:19主表和副表一对多通过主表mapper封装直接返回需要数据,里面有主表xml、主表实体类、副表实体类、dao层 -
用java程序生成mybatis的mapper.xml和mapper.java文件
2015-08-19 16:07:42只要在JAVA开发环境下都能逆向生成mapper.xml和mapper.java文件,不论开发工具 -
3.Mapper.xml 详解
2021-09-02 13:21:14文章目录Mapper.xml 详解1. parameterType2. resultType3. 级联查询3.1 一对多3.2 多对多 Mapper.xml 详解 MyBatis 主要有两个的配置文件:config.xml 和 Mapper.xml,这两个配置文件可以自定义文件名。 config.xml...Mapper.xml 详解
MyBatis 主要有两个的配置文件:config.xml 和 Mapper.xml,这两个配置文件可以自定义文件名。
- config.xml 是全局配置文件,主要配置 MyBatis 的数据源(DataSource),事务管理(TransactionManager)以及打印 SQL 语句,开启二级缓存,注册 Mapper.xml 等。
- Mapper.xml 的作用是什么?因为 MyBatis 是“半自动”的ORM框架,即SQL语句需要开发者自定义,MyBatis 的关注点在 POJO 与 SQL 之间的映射关系,所以 Mapper.xml 主要配置接口方法对应的 SQL 语句,即接口方法的具体实现。
准备测试环境如下:
实体类 User:
package com.training.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //使用这个插件后,会默认生成属性的Getter与Setter方法 @AllArgsConstructor @NoArgsConstructor @Data public class User { private Integer id; private String name; private Double score; }
自定义 UserMapper 接口
package com.training.mapper; public interface UserMapper { //定义相关的业务方法 //... }
创建接口对应的 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.training.mapper.UserMapper"> <!--定义接口方法对应的 SQL 语句--> </mapper>
测试类 Test
package com.training.test; import com.training.entity.User; import com.training.mapper.UserMapper; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; import java.util.List; public class Test { public static void main(String[] args) { //加载MyBatis配置信息 InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml"); //构建SQLSessionFactoryBuilder SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream); //获取SqlSession SqlSession sqlSession = factory.openSession(); //获取实现接口的代理对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); //... } }
1. parameterType
parameterType 设置参数的数据类型,支持基本数据类型、包装类、String、多个参数以及 Java 对象。
1.1 基本数据类型
public interface UserMapper { //通过id查询User public User findById(int id); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findById" parameterType="int" resultType="com.training.entity.User"> select * from user where id=#{id}; </select> </mapper>
注:
#{id}
可以替换任意名称,因为只有一个参数,所以无论映射名是什么,都可以完成映射。1.2 包装类
public interface UserMapper { //通过score查询User public User findByScore(Double score); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findByScore" parameterType="java.lang.Double" resultType="com.training.entity.User"> select * from user where score=#{score} </select> </mapper>
1.3 字符串 String
public interface UserMapper { //通过name查询User public User findByName(String name); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findByName" parameterType="String" resultType="com.training.entity.User"> select * from user where name=#{name} </select> </mapper>
1.4 多个参数
多个参数的情况下,无法通过映射名来进行映射,而应该采用下标进行映射,即:
[arg0, arg1, arg2, ……]
或者[param1, param2, param3, ……]
。public interface UserMapper { //通过id和name查询User public User findByIdAndName(Integer id, String name); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findByIdAndName" resultType="com.training.entity.User"> <!--select * from user where id=#{arg0} and name=#{arg1};--> select * from user where id=#{param1} and name=#{param2}; </select> </mapper>
1.5 JavaBean
参数列表中的映射名就是实体类的属性名。
public interface UserMapper { //根据User封装对象查找User public User findByUser(User user); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findByUser" parameterType="com.training.entity.User" resultType="com.training.entity.User"> select * from user where name=#{name} and score=#{score}; </select> </mapper>
2. resultType
resultType 设置返回值的数据类型,支持基本数据类型、包装类、String、多个参数以及 Java 对象。
2.1 基本数据类型
public interface UserMapper { //查询User的总数 public int getCount(); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="getCount" resultType="int"> select count(*) from user; </select> </mapper>
2.2 包装类
public interface UserMapper { //查询User的总数 public Integer getCount(); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="getCount" resultType="java.lang.Integer"> select count(*) from user; </select> </mapper>
2.3 字符串 String
public interface UserMapper { //根据id查询name public String findNameById(Integer id); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findNameById" parameterType="int" resultType="java.lang.String"> select name from user where id=#{id}; </select> </mapper>
2.4 JavaBean
public interface UserMapper { //通过id查询User public User findById(Integer id); }
<mapper namespace="com.training.mapper.UserMapper"> <select id="findById" parameterType="java.lang.Integer" resultType="com.training.entity.User"> select * from user where id=#{id}; </select> </mapper>
3. 级联查询
上面介绍的查询都是基于 User 的单表查询,如果是多表关联查询,比如:Student 与 Class 一对多的关联查询、Student 与 Course 多对多的级联查询,应该如何处理呢?显然仅仅使用 resultType 无法完成了。
3.1 一对多
创建数据表
create table class ( id int auto_increment primary key, name varchar(22) null ); create table student ( id int auto_increment primary key, name varchar(25) not null comment '姓名', GPA double default 0 null comment '分数', cid int null );
数据表对应的实体类
package com.training.entity; import lombok.Data; @Data public class Student { private Integer id; private String name; private Double GPA; private Integer cid; private Class aClass; }
package com.training.entity; import lombok.Data; @Data public class Class { private Integer id; private String name; private List<Student> students; }
例1:根据学生 id 查询学生所在的班级信息
自定义接口 StudentMapper.java
package com.training.mapper; import com.training.entity.Student; public interface StudentMapper { //查询学生的班级信息 public Student findById(Integer id); }
SQL配置文件 StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.training.mapper.StudentMapper"> <resultMap id="studentMap" type="com.training.entity.Student"> <id column="sid" property="id"></id> <result column="sname" property="name"></result> <result column="GPA" property="GPA"></result> <association property="aClass" javaType="com.training.entity.Class"> <id column="cid" property="id"></id> <result column="cname" property="name"></result> </association> </resultMap> <select id="findById" parameterType="java.lang.Integer" resultMap="studentMap"> select s.id sid, s.name sname, GPA, c.id cid, c.name cname from student s,class c where s.cid = c.id and s.id = 1; </select> </mapper>
例2:根据班级 id 查询所在班级的学生
自定义接口 ClassMapper.java
package com.training.mapper; import com.training.entity.Class; public interface ClassMapper { public Class findById(Integer id); }
SQL配置文件 StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.training.mapper.StudentMapper"> <resultMap id="studentMap" type="com.training.entity.Student"> <id column="sid" property="id"></id> <result column="sname" property="name"></result> <result column="GPA" property="GPA"></result> <association property="aClass" javaType="com.training.entity.Class"> <id column="cid" property="id"></id> <result column="cname" property="name"></result> </association> </resultMap> <select id="findById" parameterType="java.lang.Integer" resultMap="studentMap"> select s.id sid, s.name sname, GPA, c.id cid, c.name cname from student s,class c where s.cid = c.id and s.id = 1; </select> </mapper>
3.2 多对多
创建数据表
create table student ( id int auto_increment primary key, name varchar(25) not null comment '姓名', GPA double default 0 null comment '分数' ); create table stu_course ( id int auto_increment primary key, sid int null, cid int null ); create table course ( id int auto_increment primary key, name varchar(225) null );
数据表对应的实体类
package com.training.entity; import lombok.Data; import java.util.List; @Data public class Student { private Integer id; private String name; private Double GPA; private List<Course> courses; }
package com.training.entity; import lombok.Data; import java.util.List; @Data public class Course { private Integer id; private String name; private List<Student> students; }
例1:通过学生 id 查询该学生的选课信息
自定义接口 StudentMapper.java
public interface StudentMapper { public Student findById(Integer id); }
SQL配置文件 StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.training.mapper.StudentMapper"> <resultMap id="studentMap" type="com.training.entity.Student"> <id column="sid" property="id"></id> <result column="sname" property="name"></result> <result column="GPA" property="GPA"></result> <collection property="courses" ofType="com.training.entity.Course"> <id column="cid" property="id"></id> <result column="cname" property="name"></result> </collection> </resultMap> <select id="findById" parameterType="java.lang.Integer" resultMap="studentMap"> select s.id sid, s.name sname, GPA, c.id cid, c.name cname from student s,stu_course sc,course c where s.id = sc.sid and c.id = sc.cid and s.id = 1; </select> </mapper>
控制台打印结果如下:
例2:通过课程 id 查询选过该课的学生信息
自定义接口 CourseMapper.java
public interface ClassMapper { public Class findById(Integer id); }
SQL配置文件 CourseMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.training.mapper.CourseMapper"> <resultMap id="courseMap" type="com.training.entity.Course"> <id column="cid" property="id"></id> <result column="cname" property="name"></result> <collection property="students" ofType="com.training.entity.Student"> <id column="sid" property="id"></id> <result column="sname" property="name"></result> <result column="GPA" property="GPA"></result> </collection> </resultMap> <select id="findById" parameterType="java.lang.Integer" resultMap="courseMap"> select c.id cid, c.name cname, s.id sid, s.name sname, GPA from student s,stu_course sc,course c where s.id = sc.sid and c.id = sc.cid and c.id = 1; </select> </mapper>
控制台打印结果如下:
-
mybatis自动生成mapper.xml文件
2018-04-13 16:22:40springboot整合mybatis,自动生成mapper.xml文件以及dao和model -
【Spring Boot+MyBatis】配置generatorMapper.xml自动生成实体类、Mapper和xml文件
2021-12-03 20:07:16前提:先在Navicat中建立数据库及表,并在...4. 在src/main/resources目录下创建generatorMapper.xml文件 5. 在pom.xml中添加mybatis代码自动生成插件 6. 配置好maven插件后,重新加载maven 7. 单击mybati...前提:先在Navicat中建立数据库及表,并在IntelliJ IDEA中连接数据库。
目录
4. 在src/main/resources目录下创建generatorMapper.xml文件
7. 单击mybatis-generator:generate,生成实体类、Mapper和*Mapper.xml文件
1. 在Navicat中建立数据库及表(数据库表结构如下)
/* Navicat MySQL Data Transfer Source Server : community Source Server Version : 50720 Source Host : localhost:3306 Source Database : community Target Server Type : MYSQL Target Server Version : 50720 File Encoding : 65001 Date: 2021-12-03 21:07:47 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for comment -- ---------------------------- DROP TABLE IF EXISTS `comment`; CREATE TABLE `comment` ( `id` int(11) NOT NULL, `user_id` int(20) DEFAULT NULL COMMENT '评论的用户id,创建索引', `entity_id` int(20) DEFAULT NULL COMMENT '评论实体id,创建索引', `entity_type` int(50) DEFAULT NULL COMMENT '评论实体类型:1 帖子评论,2 评论回复', `target_id` int(50) DEFAULT NULL COMMENT '评论目标id', `content` text COMMENT '评论内容', `status` int(50) DEFAULT NULL COMMENT '评论状态:0 有效,1 无效', `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of comment -- ---------------------------- -- ---------------------------- -- Table structure for discuss_post -- ---------------------------- DROP TABLE IF EXISTS `discuss_post`; CREATE TABLE `discuss_post` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(45) NOT NULL COMMENT '用户id,关联user', `title` varchar(100) DEFAULT NULL COMMENT '帖子的标题', `content` text COMMENT '帖子的内容', `type` int(11) DEFAULT '0' COMMENT '0 普通,1置顶', `status` int(11) DEFAULT '0' COMMENT '帖子的状态,0 正常、1精华、2拉黑', `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '帖子创建时间', `comment_count` int(11) DEFAULT NULL COMMENT '帖子评论数量', `score` double DEFAULT NULL COMMENT '帖子的分数,用于后续帖子的排名', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of discuss_post -- ---------------------------- INSERT INTO `discuss_post` VALUES ('1', '1', '帖子标题', '帖子内容', '0', '0', '2021-12-03 17:45:20', '2', '5'); INSERT INTO `discuss_post` VALUES ('2', '1', '面试', '百度面试内容', '0', '0', '2021-12-03 17:45:22', '0', '0'); INSERT INTO `discuss_post` VALUES ('3', '1', '实习', '百度实习内容', '0', '0', '2021-12-03 17:45:24', '0', '0'); -- ---------------------------- -- Table structure for login_ticket -- ---------------------------- DROP TABLE IF EXISTS `login_ticket`; CREATE TABLE `login_ticket` ( `id` int(11) NOT NULL, `user_id` int(20) DEFAULT NULL COMMENT '登录用户id', `ticket` varchar(100) DEFAULT NULL COMMENT '登录凭证,随机字符串', `status` int(50) DEFAULT NULL COMMENT '登录状态:0 有效,1 无效', `expired` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '过期时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of login_ticket -- ---------------------------- -- ---------------------------- -- Table structure for message -- ---------------------------- DROP TABLE IF EXISTS `message`; CREATE TABLE `message` ( `id` int(11) NOT NULL, `from_id` int(50) DEFAULT NULL COMMENT '发消息的id,创建索引', `to_id` int(50) DEFAULT NULL COMMENT '收消息的id,创建索引', `conversation_id` int(50) DEFAULT NULL COMMENT '会话id,由通信双方id拼接,创建索引', `content` text COMMENT '消息内容', `status` int(50) DEFAULT NULL COMMENT '消息状态:0 未读,1 已读,2 删除', `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '消息发送时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of message -- ---------------------------- -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL COMMENT '用户id', `user_name` varchar(50) DEFAULT NULL COMMENT '用户名,创建索引', `password` varchar(100) DEFAULT NULL COMMENT '用户密码', `head_url` varchar(100) DEFAULT NULL COMMENT '用户头像地址', `salt` varchar(50) DEFAULT NULL COMMENT '盐', `email` varchar(50) DEFAULT NULL COMMENT '用户邮箱,创建索引', `type` int(50) DEFAULT '0' COMMENT '用户类型:0 普通,1 管理员, 2 版主', `status` int(50) DEFAULT '0' COMMENT '用户状态:0 未激活,1 已激活', `activation_code` varchar(50) DEFAULT NULL COMMENT '激活码', `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '注册时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', '小明', '123', 'https://img.ivsky.com/img/tupian/li/202105/24/qiaokeliquqi-011.jpg', 'xxx', 'zqq@qq.com', '1', '1', 'xxxxxx', '2021-12-03 17:45:56'); INSERT INTO `user` VALUES ('2', '小张', '123', 'https://img.ivsky.com/img/tupian/li/202105/24/qiaokeliquqi-011.jpg', 'xxx', 'zqq@qq.com', '1', '1', 'xxxx', '2021-12-03 17:46:13');
2. 在IntelliJ IDEA中连接本地数据库(注意:根据提示设置serverTimezone)
3. 在pom.xml中添加mysql、mybatis依赖
<!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.25</version> </dependency> <!-- springboot集成mybatis依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
4. 在src/main/resources目录下创建generatorMapper.xml文件(注意:如果generatorMapper.xml没有放在pom.xml同目录下,请指定generatorMapper.xml配置文件的路径)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 --> <classPathEntry location="D:\workspace-git\mysql-connector-java-5.1.8.jar"/> <context id="default" targetRuntime="MyBatis3"> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- JavaBean 实现 序列化 接口 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!-- genenat entity时,生成toString --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <commentGenerator> <property name="suppressAllComments" value="false"/> <!--生成的注释包含时间戳--> <property name="suppressDate" value="true"/> </commentGenerator> <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/community?characterEncoding=utf-8&useSSL=false&useInformationSchema=true" userId="root" password="root"> <property name="useInformationSchema" value="false"/> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成Entity类存放位置--> <javaModelGenerator targetPackage="zqq.trys.community.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao类存放位置(Mapper)--> <javaClientGenerator type="XMLMAPPER" targetPackage="zqq.trys.community.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--生成对应表及类名--> <!-- schema 数据库名 tableName 表名 domainObjectName 实体类名 方法一般不生成,设为false--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="comment" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="discuss_post" domainObjectName="DiscussPost" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="login_ticket" domainObjectName="LoginTicket" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
5. 在pom.xml中添加mybatis代码自动生成插件
<!-- mybatis代码自动生成插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--配置文件的位置 --> <configurationFile>src/main/resources/generatorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin>
6. 配置好maven插件后,重新加载maven(红框中选择其一),Plugins中就会出现mybatis-generator插件
7. 单击mybatis-generator:generate,便会生成实体类、Mapper和*Mapper.xml文件(不用提前创建好包名,会自动生成)
-
idea如何新建mybatis mapper.xml文件
2022-04-06 10:03:09如题 ...new——> XML Configure File,但是新建的文件都不是...点击 + 号,新建模板,输入易识别的名字,然后后缀是 xml,在下方框里输入mybatis-mapper.xml的通用模板内容,然后点击应用。 模板如下: <?xml ve如题
试图点击file——>new——> XML Configure File,但是新建的文件都不是自己想要的,记得上一次直接从别处直接找来的xxxMapper.xml文件内容复制粘贴再修改的。这次又遇到了,索性记录一下正儿八经的步骤。
1. 新建一个模板
点击 File——> Settings,然后如下图:
点击 + 号,新建模板,输入易识别的名字,然后后缀是 xml,在下方框里输入mybatis-mapper.xml的通用模板内容,然后点击应用。 模板如下:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace=""> <!--namespace根据自己需要创建的的mapper的路径和名称填写--> </mapper>
2. 新建mapper.xml
OK,大功告成~
-
mybatis-plus的mapper.xml配置位置的坑——解决找不到mapper.xml的问题
2022-01-24 21:43:39mybatis-plus的mapper.xml文件放置在src/main/java路径下导致运行找不到Mapper的问题解决 -
mybatis中的Mapper.xml标签具体分析
2021-01-29 20:20:37哎呀,父老乡亲们,好久不见啊,最近在赶新项目,好久没给大家做分享了,今天闲下来了,整理下最近写的代码,总结总结,然后今天准备给大家出两篇文章,一篇讲关于Mybatis.xml中的标签分析和接下来将要分享的在代码... -
快速实现mapper.java与mapper.xml之间互相切换【idea插件】
2022-03-25 11:58:25轻松轻松实现mapper.java与mapper.xml之间互切,你必须值得拥有! -
mybatis之mapper.xml
2021-03-16 10:50:08一、mapper.xml使用 1、<where>标签 如果该标签包含的元素中有返回值,就插入一个where,如果后面的字符串是以and和 or开头,就将他们剔除。 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE... -
【Mybatis Plus】之 mapper.xml
2021-09-10 16:46:59一、Mapper.java 和 Mapper.xml 映射关系 二、Mapper.xml 语句标签介绍 1、Select 标签 一、Mapper.java 和 Mapper.xml 映射关系 Mybatis 为我们提供了基本的增删改查的接口,特别是 Mybatis-Plus 提供的 ... -
mapper.xml 无法解析字段
2022-04-06 20:54:34今天写mapper.xml遇到一个问题,语句里的字段都爆红,无法解析,也找不到数据源 网上找了很多方法 找到 File->Settings->Languages&Frameworks->SQL Resolution Scopes 左边选择你的mapper包路径,... -
springboot如何与mybatis中mapper.xml交互以操作数据库
2022-03-11 10:18:25XML 可扩展标记语言,标准通用标记语言的子集,简称XML。是一种用于标记电子文件使其具有结构性的标记语言。...MyBatis中mapper.xml中的常用元素 1.SQL映射文件常用元素: select、insert、u... -
【Java】——MyBatis 中mapper.xml的语法
2022-01-29 18:47:01就在Mapper.xml中配置。当然了该配置文件可以自定义文件名。文件的样式如下: <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mappe.... -
MyBatis-Plus找不到Mapper.xml文件的解决方法
2022-03-30 15:17:44在整合SpringBoot和Mybatis-plus时,想写自定义的sql,所以创建了Mapper.xml文件,但是启动后却老是报错: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 很明显,是Mapper.xml... -
springboot配置自定义mapper.xml文件
2021-03-23 12:07:281.声明mapper接口package com.hundsun.one.mapper;@Repositorypublic interface ResultUserRoleMapper extends BaseMapper {/*** 分页查询* @param page* @return*/Page pageUsers(@Param("page...创建mapper配置文... -
Mapper.xml模板
2020-07-23 09:56:24xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace=""> <insert id=... -
Mapper.xml中常用的标签
2022-04-08 14:56:261、mapper mapper中用于主要编辑SQL语句的。 使用方法: 2、select查询标签 select中用于编辑有关查询的SQL, 使用方法: 3、insert添加标签 insert编辑添加SQL; 使用方法: 4、delete删除标签 ... -
Mapper.xml文件自动提示数据库字段
2022-05-05 09:50:39mapper.xml的文件是这样显示的 第一步:设置sql方言 setting -> sql dialects -> global dialects(设置为Mysql) 点击应用 第二步:设置sql解析范围(就是sql dialects的下面的一个)配置到你自己关联的数据库;... -
mapper.xml文件映射mapper接口的四种方式
2022-03-07 11:55:44mapper.xml文件映射mapper接口的四种方式 <mappers> //1、resource,文件的位置 <mapper resource = "mapper/UserMapper.xml"/> </mapper> //2、绝对路径,一般不用,太长 <mapper url = ... -
详解mybatis-plus的 mapper.xml 路径配置的坑
2021-03-11 10:14:20mybatis-plus今天遇到一个问题,就是mybatis 没有读取到mapper.xml 文件。特此记录一下,问题如下:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ... -
如何修改idea的mapper.xml中sql关键字的颜色
2021-08-23 11:12:15我的idea设置了主题,然后导致mapper.xml中的关键字(select之类的)变成了白色,改不了,有人知道怎么修改吗; 感觉主要是设置了主题,导致修改不生效。 -
mybatis在mapper接口和mapper.xml中传递参数的注意事项以及使用
2022-05-11 09:53:272、传递参数时如何在mapper.xml取值 Map传递参数,直接在sql中取出key即可 。 对象传递参数,直接在sql中取出对象的属性即可 只有一个基本类型参数的情况下,可以直接在sql中取到,通过参数名字. 多个参数用Map... -
mapper.xml中resultType映射类型的问题
2022-03-24 09:56:11在使用 mapper.xml 进行编写时 resultMap=“String” String竟然引用不进去,在程序中标红,别的mapper中也映射过但是别的就好使,然后重新启动的时候代码执行报错 <select id="queryAreaBy" parameterType=... -
Intellij中格式化mapper.xml整齐
2021-03-27 08:59:53默认情况下Intellij在格式化mapper.xml文件时,代码会变得非常乱不易查看,采用如下配置可以使格式化后的代码变得整齐。