jdbcType="BIGINT" 和class Long 不匹配
时间: 2023-07-06 07:30:54 浏览: 59
这是因为,在使用 MyBatis 进行数据库操作时,MyBatis 会将从数据库中查询到的数据转换成对应的 Java 类型,以便于在 Java 代码中进行处理。而在 MyBatis 中,JDBC 类型和 Java 类型之间存在一定的映射关系,可以通过 `jdbcType` 属性来指定查询结果应该转换成的 Java 类型。
在这个问题中,`jdbcType="BIGINT"` 表示查询结果的 JDBC 类型为 `BIGINT`,而 MyBatis 默认将 `BIGINT` 映射成 `java.lang.Long` 类型。因此,如果你在 MyBatis 的映射文件中将查询结果映射成 `Long` 类型,在执行查询操作时就不会出现类型不匹配的问题。
如果你确实需要将查询结果映射成其他类型,可以在映射文件中通过 `resultType` 属性来指定。例如,如果你希望将查询结果映射成 `java.math.BigInteger` 类型,可以这样写:
```
<resultMap id="myResultMap" type="java.math.BigInteger">
<result column="my_column" jdbcType="BIGINT"/>
</resultMap>
```
这样,在执行查询操作时,MyBatis 就会将查询结果转换成 `java.math.BigInteger` 类型,避免了类型不匹配的问题。
相关问题
mybatis bigint jdbctype
In MyBatis, if you want to map a column of type `BIGINT` in your database to a Java type, you can use the `java.lang.Long` data type.
To specify the JDBC type for the mapping, you can use the `jdbcType` attribute in your MyBatis mapping XML file or annotations. For a `BIGINT` column, the corresponding JDBC type is `BIGINT`. Here's an example:
Using XML:
```xml
<resultMap id="myResultMap" type="com.example.MyEntity">
<id property="id" column="id" jdbcType="BIGINT"/>
<!-- Other mappings -->
</resultMap>
```
Using annotations:
```java
public class MyEntity {
@Id
@Column(name = "id", jdbcType = JdbcType.BIGINT)
private Long id;
// Other fields and getters/setters
}
```
By specifying the `jdbcType` as `BIGINT`, MyBatis will handle the mapping between the `BIGINT` database column and the `Long` Java type.
jeecg mybatisplus怎么自动生成代码
Jeecg是一个基于Mybatis-Plus的快开发平台,可以通过Jeecg的在线代码生成器来自动生成代码。具体步骤如下:
1.在Jeecg官网下载JeecgBoot代码生成器插件,并安装到你的IDE中。
2.在JeecgBoot项目中,找到你需要生成代码的表,右键选择“在线代码生成器”。
3.在弹出的代码生成器页面中,填写相关信息,包括包名、模块名、作者等。
4.选择需要生成的代码类型,包括Controller、Service、Dao、Mapper等。
5.点击“生成”按钮,等待代码生成完成。
6.在生成的代码中,根据需要进行修改和完善。
下面是一个示例代码,演示了如何使用Jeecg的在线代码生成器自动生成Controller、Service、Dao、Mapper等代码:
```java
// 自动生成的Controller类
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/get/{id}")
public Test get(@PathVariable("id") Long id) {
return testService.getById(id);
}
@PostMapping("/save")
public boolean save(@RequestBody Test test) {
return testService.save(test);
}
@PutMapping("/update")
public boolean update(@RequestBody Test test) {
return testService.updateById(test);
}
@DeleteMapping("/delete/{id}")
public boolean delete(@PathVariable("id") Long id) {
return testService.removeById(id);
}
}
// 自动生成的Service类
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {
}
// 自动生成的Dao接口
@Mapper
public interface TestMapper extends BaseMapper<Test> {
}
// 自动生成的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="com.example.demo.mapper.TestMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.Test">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List"/>
from test
where id = #{id,jdbcType=BIGINT}
</select>
<insert id="insert" parameterType="com.example.demo.entity.Test">
insert into test (id, name, age)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})
</insert>
<update id="updateById" parameterType="com.example.demo.entity.Test">
update test
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=BIGINT}
</update>
<delete id="deleteById" parameterType="java.lang.Long">
delete from test
where id = #{id,jdbcType=BIGINT}
</delete>
</mapper>
```