• javaee spring整合mybatis


    案例一

    包含dao层

    创建maven webapp项目

    maven仓库需要改为阿里云
    在这里插入图片描述

    引入依赖

    
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0modelVersion>
    
      <groupId>org.examplegroupId>
      <artifactId>testSSMartifactId>
      <version>1.0-SNAPSHOTversion>
      <packaging>warpackaging>
    
      <name>testSSM Maven Webappname>
      
      <url>http://www.example.comurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-contextartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-beansartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-context-supportartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.37version>
        dependency>
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.4.6version>
        dependency>
        <dependency>
          <groupId>com.mchangegroupId>
          <artifactId>c3p0artifactId>
          <version>0.9.5.2version>
        dependency>
    
        
        <dependency>
          <groupId>log4jgroupId>
          <artifactId>log4jartifactId>
          <version>1.2.17version>
        dependency>
    
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>1.3.0version>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>4.3.18.RELEASEversion>
        dependency>
    
      dependencies>
    
      <build>
        <resources>
          <resource>
            
            <directory>src/main/javadirectory>
            <includes>
              <include>**/*.xmlinclude>
            includes>
            <filtering>truefiltering>
          resource>
        resources>
        <finalName>testSSMfinalName>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-clean-pluginartifactId>
              <version>3.1.0version>
            plugin>
            
            <plugin>
              <artifactId>maven-resources-pluginartifactId>
              <version>3.0.2version>
            plugin>
            <plugin>
              <artifactId>maven-compiler-pluginartifactId>
              <version>3.8.0version>
            plugin>
            <plugin>
              <artifactId>maven-surefire-pluginartifactId>
              <version>2.22.1version>
            plugin>
            <plugin>
              <artifactId>maven-war-pluginartifactId>
              <version>3.2.2version>
            plugin>
            <plugin>
              <artifactId>maven-install-pluginartifactId>
              <version>2.5.2version>
            plugin>
            <plugin>
              <artifactId>maven-deploy-pluginartifactId>
              <version>2.8.2version>
            plugin>
          plugins>
        pluginManagement>
      build>
    project>
    
    
    • 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

    spring配置文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    
        
        <context:component-scan base-package="com.test" />
    
        
        <context:property-placeholder location="classpath:db.properties" />
    
        
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driverClass}" />
            <property name="jdbcUrl" value="${url}"/>
            <property name="user" value="${user}" />
            <property name="password" value="${password}" />
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="comboPooledDataSource" />
            <property name="configLocation" value="classpath:sqlMapConfig.xml" />
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <property name="basePackage" value="com.test.mapper" />
        bean>
    
    beans>
    
    • 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

    数据库信息

    driverClass=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/itstar
    user=itstar
    password=yyy123456
    
    • 1
    • 2
    • 3
    • 4

    mybatis配置文件

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        
        
        
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING"/>
        settings>
    
        
        <typeAliases>
            <package name="com.test.pojo" />
        typeAliases>
    
        
        
            
                
                
                    
                    
                    
                    
                
            
        
    
         
        
           
        
    
    configuration>   
    
    • 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

    dao层

    package com.test.dao;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    public interface IItemsDao {
    
        public List<Items> selectItems();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    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.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.List;
    
    //实现类
    @Component
    public class ItemsDao implements IItemsDao {
    
        @Autowired
        private SqlSessionFactory sqlSessionFactory;
    
        public SqlSessionFactory getSqlSessionFactory() {
            return sqlSessionFactory;
        }
    
        public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
            this.sqlSessionFactory = sqlSessionFactory;
        }
    
        //转移到Spring的配置文件中  让Spring创建SqlSessionFactory对象
    //    public ItemsDao()
    //    {
    //        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("sqlMapConfig.xml");
    //
    //        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
    //
    //        this.sqlSessionFactory= sqlSessionFactoryBuilder.build(inputStream);
    //    }
    
        //查询所有的商品信息
            @Override
            public List<Items> selectItems() {
    
            SqlSession sqlSession= sqlSessionFactory.openSession();
    
            ItemsMapper itemsMapper= sqlSession.getMapper(ItemsMapper.class);
    
            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
    • 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

    mapper

    package com.test.mapper;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    //创建一个接口  代理ItemsMapper.xml文件
    public interface ItemsMapper {
    
        public List<Items> selectItems();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    DOCTYPE mapper
                    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.test.mapper.ItemsMapper">
    
        
        <select id="selectItems" resultType="Items">
            select * from items
        select>
    
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    service层

    package com.test.service;
    
    import com.test.pojo.Items;
    
    import java.util.List;
    
    public interface IItemsService {
    
        public List<Items> selectItems();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.test.service.impl;
    
    import com.test.dao.IItemsDao;
    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;
    
    //service层的实现类
    
    @Service
    public class ItemsService implements IItemsService {
    
        @Autowired
        private IItemsDao itemsDao;
    
        public IItemsDao getItemsDao() {
            return itemsDao;
        }
    
        public void setItemsDao(IItemsDao itemsDao) {
            this.itemsDao = itemsDao;
        }
    
        @Override
        public List<Items> 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
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    测试类

    package com.test.service;
    
    import com.test.pojo.Items;
    import com.test.service.impl.ItemsService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import java.util.List;
    
    public class TestItemsService {
    
        @Test
        public void test()
        {
            //测试service层的查询方法
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
            IItemsService itemsService= applicationContext.getBean("itemsService",IItemsService.class);
    
            List<Items> itemsList= itemsService.selectItems();
    
            System.out.println(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

    案例二

    省略dao层,由spring代替

    在案例一的基础上,修改spring配置文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    
        
        <context:component-scan base-package="com.test" />
    
        
        <context:property-placeholder location="classpath:db.properties" />
    
        
        <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driverClass}" />
            <property name="jdbcUrl" value="${url}"/>
            <property name="user" value="${user}" />
            <property name="password" value="${password}" />
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="comboPooledDataSource" />
            <property name="configLocation" value="classpath:sqlMapConfig.xml" />
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
             <property name="basePackage" value="com.test.mapper" />
        bean>
    
    beans>
    
    • 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

    删除dao层

    修改service层

    package com.test.service.impl;
    
    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;
    
    //service层的实现类
    
    @Service
    public class ItemsService implements IItemsService {
    
        //spring会帮我们创建一个代理对象( 实现了ItemsMapper接口的),代理对象代理的是dao层的功能
        @Autowired
        private ItemsMapper itemsMapper;
    
        public ItemsMapper getItemsMapper() {
            return itemsMapper;
        }
    
        public void setItemsMapper(ItemsMapper itemsMapper) {
            this.itemsMapper = itemsMapper;
        }
    
        @Override
        public List<Items> 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    KNN(K近邻)水仙花的分类(含答案)
    mysql操作 sql语句中的完整性约束有哪些,主键约束、外键约束、引用完整性约束,主键外键、唯一性
    嵌入式Linux_驱动开发:offset宏和container_of宏
    【译】Visual Studio 中的 GitHub Copilot:2023年回顾
    vue 插槽-默认插槽
    Centos系统上nginx安装
    常用工具类之使用easy-captcha生成验证码
    uview indexList 按字母跳转不了
    Springboot项目全局异常处理
    html实现计算器源码
  • 原文地址:https://blog.csdn.net/Rockandrollman/article/details/133578749