• Spring中的IOC控制Mybatis案例


    我们之前的案例都是基于单独的Mybatis练习,本次案例结合Spring的IOC管理Bean的方式来调用Mybatis中的数据库操作。这次案例中我们可以通过2种方式运行你的Mybatis.
    1 通过Srping的bean在junit完成测试
    2 通过Web应用程序上下文,监听方式来加载Mybatis和Spring管理
    一 我们先来看看数据库个结构,很简单就一个表
    在这里插入图片描述
    在这里插入图片描述
    二 通过idea工具下的Maven创建项目的结构。我们依然使用Maven的聚合方式来管理项目结构
    在这里插入图片描述
    在这里插入图片描述
    1 父项目的pom文件内容;这里面定义项目整体会用到的JAR包。通过《dependency》方式引入JAR包

    
    
      4.0.0
      com.zxf
      SpringProject
      pom
      1.0-SNAPSHOT
      
        Spring-web1
      
        
        
            4.12
            3.2.8
            5.1.32
            1.2
            3.1.0
            2.0
            1.2.17
    
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            4.1.6.RELEASE
            1.1.3
            3.3.1
            2.2.2
            3.17.1-GA
            1.2.3
            1.1.2
        
        
            
                
                    junit
                    junit
                    ${junit.version}
                
                
                
                    org.mybatis
                    mybatis
                    ${mybatis.version}
                
                
                
                    mysql
                    mysql-connector-java
                    ${mysql.version}
                
                
                
                    jstl
                    jstl
                    ${jstl.version}
                
            
                
                    javax.servlet
                    javax.servlet-api
                    ${servlet.version}
                    provided
                
                
                    javax.servlet
                    jsp-api
                    ${jsp.version}
                
                
                
                    log4j
                    log4j
                    ${log4j.version}
                
                
                
                
                    org.springframework
                    spring-aop
                    ${spring-aop.version}
                
                
                
                    org.springframework
                    spring-aspects
                    ${spring-aspects.version}
                
                
                
                    org.springframework
                    spring-beans
                    ${spring-beans.version}
                
                
                
                    org.springframework
                    spring-context
                    ${spring-context.version}
                
                
                
                    org.springframework
                    spring-context-support
                    ${spring-context-support.version}
                
                
                
                    org.springframework
                    spring-core
                    ${spring-core.version}
                
                
                
                    org.springframework
                    spring-expression
                    ${spring-expression.version}
                
                
                
                    org.springframework
                    spring-instrument
                    ${spring-instrument.version}
                
                
                
                    org.springframework
                    spring-instrument-tomcat
                    ${spring-instrument-tomcat.version}
                
                
                
                    org.springframework
                    spring-jdbc
                    ${spring-jdbc.version}
                
                
                
                    org.springframework
                    spring-jms
                    ${spring-jms.version}
                
                
                
                    org.springframework
                    spring-messaging
                    ${spring-messaging.version}
                
                
                
                    org.springframework
                    spring-orm
                    ${spring-orm.version}
                
                
                
                    org.springframework
                    spring-oxm
                    ${spring-oxm.version}
                
                
                
                    org.springframework
                    spring-test
                    ${spring-test.version}
                    test
                
                
                
                    org.springframework
                    spring-tx
                    ${spring-tx.version}
                
                
                
                    org.springframework
                    spring-web
                    ${spring-web.version}
                
                
                
                    org.springframework
                    spring-webmvc
                    ${spring-webmvc.version}
                
                
                
                    org.springframework
                    spring-webmvc-portlet
                    ${spring-webmvc-portlet.version}
                
                
                
                    org.springframework
                    spring-websocket
                    ${spring-websocket.version}
                
                
                
                
                    commons-logging
                    commons-logging
                    ${commons-logging.version}
                
                
                
                    asm
                    asm
                    ${asm.version}
                
                
                
                    cglib
                    cglib
                    ${cglib.version}
                
                
                
                    org.javassist
                    javassist
                    ${javassist.version}
                
                
                
                    org.mybatis
                    mybatis-spring
                    ${mybatis-spring.version}
                
            
        
       
           
               
                   src/main/java
                   
                       **/*.xml
                   
               
    
               
                   src/main/resouces
                   
                       **/*.xml
                       **/*.properties
                   
               
           
           
               
                   org.apache.tomcat.maven
                   tomcat7-maven-plugin
                   
                       /
                       9090
                   
               
           
       
    
    
    • 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
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275

    2 子项目引入父项目的JAR包

    
    
    
        
            SpringProject
            com.zxf
            1.0-SNAPSHOT
        
        4.0.0
        Spring-web1
        war
        
        
            junit
            junit
        
        
        
            org.mybatis
            mybatis
        
        
        
            mysql
            mysql-connector-java
        
        
        
            jstl
            jstl
            provided
        
        
        
            javax.servlet
            javax.servlet-api
            provided
        
        
            javax.servlet
            jsp-api
            provided
        
        
        
            log4j
            log4j
        
            
            
            
                org.springframework
                spring-aop
            
            
            
                org.springframework
                spring-aspects
            
            
            
                org.springframework
                spring-beans
            
            
            
                org.springframework
                spring-context
            
            
            
                org.springframework
                spring-context-support
            
            
            
                org.springframework
                spring-core
            
            
            
                org.springframework
                spring-expression
            
            
            
                org.springframework
                spring-instrument
            
            
            
                org.springframework
                spring-instrument-tomcat
                ${spring-instrument-tomcat.version}
            
            
            
                org.springframework
                spring-jdbc
            
            
            
                org.springframework
                spring-jms
            
            
            
                org.springframework
                spring-messaging
            
            
            
                org.springframework
                spring-orm
            
            
            
                org.springframework
                spring-oxm
            
            
            
                org.springframework
                spring-test
                test
            
            
            
                org.springframework
                spring-tx
            
            
            
                org.springframework
                spring-web
            
            
            
                org.springframework
                spring-webmvc
            
            
            
                org.springframework
                spring-webmvc-portlet
            
            
            
                org.springframework
                spring-websocket
            
            
            
            
                commons-logging
                commons-logging
            
            
            
                asm
                asm
            
            
            
                cglib
                cglib
            
            
            
                org.javassist
                javassist
            
            
            
                org.mybatis
                mybatis-spring
            
        
    
    
    • 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
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180

    3 开始编写pojo层实体类

    package com.zxf.web.pojo;
    
    public class Flower {
        private  int id;
        private String name;
        private float price;
        private String production;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public float getPrice() {
            return price;
        }
    
        public void setPrice(float price) {
            this.price = price;
        }
    
        public String getProduction() {
            return production;
        }
    
        public void setProduction(String production) {
            this.production = production;
        }
    }
    
    • 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

    4 编写Mybatis的Mapper,这次使用的是注解方式。

    package com.zxf.web.mapper;
    
    import com.zxf.web.pojo.Flower;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface FlowerMapper {
        @Select("select * from flower")
        List getAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    5 编写Service层,后面我们会把Service交给Spring容器来管理

    package com.zxf.web.service;
    
    import com.zxf.web.pojo.Flower;
    
    import java.util.List;
    
    public interface FlowerService {
        List show();
    }
    
    
    
    package com.zxf.web.service.impl;
    
    import com.zxf.web.mapper.FlowerMapper;
    import com.zxf.web.pojo.Flower;
    import com.zxf.web.service.FlowerService;
    
    import java.util.List;
    
    public class FlowerServiceImpl implements FlowerService {
        private FlowerMapper flowerMapper;
    
        public FlowerMapper getFlowerMapper() {
            return flowerMapper;
        }
    
        public void setFlowerMapper(FlowerMapper flowerMapper) {
            this.flowerMapper = flowerMapper;
        }
    
        public List show() {
            return  flowerMapper.getAll();
        }
    }
    
    • 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

    6 编写Spring配置文件,它负责加载Mybatis用到的资源和数据库连接

    
    
        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
            
            
        
        
        
            
        
    
    
    • 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

    7 以上都编写好以后,我们先用junit来测试一下,各个资源资源直接是否正常

    package com.zxf.web.test;
    
    import com.zxf.web.pojo.Flower;
    import com.zxf.web.service.impl.FlowerServiceImpl;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.List;
    
    public class Test1 {
        @Test
        public void test1(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            FlowerServiceImpl flowerService=ac.getBean("flowerSerivce",FlowerServiceImpl.class);
            List show = flowerService.show();
            for(Flower f:show){
                System.out.println(f.getProduction());
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述
    8 编写Servlet类,也是采用注解的方式建立Servlet

    package com.zxf.web.servlet;
    
    import com.zxf.web.service.FlowerService;
    import com.zxf.web.service.impl.FlowerServiceImpl;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet("/flowerServlet")
    public class FlowerServlet extends HttpServlet {
        private FlowerService flowerService;
        @Override
        public void init() throws ServletException {
            ApplicationContext ac= WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
            flowerService=ac.getBean("flowerSerivce", FlowerServiceImpl.class);
        }
        @Override
        public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
            req.setAttribute("list",flowerService.show());
            req.getRequestDispatcher("showFlower.jsp").forward(req,res);
        }
    }
    
    • 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

    9 这个时候我们用web.xml方式加载配置文件
    web.xml文件内容

    
    
    
      
      
        contextConfigLocation
        classpath*:applicationContext.xml
      
      
      
        org.springframework.web.context.ContextLoaderListener
      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    这样我们在Servlet中就可以直接调用Spring中定义的标签的Service了
    我们使用Maven的打包工具 把当前的web工程打WAR包
    在这里插入图片描述
    到这里把你的WAR包拷贝的tomcat中的webapps
    在这里插入图片描述
    最后在WEB层运行的结果
    在这里插入图片描述
    如果对你有帮助,请记得点个赞哦!!!!!!

  • 相关阅读:
    【Java 基础篇】Java线程同步:Lock接口详解
    Android:关于定时任务重启之后的问题研究
    2311rust到20版本更新
    TSINGSEE青犀煤矿矿井视频监控与汇聚融合管理视频监管平台建设方案
    【附源码】计算机毕业设计java自习室管理系统设计与实现
    网络原理---拿捏应用层:HTTP协议
    /node_modules/XXX/index.js:XXX XXX ??= X;SyntaxError: Unexpected token ‘??=‘
    前端各种布局
    MySQL
    Apache httpd漏洞复现
  • 原文地址:https://blog.csdn.net/m0_67391518/article/details/126516484