public class FacilityParam { private String tableName;//表名 private List<String> fieldNames;//字段 private List<String> params;//参数 private String name; private String factory_name; private String workshop_name; private String productionline_name; private String station_name; }使用SpringMVC相关技术对这个实体类写mapper层的修改
时间: 2024-03-01 15:55:22 浏览: 69
oracle pl/sql技术
假设你使用的是 MyBatis 框架,可以按照以下步骤来编写 mapper 层的修改方法:
1.在 mapper.xml 文件中添加 update 标签:
```
<update id="updateFacilityParam" parameterType="com.example.FacilityParam">
UPDATE ${tableName}
<set>
<if test="name != null">name = #{name},</if>
<if test="factory_name != null">factory_name = #{factory_name},</if>
<if test="workshop_name != null">workshop_name = #{workshop_name},</if>
<if test="productionline_name != null">productionline_name = #{productionline_name},</if>
<if test="station_name != null">station_name = #{station_name},</if>
<foreach collection="fieldNames" item="fieldName">
<if test="params[fieldNames.indexOf(fieldName)] != null">${fieldName} = #{params[fieldNames.indexOf(fieldName)]},</if>
</foreach>
</set>
WHERE id = #{id}
</update>
```
2.在 mapper.java 文件中添加对应的方法:
```
public interface FacilityParamMapper {
void updateFacilityParam(FacilityParam facilityParam);
}
```
3.在 Service 层调用该方法:
```
@Service
public class FacilityParamService {
@Autowired
private FacilityParamMapper facilityParamMapper;
public void updateFacilityParam(FacilityParam facilityParam) {
facilityParamMapper.updateFacilityParam(facilityParam);
}
}
```
这样就完成了 mapper 层的修改方法的编写,可以通过调用 Service 层的方法来实现对数据库的修改操作。其中,使用了动态 SQL 的语法,可以根据实体类中各个属性是否为 null 来动态生成 SQL 语句,从而实现只更新部分字段的目的。
阅读全文