• spring整合mybatis


    spring中整合mybatis

    一 先添加spring框架

    1.创建一个maven项目
    2.在pom.xml中添加spring jar包
    
    
      org.springframework
      spring-context
      4.1.3.RELEASE
    
    
      org.springframework
      spring-core
      4.1.3.RELEASE
    
    
    
      org.springframework
      spring-beans
      4.1.3.RELEASE
    
    
      org.springframework
      spring-context-support
      4.1.3.RELEASE
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    3.创建基本mvc结构

    pojo层:

    Items类

    package com.test.pojo;
    
    import java.util.Date;
    
    public class Items {
           private int id;
           private String name;
           private float price;
           private String detail;
           private String pic;
           private Date createtime;
    
        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 getDetail() {
            return detail;
        }
    
        public void setDetail(String detail) {
            this.detail = detail;
        }
    
        public String getPic() {
            return pic;
        }
    
        public void setPic(String pic) {
            this.pic = pic;
        }
    
        public Date getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(Date createtime) {
            this.createtime = createtime;
        }
    
        @Override
        public String toString() {
            return "Items{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", price=" + price +
                    ", detail='" + detail + '\'' +
                    ", pic='" + pic + '\'' +
                    ", createtime=" + createtime +
                    '}';
        }
    }
    
    • 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

    ​ dao层

    ​ 接口 IItemsDao

    package com.test.dao;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    public interface IItemsDao {
    
    
        public List selectItems();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ​ 实现类 ItemsDao

    package com.test.dao.impl;
    
    import com.test.dao.IItemsDao;
    import com.test.mapper.ItemsMapper;
    import com.test.pojo.Items;
    
    import org.springframework.stereotype.Component;
    
    import java.io.InputStream;
    import java.util.List;
    
    
    public class ItemsDao implements IItemsDao {
    
        public ItemsDao()
        {
    
        }
        public List selectItems()
        {
              return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    service层

    接口:IItemsService

    package com.test.service;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    public interface IItemsService {
    
        public List selectItems();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    实现类:ItemsService

    package com.test.service.impl;
    
    import com.test.dao.IItemsDao;
    import com.test.mapper.ItemsMapper;
    import com.test.pojo.Items;
    import com.test.service.IItemsService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    
    public class ItemsService implements IItemsService {
    
        
        private IItemsDao itemsDao;
    
    
        public List selectItems()
        {
            return itemsDao.selectItems();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    4.创建spring的配置文件

    applicationContext-ioc.xml

    
    
       
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.在配置文件中添加注解扫描包

    
    
       
       
    
       
     
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 在mvc各层中添加注解

      package com.test.dao.impl;
      
      import com.test.dao.IItemsDao;
      import com.test.mapper.ItemsMapper;
      import com.test.pojo.Items;
      
      import org.springframework.stereotype.Component;
      
      import java.io.InputStream;
      import java.util.List;
      
      @Component
      public class ItemsDao implements IItemsDao {
      
          public ItemsDao()
          {
      
          }
          public List selectItems()
          {
             return null;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23

    @Service
    public class ItemsService implements IItemsService {
    
        @Autowired
        private IItemsDao itemsDao;   
        
    
    
        public List selectItems()
        {
            return itemsDao.selectItems();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    二.在spring中引入mybatis

    1.导入mybatis jar包
    
    
      mysql
      mysql-connector-java
      5.1.37
    
    
      org.mybatis
      mybatis
      3.4.6
    
    
      com.mchange
      c3p0
      0.9.5.2
    
    
     
        
          log4j
          log4j
          1.2.17
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.导入spring整合mybatis架包
    
      
          org.mybatis
          mybatis-spring
          1.3.0
        
     
          org.springframework
          spring-jdbc
          4.1.3.RELEASE
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    3.创建mapper文件
    
    
    
    
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    4.创建mapper文件的代理接口
    package com.test.mapper;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    public interface ItemsMapper {
    
        public List selectItems();
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    5.创建mybatis配置文件

    mybatis.xml文件

    
    
    
    
        
               
        
    
        
        
            
        
        
    
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    6. 创建db.properties文件
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123
    
    • 1
    • 2
    • 3
    • 4
    7. 修改applicationContext-ioc.xml文件

    ​ 将之前在mybatis文件中配置的数据源 交给spring来管理,并创建sqlSessionFactory对象

    
    
    
        
        
    
        
        
    
        
        
            
            
            
            
        
    
        
        
            
            
        
    
    
    
    
    • 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
    8.修改dao层
    package com.test.dao.impl;
    
    import com.test.dao.IItemsDao;
    import com.test.mapper.ItemsMapper;
    import com.test.pojo.Items;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.io.InputStream;
    import java.util.List;
    
    @Component
    public class ItemsDao implements IItemsDao {
        
        @Autowired
        private SqlSessionFactory sqlSessionFactory;
    
        public ItemsDao()
        {
          
        }
    
        public List selectItems()
        {
            SqlSession sqlSession= sqlSessionFactory.openSession();
    
            ItemsMapper itemsMapper= sqlSession.getMapper(ItemsMapper.class);
    
            List itemsList= itemsMapper.selectItems();
    
            return itemsList;
        }
    }
    
    
    • 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
    9.在pom.xml文件中添加mapper.xml文件的路径

    在build标签中添加

     
     
       
       src/main/java
       
         **/*.xml
       
       true
     
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    
    
    • 1
    10.测试
    @Test
    public void fun()
    {
        ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/applicationContext-ioc.xml");
    
        IItemsService itemsService= classPathXmlApplicationContext.getBean("itemsService",IItemsService.class);
    
         System.out.println(itemsService.selectItems());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三 给mapper接口创建代理,替代原dao层的功能

    ​ 1.在applicationContext-ioc.xml文件中 添加

    
    
        
    
    
    • 1
    • 2
    • 3
    • 4
    1. 修改service代码

    将之前的

    @Autowired
    private IItemsDao itemsDao;
    
    • 1
    • 2

    修改为

    @Autowired
    private ItemsMapper itemsMapper;
    
    • 1
    • 2

    ​ 完整代码如下

    package com.test.service.impl;
    
    import com.test.dao.IItemsDao;
    import com.test.mapper.ItemsMapper;
    import com.test.pojo.Items;
    import com.test.service.IItemsService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    
    @Service
    public class ItemsService implements IItemsService {
    
    
        @Autowired
        private ItemsMapper itemsMapper;
    
        public List selectItems()
        {
            return itemsMapper.selectItems();
        }
    }
    
    • 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
    1. 原来的dao层可以删除

    2. 测试代码不变

  • 相关阅读:
    【发表案例】IF6.5+,中科院2区,2个月录用,6天见刊!
    安装配置ingress-nginx支持https访问
    Effective Java学习笔记---------枚举和注解
    jQuery中attr()、prop()、data()用法及区别
    【Docker】Docker学习之一:离线安装Docker步骤
    基于NVIDIA的deepstream进行串行多任务模型开发,DeepStream 多模型组合检测(精)
    晚期非小细胞肺癌肿瘤异质性和微环境的单细胞分析(Nature Communication, 2021年5月5日)
    模块语法笔记
    新知识:去掉li前面的项目符号(小圆点)
    Go语言学习笔记——错误处理
  • 原文地址:https://blog.csdn.net/daimenglaoshi/article/details/127581046