H5W3
当前位置:H5W3 > 其他技术问题 > 正文

mybatis-spring-boot-starter 如何忽略非数据字段?

使用springboot+ mybatis-spring-boot-starter整合mybatis,在实体bean添加一个非数据库字段,启动会报错,一直提示没有这个属性
Caused by: java.lang.IllegalStateException: No typehandler found for property test

实体bean

@Data
public class User implements Serializable {
 private Integer id;
 private String userName;
 private String passWord;
 private String realName;
 private List<String> test;  //非数据库字段
}

mapper

<?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.cmb.charge.mapper.UserMapper">
 <resultMap id="BaseResultMap" type="com.cmb.charge.model.user.User">
 <result column="id" property="id" />
 <result column="userName" property="userName" />
 <result column="passWord" property="passWord" />
 <result column="realName" property="realName" />
 <result column="Test" property="test" />
 </resultMap>
 <select id="Sel" resultMap="BaseResultMap" parameterType="java.lang.Integer">
 select * from user where id = #{id}
    </select>
 <select id="loadAll" resultMap="BaseResultMap">
 select *
 from user
    </select>
 <select id="load" resultMap="BaseResultMap" parameterType="java.lang.Integer">
 select
        *
 from user
        where id = #{id}
    </select>
 <insert id="persist" parameterType="com.cmb.charge.model.user.User"
 useGeneratedKeys="true" keyProperty="id">
 insert into user (ID,
        <if test="userName != null">
 userName,
        </if>
 <if test="passWord != null">
 passWord,
        </if>
 <if test="realName != null ">
 realName
        </if>
 )
        values
        (#{id},
        <if test="userName != null">
 #{userName},
        </if>
 <if test="passWord != null">
 #{passWord},
        </if>
 <if test="realName != null ">
 #{realName}
        </if>
 )
    </insert>
 <update id="merge" parameterType="com.cmb.charge.model.user.User"
 useGeneratedKeys="true" keyProperty="id">
 update user
        <set>
 <if test="userName != null">
 userName = #{userName},
            </if>
 <if test="passWord != null">
 passWord = #{passWord},
            </if>
 <if test="realName != null">
 realName = #{realName}
            </if>
 </set> where ID = #{id}
    </update>
</mapper>

pom


<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>charge</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--Spring Boot热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.6.RELEASE</version>
            </plugin>
            <!-- mybatis逆向工程插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <!--配置文件的路径-->
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


回答:

已解决,不需要再mapper里配置test,去除

<result column="Test" property="test" />

本文地址:H5W3 » mybatis-spring-boot-starter 如何忽略非数据字段?

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址