mybatis(1)

  

 

 

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
<?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.beiluo.pojo">
        <select id="listCategory" resultType="Category">
            select * from category_
        </select>
         
        <insert id="addCategory" parameterType="Category">
            insert into category_ ( name ) values (#{name})
        </insert>
         
        <delete id="deleteCategory">
            delete from category_ where id = #{id}
        </delete>
         
        <select id="getCategory" parameterType="_int" resultType="Category">
            select * from category_ where id = #{id}
        </select>
         
        <update id="updateCategory" parameterType="Category">
            update category_ set name=#{name} where id=#{id}
        </update>
         
        <!-- 模糊查询 -->
        <select id="listCategoryByName" parameterType="string" resultType="Category">
            select * from category_ where name like concat('%',#{0},'%')
        </select>
         
        <!-- 多条件查询 -->
        <select id="listCategoryByIdAndName" parameterType="map" resultType="Category">
            select * from category_ where id> #{id} and name like concat('%',#{name},'%')
        </select>
         
    </mapper>
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
  

  

 

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.beiluo.test;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
 
import javax.websocket.Session;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import com.beiluo.pojo.Category;
 
 
 
public class TestCRUD {
     static TestCRUD  test =new TestCRUD();
     
     Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
     
        while(true){
            int choice = test.showMenu();
            if(choice == 6){
                break;
            }
            test.chooseMenu(choice);
        }      
         
    }
     
     
    public int showMenu(){
        System.out.println("=========欢迎使用mybatis的CRUD操作=========");
        System.out.println("\t1.添加商品");
        System.out.println("\t2.修改商品");
        System.out.println("\t3.删除商品");
        System.out.println("\t4.查询商品");
        System.out.println("\t5.所有商品");
        System.out.println("\t6.退出系统");
        System.out.println("*********************");
        System.out.print("请选择菜单:");
        int choice = input.nextInt();
        return choice;
    }
     
    public boolean chooseMenu(int choice) throws IOException{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sessionFactory.openSession();
         
        boolean result = true;
        String go_on = "Y";
        //判断用户的输入
        switch(choice){
        case 1:
            System.out.println("=========添加商品=========");
            while(go_on.equals("Y")){
                test.addCategory(session);
                System.out.println("是否继续?(y/n)");
                go_on = input.next();
                go_on = go_on.toUpperCase();
            }  
            break;
        case 2:
            System.out.println("=========修改商品=========");
            test.updaetCategory(session);
            break;
        case 3:
            System.out.println("=========删除商品=========");
            test.deleteCategory(session);
            break;
        case 4:
            System.out.println("=========查询单个商品==========");
        test.selectCategory(session);
            break;
        case 5:
            System.out.println("==========查找商品列表=========");
            listAll(session);
            break;
        case 6:
            System.out.println("退出系统,谢谢使用");
            result = false;
            break;
        default:
            System.out.println("你输入的有误!!!");
            break
        }
         
        session.commit();
        session.close();
        return result;
             
    }
     
     
     
     
    //增加
    public void addCategory(SqlSession session){
        Category c = new Category();
        System.out.println("输入Category的name");
        String name = input.next();
        c.setName(name);
        session.insert("addCategory",c);
        listAll(session);
    }
 
    //查看所有Category
    private static void listAll(SqlSession session){
        List<Category> cs =  session.selectList("listCategory");
        System.out.println("id\tname");
        for (Category c : cs) {
             
            System.out.println(c.getId()+"\t"+c.getName());
        }
         
    }
     
    public void deleteCategory(SqlSession session){
        System.out.println("输入要删除的id");
        Category c =new Category();
        int id = input.nextInt();
        c.setId(id);
        session.delete("deleteCategory",c);
        listAll(session);
    }
 
    public void selectCategory(SqlSession session){
        System.out.println("请输入要查找的编号");
        int id = input.nextInt();
        Category c =session.selectOne("getCategory", id);
         
    System.out.println("查找的记录为:"+c.getId()+"\t"+c.getName());
    }
 
    public void updaetCategory(SqlSession session){
        System.out.println("请输入要修改的编号");
        int id = input.nextInt();
        Category c =session.selectOne("getCategory", id);
        System.out.println("请输入要修改的名字");
        String name = input.next();
        c.setName(name);
        session.update("updateCategory",c);
        System.out.println("修改后的记录为:"+c.getId()+"\t"+c.getName());
//      listAll(session);
    }
 
     
}

  

posted @   我有满天星辰  阅读(156)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示