mybatis中的result的bigint转long是null
时间: 2024-05-20 14:18:12 浏览: 93
的问题该怎么解决?
该问题可能是由于result中的bigint值为空导致的。您可以在MyBatis的映射文件中使用COALESCE函数来处理该问题,将空值转换为0或其他默认值。例如:
<result column="id" property="id" jdbcType="BIGINT" select="COALESCE(id, 0)"/>
这样可以确保即使bigint值为空,MyBatis也可以正确地将其转换为long类型。
相关问题
假设我们已经有了以下两张表: 一级菜单表 first_menu: 字段名 类型 描述 id bigint 主键 name varchar 菜单名称 url varchar 菜单链接 二级菜单表 second_menu: 字段名 类型 描述 id bigint 主键 name varchar 菜单名称 url varchar 菜单链接 first_menu_id bigint 所属的一级菜单id 现在我们需要创建一张标记是否被选中的用户-菜单表 user_menu: 字段名 类型 描述 id bigint 主键 user_id bigint 用户id menu_id bigint 菜单id selected boolean 是否被选中 java 详细设计,使用mybatis 操作数据库
首先,我们需要建立三个实体类,分别对应三张表:
FirstMenu.java
```java
public class FirstMenu {
private Long id;
private String name;
private String url;
// 省略getter和setter
}
```
SecondMenu.java
```java
public class SecondMenu {
private Long id;
private String name;
private String url;
private Long firstMenuId;
// 省略getter和setter
}
```
UserMenu.java
```java
public class UserMenu {
private Long id;
private Long userId;
private Long menuId;
private boolean selected;
// 省略getter和setter
}
```
然后,我们需要创建对应的Mapper接口和XML文件:
FirstMenuMapper.java
```java
public interface FirstMenuMapper {
List<FirstMenu> findAll();
}
```
FirstMenuMapper.xml
```xml
<mapper namespace="com.example.mapper.FirstMenuMapper">
<resultMap id="firstMenuMap" type="com.example.entity.FirstMenu">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="url" property="url"/>
</resultMap>
<select id="findAll" resultMap="firstMenuMap">
select id, name, url from first_menu;
</select>
</mapper>
```
SecondMenuMapper.java
```java
public interface SecondMenuMapper {
List<SecondMenu> findByFirstMenuId(Long firstMenuId);
}
```
SecondMenuMapper.xml
```xml
<mapper namespace="com.example.mapper.SecondMenuMapper">
<resultMap id="secondMenuMap" type="com.example.entity.SecondMenu">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="url" property="url"/>
<result column="first_menu_id" property="firstMenuId"/>
</resultMap>
<select id="findByFirstMenuId" parameterType="Long" resultMap="secondMenuMap">
select id, name, url, first_menu_id from second_menu where first_menu_id = #{firstMenuId};
</select>
</mapper>
```
UserMenuMapper.java
```java
public interface UserMenuMapper {
void save(UserMenu userMenu);
UserMenu findByUserIdAndMenuId(Long userId, Long menuId);
void updateSelected(Long id, boolean selected);
}
```
UserMenuMapper.xml
```xml
<mapper namespace="com.example.mapper.UserMenuMapper">
<insert id="save" parameterType="com.example.entity.UserMenu">
insert into user_menu (user_id, menu_id, selected) values (#{userId}, #{menuId}, #{selected});
</insert>
<resultMap id="userMenuMap" type="com.example.entity.UserMenu">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="menu_id" property="menuId"/>
<result column="selected" property="selected"/>
</resultMap>
<select id="findByUserIdAndMenuId" parameterType="map" resultMap="userMenuMap">
select * from user_menu where user_id = #{userId} and menu_id = #{menuId};
</select>
<update id="updateSelected" parameterType="map">
update user_menu set selected = #{selected} where id = #{id};
</update>
</mapper>
```
最后,我们在Service层进行对应的逻辑操作:
MenuService.java
```java
@Service
public class MenuService {
@Autowired
private FirstMenuMapper firstMenuMapper;
@Autowired
private SecondMenuMapper secondMenuMapper;
@Autowired
private UserMenuMapper userMenuMapper;
public List<FirstMenu> findAllFirstMenu() {
return firstMenuMapper.findAll();
}
public List<SecondMenu> findByFirstMenuId(Long firstMenuId) {
return secondMenuMapper.findByFirstMenuId(firstMenuId);
}
public void saveUserMenu(Long userId, Long menuId) {
UserMenu userMenu = userMenuMapper.findByUserIdAndMenuId(userId, menuId);
if (userMenu == null) {
userMenu = new UserMenu();
userMenu.setUserId(userId);
userMenu.setMenuId(menuId);
userMenu.setSelected(true);
userMenuMapper.save(userMenu);
} else {
userMenuMapper.updateSelected(userMenu.getId(), !userMenu.isSelected());
}
}
}
```
这样,我们就完成了对应的需求。其中,第一个方法用于获取所有一级菜单;第二个方法用于根据一级菜单id获取对应的二级菜单;第三个方法用于保存或更新用户-菜单表中的选中状态。
create table cic_bracket ( id bigint auto_increment primary key, bracket_no varchar(6) not null comment '胶托编号', owning_airport char(3) not null comment '所属站点', current_airport varchar(3) null comment '当前站点', bluetooth_id bigint null comment '蓝牙id', bluetooth_mac varchar(12) null comment '蓝牙MAC地址', create_time timestamp default CURRENT_TIMESTAMP not null comment '创建时间', update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间' ) comment '胶托信息';。基于该表,使用spring,springboot,MybatisPlus框架生成增删改查功能代码
好的,以下是生成增删改查功能代码的步骤:
1. 首先,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
2. 在application.properties文件中添加以下配置:
```properties
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MybatisPlus配置
mybatis-plus.mapper-locations=classpath:mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
# Druid监控配置
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=127.0.0.1
spring.datasource.druid.stat-view-servlet.deny=
```
3. 创建实体类CicBracket.java:
```java
@Data
@TableName("cic_bracket")
public class CicBracket {
@TableId(type = IdType.AUTO)
private Long id;
@NotBlank(message = "胶托编号不能为空")
private String bracketNo;
@NotBlank(message = "所属站点不能为空")
@Length(min = 3, max = 3, message = "所属站点必须为3位字符")
private String owningAirport;
private String currentAirport;
private Long bluetoothId;
private String bluetoothMac;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
```
4. 创建mapper接口CicBracketMapper.java:
```java
@Mapper
public interface CicBracketMapper extends BaseMapper<CicBracket> {
}
```
5. 创建service接口ICicBracketService.java:
```java
public interface ICicBracketService extends IService<CicBracket> {
}
```
6. 创建service实现类CicBracketServiceImpl.java:
```java
@Service
public class CicBracketServiceImpl extends ServiceImpl<CicBracketMapper, CicBracket> implements ICicBracketService {
}
```
7. 创建controller类CicBracketController.java:
```java
@RestController
@RequestMapping("/cic-bracket")
public class CicBracketController {
@Autowired
private ICicBracketService cicBracketService;
@PostMapping("/add")
public Result add(@Validated @RequestBody CicBracket cicBracket) {
cicBracketService.save(cicBracket);
return Result.ok();
}
@PostMapping("/update")
public Result update(@Validated @RequestBody CicBracket cicBracket) {
cicBracketService.updateById(cicBracket);
return Result.ok();
}
@GetMapping("/delete/{id}")
public Result delete(@PathVariable Long id) {
cicBracketService.removeById(id);
return Result.ok();
}
@GetMapping("/get/{id}")
public Result get(@PathVariable Long id) {
CicBracket cicBracket = cicBracketService.getById(id);
return Result.ok().put("data", cicBracket);
}
@GetMapping("/list")
public Result list() {
List<CicBracket> list = cicBracketService.list();
return Result.ok().put("data", list);
}
}
```
8. 最后,启动Spring Boot项目,访问http://localhost:8080/swagger-ui.html,即可使用Swagger测试接口。
以上就是基于该表,使用Spring Boot、MybatisPlus框架生成增删改查功能代码的步骤,如果有疑问可以随时问我。
阅读全文