------------------------------------------------------
-----------------自定义-查询举例----------------------
------------------------------------------------------
-------------------------mapper.xml:-------------------------
<select id="queryListById" resultMap="BaseResultMap">
select *
from sys_project
where id=#{id}
order by createDate
</select>
-----------------------mapper:------------------------
/***不可直接写(String str)or(String str,Integer id)这样传不了值**/
List<ProjectModel> queryListById(@Param("id") String id);
---------------------serviceImpl:---------------------
@Autowired
private ProjectMapper projectMapper;
List<ProjectModel> projectModel=projectMapper.queryListById(id.toString());
-----------------------------------------------------
-----------------自定义-删除举例---------------------
-----------------------------------------------------
-----------------------mapper.xml:--------------------------
<delete id="deleteById" >
delete from sys_project
where id=#{id}
</delete>
-----------------------mapper:-----------------------
int deleteById(@Param("id") Integer id);
--------------------serviceImpl:---------------------
@Autowired
private ProjectMapper projectMapper;
//直接写删除方法。
projectMapper.deleteById(id);
-----------------------------------------------------
-----------------自定义-修改举例---------------------
-----------------------------------------------------
-----------------------mapper.xml:--------------------------
<update id="updateByIdData">
update sys_project set projectName=#{projectName} where id=#{id}
</update>
-----------------------mapper:-----------------------
int updateByIdData(@Param("projectName") String projectName, @Param("id") Integer id);
--------------------serviceImpl:---------------------
@Autowired
private ProjectMapper projectMapper;
//更新数据:
projectMapper.updateByIdData(proName,id);
-----------------------------------------------------
-----------------自定义-添加举例---------------------
-----------------------------------------------------
----------------------mapper.xml:--------------------------
<!--加入useGeneratedKeys="true" keyProperty="id" keyColumn="id" -->
<!--执行int i=projectMapper.insertSelectiveData(projectModel);后会生成ID并把id添加到实体对象中。在接下来取id值时候用-->
<!--很多的时候是没有加此参数,类中取不到id值,值为null 就报空指针异常-->
<insert id="insertSelectiveData" parameterType="org.xx.xx.xxxx.model.ProjectModel" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
insert into sys_project
<trim prefix="("suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="projectTitle != null">
projectTitle,
</if>
.....
<!--写对应字段-->
.....
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="projectTitle != null">
#{projectTitle,jdbcType=VARCHAR},
</if>
.....
<!--写对应字段-->
</trim>
</insert>
-----------------------mapper:-----------------------
Integer insertSelectiveData(ProjectModel data);
--------------------serviceImpl:---------------------
@Autowired
private ProjectMapper projectMapper;
ProjectModel projectModel=new ProjectModel();
projectModel.setProjectTitle(xxxx);
.....
.....
//添加数据:
projectMapper.insertSelectiveData(projectModel);
//若使用自动生成的,不用写mapper和xml,直接用自动生成的xml中的insert即可。
//举例:
int i=projectMapper.insertSelective(data);
if(i>0){
logger.info("添加"+xxx+"数据成功。");
}
--------------------------------------------------------------------------
--------------------------------------------------------------------------
MyBatis增删查改【java基础】
最新推荐文章于 2024-07-03 18:06:41 发布