springboot整合mybatis
2021-06-03 08:02
标签:esc 一个 doctype turn efault tis gets user 命名 1、yml文件 2、pojo 实体类 sysnotice.java 3、接口类 dao sysnticedao.java 4、sysnoticemapper.xml 5、测试 springboot整合mybatis 标签:esc 一个 doctype turn efault tis gets user 命名 原文地址:https://www.cnblogs.com/16904985zy-aoyu/p/14678614.html#spring datasource
spring.datasource.url=jdbc:mysql:///db_notice?serverTimezone=GMT%2B8&charactterEncoding=uft8
spring.datasource.username=root
spring.datasource.password=123456
#spring mybatis
#配置sql超时
mybatis.configuration.default-statement-timeout=30
#驼峰命名规则
mybatis.configuration.map-underscore-to-camel-case=true
#映射文件路径
mybatis.mapper-locations=classpath:/mapper/*/*.xml
#配置日志
logging.level.com.cy=debug
package com.zy.pj.sys.pojo;
import java.util.Date;
/*sysnotice对象用于存储通知数据
* java中的对象可以简单分为两大类型
* 一类:适用于执行逻辑(控制逻辑,业务逻辑,数据持久逻辑)
* 二类:用于存储数据(pojo对象)
*
* */
public class SysNotice {
/** 公告 ID */
private Long id;
/** 公告标题 */
private String title;
/** 公告类型(1 通知 2 公告) */
private String type;
/** 公告内容 */
private String content;
/** 公告状态(0 正常 1 关闭) */
private String status;
/** 创建时间 */
private Date createdTime;
/** 修改时间*/
private Date modifiedTime;
/** 创建用户 */
private String createdUser;
/** 修改用户*/
private String modifiedUser;
/*备注*/
private String remark;
//生成set/get/tostring方法 快捷键:alt+insert
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
public String getCreatedUser() {
return createdUser;
}
public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
}
public String getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser;
}
@Override
public String toString() {
return "SysNotice{" +
"id=" + id +
", title=‘" + title + ‘\‘‘ +
", type=‘" + type + ‘\‘‘ +
", content=‘" + content + ‘\‘‘ +
", status=‘" + status + ‘\‘‘ +
", createdTime=" + createdTime +
", modifiedTime=" + modifiedTime +
", createdUser=‘" + createdUser + ‘\‘‘ +
", modifiedUser=‘" + modifiedUser + ‘\‘‘ +
",rmark="+remark+‘\‘‘+
‘}‘;
}
}
package com.zy.pj.sys.dao;
import com.zy.pj.sys.pojo.SysNotice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/*
* @Mapper注解是由mybatis框架定义,用于描述数据持久层接口*/
@Mapper
public interface SysNoticeDao {
/*
基于条件查询通告信息
* 返回查询到的通过信息
* 根据多个字段进行查询
* */
List
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.zy.pj.sys.dao.SysNoticeDao">
select id="selectNotices" parameterType="com.zy.pj.sys.pojo.SysNotice" resultType="com.zy.pj.sys.pojo.SysNotice">
select * from sys_notice
where>
if test="type !=null and type !=‘‘">
type =#{type}
if>
if test="title !=null and title !=‘‘">
and title like concat("%",#{title},"%")
if>
if test="modifiedUser !=null and modifiedUser !=‘‘">
and modifiedUser like concat("%",#{modifiedUser},"%")
if>
order by createdTime desc
where>
select>
delete id="deleteById" parameterType="com.zy.pj.sys.pojo.SysNotice">
delete from sys_notice where id in /*(1,2,3,4,5)*/
/*foreach 用于迭代一个数组或者集合*/
foreach collection="ids" open="(" close=")" separator="," item="id">
#{id}
foreach>
delete>
insert id="insertNotice" parameterType="com.zy.pj.sys.pojo.SysNotice">
insert into sys_notice
(title,type,content,status,remark,
createdTime,modifiedTime,createdUser,modifiedUser)
values
(#{title},#{type},#{content},#{status},#{remark},
now(),now(),#{createdUser},#{modifiedUser})
insert>
update id="updateNotice" parameterType="com.zy.pj.sys.pojo.SysNotice">
update sys_notice
set title=#{title},type=#{type},content=#{content},
status=#{status},remark=#{remark},modifiedTime=now(),
modifiedUser=#{modifiedUser}
where id=#{id}
update>
mapper>
package com.zy.pj.sys.dao;
import com.zy.pj.sys.pojo.SysNotice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SysNoticeDaoTests {
@Autowired
private SysNoticeDao sysNoticeDao;
/*
* 根据多个字段进行查询*/
@Test
void testSelectNotices(){
SysNotice notice = new SysNotice();
notice.setType("1");
notice.setTitle("通知");
notice.setModifiedUser("tony");
List
上一篇:Java开发环境搭建
下一篇:4.19Java数组存储表格数据