zoukankan      html  css  js  c++  java
  • spring+mybatis

    闲话少叙,自由自在

    项目简单说明:为了学习springboot特性,特意搭建spring用户改造springboot,项目采用简单的mybatis模式+配置文件方式。

    项目目录结构:

     代码详细介绍:

    spring 配置文件:applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            ">
    
        <!-- 配置组件包扫描的位置 -->
        <context:component-scan base-package="com.adao" />
        
        <!-- 读取db.properties配置文件到Spring容器中 -->
        <context:property-placeholder location="classpath:db.properties" />
        
        <!-- 配置service的实现类 -->
        <bean name="teaminalService" class="com.adao.service.impl.TeaminalServiceImpl"></bean>
        
        <!-- 配置 阿里巴巴的 druid 数据源(连接池) -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
            destroy-method="close">
            <!-- 基本属性 url、user、password -->
    <!--     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@192.168.128.59:1521:ORCL" />
            <property name="username" value="coll_platform" />
            <property name="password" value="coll_platform" /> -->
            <!-- 基本属性 url、user、password -->
            <property name="driverClassName" value="${db.driverClassName}" />
            <property name="url" value="${db.url}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="${db.password}" />
            
            <property name="initialSize" value="5" />
            <property name="minIdle" value="1" />
            <property name="maxActive" value="50" />
            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="60000" />
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="300000" />
            <property name="validationQuery" value="SELECT 1 FROM DUAL" />
            <property name="testWhileIdle" value="true" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true" />
            <property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
            <property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小,mysql 不使用 -->
            <property name="poolPreparedStatements" value="true" />
            <!-- 配置监控统计拦截的filters -->
            <property name="filters" value="stat" />
            <!-- 慢查询sql打印 -->
            <property name="connectionProperties" value="druid.stat.slowSqlMillis=100" />
    
        </bean>
        
            <!-- sqlSessionFactory给spring托管 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 别名,类名开头变小写 -->
            <property name="typeAliasesPackage"  value="com.adao.pojo"></property>
            <!-- sql映射文件路径 -->
            <!-- classpath *和不加*的区别 -->
            <property name="mapperLocations" value="classpath*:com/adao/mapper/*Mapper.xml"></property>
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 配置SqlSessionFactoryBean的名称 -->
            <property name="basePackage" value="com.adao.mapper" />
            <!-- 可选,如果不写,Spring启动时候。容器中。自动会按照类型去把SqlSessionFactory对象注入进来 -->
            <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
    </beans>

    数据库配置文件:db.properties

    #db config
    db.driverClassName=oracle.jdbc.driver.OracleDriver
    db.url=jdbc:oracle:thin:@192.168.128.59:1521:ORCL
    db.username=coll_platform
    db.password=coll_platform

    实体类: pojo.Teaminal

    package com.adao.pojo;
    
    /**
     * 终端档案
     * 
     */
    public class Teaminal {
    
        private long rtuId; // 终端ID
    
        private long rtuAddress;// 终端地址
    
        public long getRtuId() {
            return rtuId;
        }
    
        public void setRtuId(long rtuId) {
            this.rtuId = rtuId;
        }
    
        public long getRtuAddress() {
            return rtuAddress;
        }
    
        public void setRtuAddress(long rtuAddress) {
            this.rtuAddress = rtuAddress;
        }
    
    }

    mapper.TeaminalMapper

    package com.adao.mapper;
    
    import java.util.List;
    
    import com.adao.pojo.Teaminal;
    
    /**
     * 终端mapper
     * 
     */
    
    public interface TeaminalMapper {
        
        /**
         * 根据ID取终端名称
         * 
         * @param terId
         * @return
         */
        public String getTerNameById(long terId);
        
        public List<Teaminal> list();
    
        
    }

    mapper.TeaminalMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
    
    <mapper namespace="com.adao.mapper.TeaminalMapper">
        <!-- 根据ID取终端名称 -->
        <select id="getTerNameById" resultType="String">
            select TERMINAL_NAME
            from T_TERMINAL  where TERMINAL_ID=#{terId}
        </select>
    
        <select id="list" resultType="Teaminal">
            select * from T_TERMINAL
        </select>    
        
    </mapper>

    service:TeaminalService

    package com.adao.service;
    
    import java.util.List;
    
    import com.adao.pojo.Teaminal;
    
    public interface TeaminalService {
    
        public List<Teaminal> list();
        
        public String getTerNameById();
    
    }

    service:TeaminalServiceImpl

    package com.adao.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.adao.mapper.TeaminalMapper;
    import com.adao.pojo.Teaminal;
    import com.adao.service.TeaminalService;
    
    
    //@Service
    public class TeaminalServiceImpl implements TeaminalService {
     
        /*
         *     问题1 : UserMapper是接口,如何创建对象?
         *     答: 使用MyBatis的SqlSession 会话对象。
         * 
         *    问题2:SqlSession如何创建?
         *    答: 使用MyBatis的SqlSessionFactory 工厂对象创建
         *
         *    问题3:SqlSessionFactory工厂对象如何创建?
         *
         *    答:在使用Spring之前
         *    开发者自己写代码读取MyBatis配置文件,创建SqlSessionFactory
         *    使用Spring之后,让Spring框架帮我们创建
         *
         *    问题4:SqlSessionFactory 工厂对象Spring框架如何创建出来?
         *
         *    答:MyBatis工厂对象创建的类在 MyBatis框架和Spring集成的桥梁包mybatis-spring-1.3.1.jar
         *      的org.mybatis.spring.SqlSessionFactoryBean 负责创建工厂对象
         *        开发者只需要在Spring配置配置此类就可以创建出来SqlSessionFactory对象 
         * 
         */
         @Autowired
            private TeaminalMapper teaminalMapper;
    
            @Override
            public List<Teaminal> list(){
                 System.out.println("teaminalMapper : " + teaminalMapper);
                return teaminalMapper.list();
            }
            
            @Override
            public String getTerNameById(){
    
                long terId= Long.parseLong("1437640452317");
                String teaminalName =teaminalMapper.getTerNameById(terId);
                System.out.println(teaminalName);
                return teaminalName;
            }
    
    }

    main  测试类

    package com.adao;
    
    import java.util.List;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.adao.pojo.Teaminal;
    import com.adao.service.TeaminalService;
    
    /**
     * 
     * 
     * @author adao 2020-06-20
     */
    public class test01 {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            TeaminalService teaminalService = ac.getBean(TeaminalService.class);// 单例才能这样传类类型
            System.out.println("teaminalService : " + teaminalService);
            List<Teaminal> list = teaminalService.list();
            System.out.println(list.size());
    
            String teaminalName = teaminalService.getTerNameById();
            System.out.println(teaminalName);
        }
    }

    数据库数据:

    CREATE TABLE "COLL_PLATFORM"."T_TERMINAL"
    ( "TERMINAL_ID" NUMBER(14,0) NOT NULL ENABLE,
    "TERMINAL_ADDRESS" NUMBER(16,0) NOT NULL ENABLE,
    "TERMINAL_NAME" VARCHAR2(60) NOT NULL ENABLE,
    ) SEGMENT CREATION IMMEDIATE
    PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
    STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
    PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
    TABLESPACE "USERS"

     

    执行结果:

  • 相关阅读:
    第06组Alpha冲刺(4/6)
    第06组Alpha冲刺(3/6)
    第06组Alpha冲刺(2/6)
    第06组 Alpha冲刺 (1/6)
    08-js函数
    07-数组
    06-js分支
    05-js运算符
    04-js变量
    03-css3D转换
  • 原文地址:https://www.cnblogs.com/adao21/p/13167864.html
Copyright © 2011-2022 走看看