系统公告表
CREATE TABLE `notice` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '公告标题',
`content` text COLLATE utf8mb4_unicode_ci COMMENT '公告内容',
`time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发布时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统公告表';
Entity
public class Notice {
private Integer id;
private String title;
private String content;
private String time;
}
Mapper
public interface NoticeMapper {
int insert(Notice notice);
void updateById(Notice notice);
void deleteById(Integer id);
@Select("select * from `notice` where id = #{id}")
Notice selectById(Integer id);
List<Notice> selectAll(Notice notice);
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.NoticeMapper">
<select id="selectAll" resultType="com.example.entity.Notice">
select * from `notice`
<where>
<if test="title != null"> and title like concat('%', #{
title}, '%')</if>
</where>
</select>
<delete id="deleteById">
delete from `notice`
where id = #{
id}
</delete>
<!-- insert into notice (username, password, ...) values ('notice', 'notice', ...) -->
<insert id="insert" parameterType="com.example.entity.Notice" useGeneratedKeys="true">
insert into `notice`
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="title != null">title,</if>
<if test="content != null">content,</if>
<if test="time != null">time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{
id},</if>
<if test="title != null">#{
title},</if>
<if test="content != null">#{
content},</if>
<if test="time != null">#{
time},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.example.entity.Notice">
update `notice`
<set>
<if test="title != null">
title = #{
title},
</if>
<if test="content != null">
content = #{
content},
</if>
<if test="time != null">
time = #{
time},
</if>
</set>
where id = #{
id}
</update>
</mapper>
Service
@Service
public class NoticeService {
@Resource
private NoticeMapper noticeMapper;
public void