1.自定义sql方法的使用,mapper的依赖配置文件就不一一赘述了,详情: https://blog.csdn.net/brantykl/article/details/129360320?spm=1001.2014.3001.5502
2.实体类映射
@Repository
@Data
@Table(name = "t_company")
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "shortName")
private String shortName;
@Column(name = "fullName")
private String fullName;
@Column(name = "isFamous")
private Integer isFamous;
@Column(name = "state")
private Integer state;
@Column(name = "remark")
private String remark;
@Column(name = "createDate")
private Date createDate;
@Column(name = "cityId")
private Integer cityId;
@Column(name = "userId")
private Integer userId;
@Column(name = "orderNo")
private Integer orderNo;
}
3.生成表和数据
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_company
-- ----------------------------
DROP TABLE IF EXISTS `t_company`;
CREATE TABLE `t_company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`shortName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '公司名称',
`fullName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '公司全称',
`isFamous` int(11) NULL DEFAULT NULL COMMENT '是否名企:\r\n 0.不是 \r\n 1.是',
`state` int(11) NULL DEFAULT NULL COMMENT '状态:\r\n 0.启用\r\n 1.禁用',
`remark` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`createDate` datetime NULL DEFAULT NULL,
`cityId` int(11) NULL DEFAULT NULL,
`userId` int(11) NULL DEFAULT NULL,
`orderNo` int(11) NULL DEFAULT NULL COMMENT '排序编号',
PRIMARY KEY (`id`) USING BTREE,
INDEX `fk_compone_user`(`userId`) USING BTREE,
INDEX `FK_fk_dict_company`(`cityId`) USING BTREE,
CONSTRAINT `fk_compone_user` FOREIGN KEY (`userId`) REFERENCES `t_user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FK_fk_dict_company` FOREIGN KEY (`cityId`) REFERENCES `t_dict` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB AUTO_INCREMENT = 36 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '公司表' ROW_FORMAT = Compact;
-- ----------------------------
-- Records of t_company
-- ----------------------------
INSERT INTO `t_company` VALUES (1, '北京淘宝网', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (2, '北京支付宝', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 10, 1, 1);
INSERT INTO `t_company` VALUES (3, '北京蚂蚁金服', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 10, 1, 1);
INSERT INTO `t_company` VALUES (4, '七牛云服务', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 10, 1, 1);
INSERT INTO `t_company` VALUES (5, '拼多多', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 10, 1, 1);
INSERT INTO `t_company` VALUES (6, '唯品会', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 10, 1, 1);
INSERT INTO `t_company` VALUES (7, '唯品会', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 12, 1, 1);
INSERT INTO `t_company` VALUES (8, '腾讯公司', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 13, 1, 1);
INSERT INTO `t_company` VALUES (9, '杭州淘宝网', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 13, 1, 1);
INSERT INTO `t_company` VALUES (10, '浪潮软件', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 14, 1, 1);
INSERT INTO `t_company` VALUES (12, '简途旅行', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 15, 1, 1);
INSERT INTO `t_company` VALUES (13, '绿盟科技', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 16, 1, 1);
INSERT INTO `t_company` VALUES (14, '金信石', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (15, '贵鑫堂', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 3, 1, 1);
INSERT INTO `t_company` VALUES (16, '掌众金服', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (17, '晨创科技', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (18, '中软国际', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (19, '全景网', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (20, '翼龙贷', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (21, '中烟新商盟', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (22, '西安泰晟', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (23, '联创佳讯', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (24, '神航星云', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (25, '软通动力', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (26, '网众传媒', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (27, '软通动力', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (28, '文思海辉', NULL, 1, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (29, '赞同科技', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (30, '北正云鼎', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (31, '信雅达', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (32, '东方微银', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (33, '易宝软件', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (34, '智乾区块链', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
INSERT INTO `t_company` VALUES (35, '中科金财', NULL, 0, 0, NULL, '2019-08-08 00:00:00', 9, 1, 1);
SET FOREIGN_KEY_CHECKS = 1;
4.编写xml文件
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.brant.dao.ICompanyDao">
<select id="findAll" resultType="com.brant.entity.Company">
SELECT * FROM t_company
select>
<select id="findById" parameterType="int" resultType="com.brant.entity.Company">
SELECT * FROM t_company WHERE id=#{id}
select>
<update id="update" parameterType="com.brant.entity.Company">
UPDATE t_company SET shortName=#{shortName},fullName=#{fullName},isFamous=#{isFamous},
state=#{state},remark=#{remark},createDate=#{createDate},cityId=#{cityId},userId=#{userId},orderNo=#{orderNo}
WHERE id=#{id}
update>
<delete id="deleteById" parameterType="int">
DELETE FROM t_company WHERE id=#{id}
delete>
mapper>
5.目录结构,这里需要注意xml文件名称需要与ICompanyDao一致.在配置文件配置也要是这样mapper-locations: classpath:com.brant.dao/*Mapper.xml
,否则就不可以找到xml文件(这里的*是任意)
6.编写一个简单demo,依次在controller,service,dao编写deleteById
@RequestMapping("/company")
@Controller
public class CompanyController {
@Autowired
private ICompanyService companyService;
@RequestMapping(method = RequestMethod.GET,path = "/deleteCompanyById/{id}")
@ResponseBody
public String deleteCompanyById( @PathVariable Integer id){
if(StringUtils.isEmpty(companyService.findCompanyById(id))){
return "公司不存在";
}else {
companyService.deleteById(id);
return "删除成功";
}
}
}
public interface ICompanyService {
void deleteById(Integer id);
}
@Service
public class CompanyServiceImpl implements ICompanyService {
@Autowired
private ICompanyDao companyDao;
@Override
public void deleteById(Integer id) {
companyDao.deleteById(id);
}
}
@Component
public interface ICompanyDao extends IBaseDao<Company>{
/**
* description: 根据id删除公司
*
* @params No such property: code for class: Script1
* @return
*/
Boolean delete(Integer id);
}
6.总结
.1)在使用自定义的数据操作时,要注意方法不要与mapper里封装好的方法重复,否则编译会报错
2)在写xml文件时,一定要注意文件名和命名空间(namespace)不要写错了,后面spring找不到
3)在resources建立目录需要使用com/brant/dao的形式,当然也可以直接和dao.下的xxx.java文件写在一起,只要定义好mapper-locations
就可以了
4)除了使用xml也可以使用注解@Delete来实现数据操作
/**
* description: 根据id删除公司
*
* @params No such property: code for class: Script1
* @return
*/
@Delete("DELETE FROM t_company WHERE id=#{id}")
void deleteById(int id);
7.之前有拿到一个ChatGPT的账号,于是就是用了.发现ChatGPT体验非常好,之前碰到的好多问题,都需要我们去百度搜索.可是,百度出来的垃圾数据有许多,而且还有一些不成功的案例.有一些常识性的东西忘记了,百度却要花费十几分钟的时间.因此ChatGPT确实可以提高解决问题的速度.下面贴一些使用ChatGPT的一些问答,当然,对于我来说,ChatGPT下一位老师,简称(柴师傅),后面会借助它在java以及一些其它领域升级打怪.目前在工作没有用到,不过学习上确实受益匪浅,码代码的速度也快多了.后面会更注重一些设计模式底层的东西了.