多平台统一管理软件接口,如何实现多平台统一管理软件接口
316
2022-10-11
mybatis之增删改查
目录jar包需要三个 连接数据库的以及mybatis的jar包下面创建数据库 复制到一個文档里面导入数据库即可mybatisUtils文件mybatis.xml配置文件ParkingMapp接口ParkingMapper.xml文件为增删改查sql語句model类ParkingInfo最后就是测试类了:总结
jar包需要三个 连接数据库的以及mybatis的jar包
下面创建数据库 复制到一個文档里面导入数据库即可
/*
Navicat mysql Data Transfer
Source Server : localhost_3306
Source Server Version : 50731
Source Host : localhost:3306
Source Database : ssm_dome_tingche01
Target Server Type : MYSQL
Target Server Version : 50731
File Encoding : 65001
Date: 2021-07-12 16:49:15
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tbl_parking
-- ----------------------------
DROP TABLE IF EXISTS `tbl_parking`;
CREATE TABLE `tbl_parking` (
`pId` int(11) NOT NULL AUTO_INCREMENT,
`carNo` varchar(50) DEFAULT NULL,
`pMark` varchar(20) DEFAULT NULL,
PRIMARY KEY (`pId`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_parking
-- ----------------------------
INSERT INTO `tbl_parking` VALUES ('1', ' 浙A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('2', '京A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('3', '豫A88888', '123456');
INSERT INTO `tbl_parking` VALUES ('4', '貴A88888', '124566');
INSERT INTO `tbl_parking` VALUES ('5', '莞A88888', '123456');
-- ----------------------------
-- Table structure for tbl_parking_detail
-- ----------------------------
DROP TABLE IF EXISTS `tbl_parking_detail`;
CREATE TABLE `tbl_parking_detail` (
`pdId` int(11) NOT NULL AUTO_INCREMENT,
`pId` int(11) DEFAULT NULL,
`beginDate` datetime NOT NULL,
`endDate` datetime DEFAULT NULL,
`pDur` int(11) DEFAULT NULL,
`pCost` float DEFAULT NULL,
`pName` varchar(11) DEFAULT NULL,
PRIMARY KEY (`pdId`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tbl_parking_detail
-- ----------------------------
INSERT INTO `tbl_parking_detail` VALUES ('2', '2', '2019-05-15 17:06:30', '2019-05-15 19:06:30', '2', '30', '張四');
INSERT INTO `tbl_parking_detail` VALUES ('3', '3', '2019-05-15 17:06:30', '2019-05-15 20:06:30', '1', '50', '李四');
INSERT INTO `tbl_parking_detail` VALUES ('4', '4', '2021-07-12 10:57:25', '2021-07-14 14:17:52', '48', '200', '趙四');
INSERT INTO `tbl_parking_detail` VALUES ('5', '5', '2021-07-22 14:17:37', '2021-07-23 14:17:40', '1', '5', '趙雲');
INSERT INTO `tbl_parking_detail` VALUES ('6', '1', '2019-05-15 17:06:30', '2019-05-15 18:06:30', '1', '11', 'dd');
mybatisUtils文件
package cn.hp.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
private static SqlSessionFactory sessionFactory;
static{
String resource="mybatis.xml";
try {
InputStream is=Resources.getResourceAsStream(resource);
sessionFactory=new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}
mybatis.xml配置文件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
ParkingMapp接口
package cn.hp.dao;
import cn.hp.model.ParkingInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ParkingMapper {
public List
//查詢大於某個車費大於30的車
public List
//查詢某個停車信息
public ParkingInfo findById(String id);
//根據車主名稱模糊查詢
public List
//查詢大於某個車費大於30的車 以及車主姓名模糊查詢
public List
//Map接受
public List
//刪除某個信息
public int deletePark(int pdid);
//修改某個信息
public int update(ParkingInfo pi);
//添加一條數據
public int add(ParkingInfo pi);
}
ParkingMapper.xml文件为增删改查sql語句
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select d.*,p.carNo from tbl_parking_detail d,tbl_parking p where d.pid=p.pid
select * from tbl_parking_detail where pCost>#{pCost}
select * from tbl_parking_detail where pdid=#{pdid}
select * from tbl_parking_detail where pName like '%${pName}%'
select * from tbl_parking_detail where pName like '%${pName}%' and pCost>#{pCost}
select * from tbl_parking_detail where pDur=#{pDur} and pCost>#{pCost}
delete from tbl_parking_detail where pdid=#{pdid}
update tbl_parking_detail set pName =#{pName} where pdid=#{pdid}
insert into tbl_parking_detail values (null,#{pid},#{beginDate},#{endDate},#{pDur},#{pCost},#{pName})
model类ParkingInfo
package cn.hp.model;
public class ParkingInfo {
public int pdid;
public int pid;
public String beginDate;
public String endDate;
public int pDur;
public String pName;
public float pCost;
public String carNo;
public int getPdid() {
return pdid;
}
public void setPdid(int pdid) {
this.pdid = pdid;
}
public ParkingInfo(int pid, String beginDate, String endDate, int pDur, String pName, float pCost, String carNo) {
this.pid = pid;
this.beginDate = beginDate;
this.endDate = endDate;
this.pDur = pDur;
this.pName = pName;
this.pCost = pCost;
this.carNo = carNo;
}
@Override
public String toString() {
return "ParkingInfo{" +
"pdid=" + pdid +
", pid=" + pid +
", beginDate='" + beginDate + '\'' +
", endDate='" + endDate + '\'' +
", pDur=" + pDur +
", pName='" + pName + '\'' +
", pCost=" + pCost +
", carNo='" + carNo + '\'' +
'}';
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getBeginDate() {
return beginDate;
}
public void setBeginDate(String beginDate) {
this.beginDate = beginDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public int getpDur() {
return pDur;
}
public void setpDur(int pDur) {
this.pDur = pDur;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public float getpCost() {
return pCost;
}
public void setpCost(float pCost) {
this.pCost = pCost;
}
public String getCarNo() {
return carNo;
}
public void setCarNo(String carNo) {
this.carNo = carNo;
}
public ParkingInfo(int pdid, int pid, String beginDate, String endDate, int pDur, String pName, float pCost, String carNo) {
this.pdid = pdid;
this.pid = pid;
this.beginDate = beginDate;
this.endDate = endDate;
this.pDur = pDur;
this.pName = pName;
this.pCost = pCost;
this.carNo = carNo;
}
public ParkingInfo() {
}
}
最后就是测试类了:
package cn.hp.test;
import cn.hp.dao.ParkingMapper;
import cn.hp.model.ParkingInfo;
import cn.hp.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test01 {
public static void main(String[] args) {
// test1();
// test2();
// test3();
// test4();
// test5();
// test6();
// test7();
// test8();
test9();
}
private static void test9() {
SqlSession session =MybatisUtils.getSession();
int i= session.getMapper(ParkingMapper.class).add(new ParkingInfo(1,"2019-05-15-17:06:30","2019-05-15-18:06:30",1,"dd",11,"sss"));
if (i>0){
System.out.println("修改ok");
}
session.commit();
}
private static void test8(){
SqlSession session =MybatisUtils.getSession();
ParkingInfo pi= session.getMapper(ParkingMapper.class).findById("2");
pi.setpName("張四");
int result=session.getMapper(ParkingMapper.class).update(pi);
if (result>0){
System.out.println("修改ok");
}
session.commit();
}
private static void test7() {
SqlSession session =MybatisUtils.getSession();
int result= session.getMapper(ParkingMapper.class).deletePark(1);
if (result>0){
System.out.println("刪除ok");
}
session.commit();
}
private static void test6() {
SqlSession session =MybatisUtils.getSession();
Map
map.put("pDur",1);
map.put("pCost",30);
List
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getpCost()+"\t"+s.getpName());
}
}
private static void test5() {
SqlSession session =MybatisUtils.getSession();
List
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getpCost()+"\t"+s.getpName());
}
}
private static void test4() {
SqlSession session =MybatisUtils.getSession();
List
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getpName());
}
}
private static void test3() {
SqlSession session =MybatisUtils.getSession();
ParkingInfo si= session.getMapper(ParkingMapper.class).findById("2");
System.out.println(si.getPdid()+"\t"+si.getPid()+"\t"+si.beginDate+"\t"+si.endDate+"\t"+si.pDur+"\t"+si.pCost);
}
private static void test2() {
SqlSession session =MybatisUtils.getSession();
List
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getpCost());
}
}
private static void test1() {
SqlSession session =MybatisUtils.getSession();
List
for (ParkingInfo s:parkingInfoList){
System.out.println(s.getPdid()+"\t"+s.getCarNo());
}
}
}
总结
本篇文章就到这里了,希望能给你带来帮助,也希望你能够多多关注我们的更多内容!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~