zoukankan      html  css  js  c++  java
  • spring整合mybatis(非代理方式)【我】

    首先创建要给 maven 的war项目

    不用代理的方式:

    如果不适用Mapper代理的方式,配置就非常简单:

    首先是pom文件(以下配置文件包含其他多余内容,仅供参考):

    <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.0</modelVersion>
      <groupId>com.my</groupId>
      <artifactId>test-mybatis-war</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <spring.version>4.3.0.RELEASE</spring.version>
        </properties>
    
        <dependencies>
        
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            
            <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            
            <!-- servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <scope>provided</scope>
                <version>2.5</version>
            </dependency>
            
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <scope>provided</scope>
                <version>2.0</version>
            </dependency>
            
            <!-- jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- Redis客户端 -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.7.2</version>
            </dependency>
            
            <!-- redisson -->
             <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson</artifactId>
                <version>3.8.1</version>
            </dependency>
            
            
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>2.7.7</version>
            </dependency>
            
             <!--mybatis-spring适配器 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.0</version>
            </dependency>
            
            <!-- mybatis ORM框架 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.1</version>
            </dependency>
            
            <!--mysql数据库驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            
               <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.9</version>
            </dependency>
            
            <!--log4j日志包 -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.6.1</version>
            </dependency>
            
            <!-- poi -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.8</version> 
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.8</version> 
            </dependency>
            
        </dependencies>
    
    </project>

    然后是项目结构:

    主要就是红框中的部分,

    配置文件:

    首先数据库的 db.properties:

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/test1?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123

    然后是mybatis的核心配置文件 SqlMapConfig.xml :

    <?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>
        <settings>
            <setting name="logImpl" value="STDOUT_LOGGING" />
        </settings>
        
            <!-- 配置分页插件 -->
    <!--     <plugins>
            <plugin interceptor="com.github.pagehelper.PageHelper">
                设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库        
                <property name="dialect" value="mysql"/>
            </plugin>
        </plugins> -->
    </configuration>

    因为使用spring整合,所以,其非常简单,基本是空的,但是要有。

    然后是 spring的配置文件:

    1、核心dao文件 applicationContext-dao.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        <!-- 加载配置文件 -->
        <context:property-placeholder location="classpath:resource/*.properties" />
        
        <!-- 引入其他xml -->
        <import resource="classpath:spring/applicationContext-service.xml"/>
        
        
        <!-- 数据库连接池 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
            destroy-method="close">
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="maxActive" value="10" />
            <property name="minIdle" value="5" />
        </bean>
        
        <!-- 配置sqlsessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
            <property name="dataSource" ref="dataSource"></property>
            
            <!-- sql映射文件路径(是否用代理方式都可以在这配置xml文件路径) -->
    <!--         <property name="mapperLocations" value="classpath*:com/zhangguo/bookstore/mapper/*Mapper.xml"></property> -->
            <property name="mapperLocations" value="classpath*:mybatis/mapper/*.xml"></property>
        </bean>
        
    </beans>

    可见,如果不适用代理,非常简单,只需要配置  数据源、mybatis的sql工厂,mybatis使用sql的xml文件路径即可。

    2、service的扫描包配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        <!-- 扫描包 加载 service实现类 -->
        <!-- <context:component-scan base-package="com.taotao.service"> -->
        <context:component-scan base-package="mybatis.service">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    </beans>

    最后就是代码了:

    先看 xml 文件: T1.xml

    <?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">
    <!-- 这个xml是非代理方式使用的(且入参出参都不用pojo) -->
    
    
    <mapper namespace="T1">
     <!--      查询已处理的客户数 -->    
        <select id="getBookById" parameterType="java.util.HashMap" resultType="java.util.Map">
              SELECT * FROM books WHERE id =  #{id};
         </select>
        
        
    </mapper>

    超简单,里面不用任何 pojo,入参出参都可以用map。

    java代码:

    定义一个公共的父 service : ParentService

    package mybatis.service;
    
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    
    
    public abstract class ParentService {
        
        @Autowired
        private SqlSessionFactory sqlSessionFactory;
    
        protected <T> T selectOne(String statement,Object parameter) {
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectOne(statement, parameter);
        }
    
    
        protected <E> List<E> selectList(String statement,Object parameter) {
            return sqlSessionFactory.openSession().selectList(statement, parameter);
        }
    
    }

    业务service :  T1Service

    package mybatis.service;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class T1Service extends ParentService{
    
        public void getBookById() {
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put("id", 1);
            Map reult = this.selectOne("T1.getBookById", param);
            System.out.println(reult);
    
        }
    }

    测试类: Test

    package mybatis.service;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
             ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
             
             //非代理的方式
             T1Service  t1Service =ctx.getBean(T1Service.class);
             t1Service.getBookById();
        }
    }
  • 相关阅读:
    XDebug的配置和使用
    PHP一致性hash
    命令注入绕过技巧总结
    Aireplay-ng 6 种常用攻击模式详解
    CDlinux无线审计工具使用
    Aircrack-ng无线审计工具使用
    Ubuntu中的mysql
    Centos安装python3.7时遇到的问题
    写程序的时候发现了个数学在线工具,感觉挺好,Gegebra
    OpenCV实现图像变换(python)-仿射变换原理
  • 原文地址:https://www.cnblogs.com/libin6505/p/10900599.html
Copyright © 2011-2022 走看看