服务器之家

服务器之家 > 正文

mybatis之增删改查

时间:2021-10-14 11:51     来源/作者:池鱼i_

jar包需要三个 连接数据库的以及mybatis的jar包

下面创建数据库 复制到一個文档里面导入数据库即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
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文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="cn.hp.model"/>
    </typeAliases>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm_dome_tingche01?characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
   <mapper resource="cn/hp/dao/ParkingMapper.xml"></mapper>
    </mappers>
</configuration>

ParkingMapp接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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<ParkingInfo> findAll();
//查詢大於某個車費大於30的車
public List<ParkingInfo> findPCost(int pCost);
//查詢某個停車信息
public  ParkingInfo findById(String id);
//根據車主名稱模糊查詢
    public List<ParkingInfo> findParkName(String name);
 //查詢大於某個車費大於30的車 以及車主姓名模糊查詢
    public   List<ParkingInfo>   findPark(@Param("pCost")float pCost,@Param("pName")String pName);
    //Map接受
    public List<ParkingInfo> findParkByMap(Map<String, Object> map);
 
    //刪除某個信息
    public  int deletePark(int pdid);
    //修改某個信息
    public  int update(ParkingInfo pi);
    //添加一條數據
    public  int add(ParkingInfo pi);
}

ParkingMapper.xml文件为增删改查sql語句

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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="cn.hp.dao.ParkingMapper">
<resultMap id="parkingList" type="cn.hp.model.ParkingInfo">
    <result property="pdid" column="pdid"></result>
    <result property="pid" column="pid"></result>
    <result property="beginDate" column="beginDate"></result>
    <result property="endDate" column="endDate"></result>
    <result property="pDur" column="pDur"></result>
    <result property="pCost" column="pCost"></result>
    <result property="pName" column="pName"></result>
    <result property="carNo" column="carNo"></result>
</resultMap>
 
    <select id="findAll" resultMap="parkingList">
        select d.*,p.carNo from tbl_parking_detail d,tbl_parking p where  d.pid=p.pid
    </select>
 
    <select id="findPCost" parameterType="int" resultType="cn.hp.model.ParkingInfo">
        select  * from tbl_parking_detail  where pCost>#{pCost}
    </select>
 
    <select id="findById" parameterType="String" resultType="cn.hp.model.ParkingInfo">
        select * from  tbl_parking_detail where  pdid=#{pdid}
    </select>
    <select id="findParkName"  resultType="cn.hp.model.ParkingInfo">
    select * from  tbl_parking_detail where pName  like '%${pName}%'
    </select>
    <select id="findPark" resultType="cn.hp.model.ParkingInfo">
        select * from  tbl_parking_detail where pName  like '%${pName}%' and  pCost>#{pCost}
    </select>
<select id="findParkByMap" parameterType="map" resultType="cn.hp.model.ParkingInfo">
 select * from  tbl_parking_detail where pDur=#{pDur} and  pCost>#{pCost}
</select>
    <delete id="deletePark" parameterType="cn.hp.model.ParkingInfo" >
        delete  from  tbl_parking_detail where pdid=#{pdid}
    </delete>
    <update id="update" parameterType="cn.hp.model.ParkingInfo">
        update  tbl_parking_detail  set pName =#{pName} where   pdid=#{pdid}
    </update>
    <insert id="add" parameterType="cn.hp.model.ParkingInfo">
        insert into tbl_parking_detail values (null,#{pid},#{beginDate},#{endDate},#{pDur},#{pCost},#{pName})
    </insert>
</mapper>

model类ParkingInfo

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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() {
    }
}

最后就是测试类了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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<String, Object> map = new HashMap<String, Object>();
        map.put("pDur",1);
        map.put("pCost",30);
        List<ParkingInfo> parkingInfoList=  session.getMapper(ParkingMapper.class).findParkByMap(map);
        for (ParkingInfo s:parkingInfoList){
            System.out.println(s.getpCost()+"\t"+s.getpName());
        }
    }
    private static void test5() {
        SqlSession session =MybatisUtils.getSession();
        List<ParkingInfo> parkingInfoList=  session.getMapper(ParkingMapper.class).findPark(30,"趙");
        for (ParkingInfo s:parkingInfoList){
            System.out.println(s.getpCost()+"\t"+s.getpName());
        }
    }
    private static void test4() {
        SqlSession session =MybatisUtils.getSession();
        List<ParkingInfo> parkingInfoList=  session.getMapper(ParkingMapper.class).findParkName("四");
        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<ParkingInfo> parkingInfoList=  session.getMapper(ParkingMapper.class).findPCost(30);
        for (ParkingInfo s:parkingInfoList){
            System.out.println(s.getPdid()+"\t"+s.getpCost());
        }
    }
 
    private static void test1() {
        SqlSession session =MybatisUtils.getSession();
        List<ParkingInfo> parkingInfoList=  session.getMapper(ParkingMapper.class).findAll();
    for (ParkingInfo s:parkingInfoList){
        System.out.println(s.getPdid()+"\t"+s.getCarNo());
    }
    }
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望你能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/best_p1/article/details/118678735

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部