码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis学习笔记(3)—— 利用mybatis灌入假数据

时间:2016-04-26 23:51:00      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

  由于第三方厂商未能按时提供实时数据,故需要纯手动导入一些实时数据,用于统计分析。正好最近自己学习了mybatis 。因此使用mybatis 配置一个select.insert 的简单操作语句,用于灌入实时数据。

  业务表

    Ems_Standard_FormulaRelation【配置表,主要提供实时表的unitId,mediaId,standardId】

    Ems_StandardRuntime_Hour【实时表】

  准备工作

    数据库我用的是sqlserver 2008 r2

    1、在lib 中导入所需要的jar包

      技术分享

    2、在Referenced Libraries 中添加 jar包。

      技术分享

    3、配置mybatis 的config 文件

<?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>
  <!-- MyBatis针对SqlServer进行的配置  -->
  <typeAliases>
  <!-- 定义javabean -->
    <typeAlias alias="formulaRelation" type="com.authority.model.Ems_Standard_FormulaRelation"/>
    <typeAlias alias="runtimeHour" type="com.authority.model.Ems_StandardRuntime_Hour"/>
  </typeAliases>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="url" value="jdbc:sqlserver://10.18.14.221:1433;DatabaseName=ICPSystem"/>
        <property name="username" value="sa"/> 
        <property name="password" value="3333"/>
      </dataSource>
    </environment>
    </environments>
    <!-- 定义配置sql xml的文件路径 -->
      <mappers>
    <mapper resource="com/authority/mapper/formulaRelation.xml"/>
    <mapper resource="com/authority/mapper/runtimeHour.xml"/>
  </mappers>

  </configuration>

      4、bean文件

Ems_Standard_FormulaRelation:

package com.authority.model;

public class Ems_Standard_FormulaRelation {
	
	private int id;
	
	private int standardId;
	
	private String unitId;
	
	private String mediaId;
	
	private String formulaId;
	
	private String formulaText;

	@Override
	public String toString() {
		return "Ems_Standard_FormulaRelation [id=" + id + ", standardId=" + standardId + ", unitId=" + unitId
				+ ", mediaId=" + mediaId + ", formulaId=" + formulaId + ", formulaText=" + formulaText + "]";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getStandardId() {
		return standardId;
	}

	public void setStandardId(int standardId) {
		this.standardId = standardId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getMediaId() {
		return mediaId;
	}

	public void setMediaId(String mediaId) {
		this.mediaId = mediaId;
	}

	public String getFormulaId() {
		return formulaId;
	}

	public void setFormulaId(String formulaId) {
		this.formulaId = formulaId;
	}

	public String getFormulaText() {
		return formulaText;
	}

	public void setFormulaText(String formulaText) {
		this.formulaText = formulaText;
	}
	
}

Ems_StandardRuntime_Hour:

package com.authority.model;

public class Ems_StandardRuntime_Hour {
    
    private int id;
    
    private int standardId;
    
    private String unitId;
    
    private String formulaValue;
    
    public String getFormulaValue() {
        return formulaValue;
    }

    public void setFormulaValue(String formulaValue) {
        this.formulaValue = formulaValue;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStandardId() {
        return standardId;
    }

    public void setStandardId(int standardId) {
        this.standardId = standardId;
    }

    public String getUnitId() {
        return unitId;
    }

    public void setUnitId(String unitId) {
        this.unitId = unitId;
    }

    public String getMediaId() {
        return mediaId;
    }

    public void setMediaId(String mediaId) {
        this.mediaId = mediaId;
    }

    public String getCaculateTime() {
        return caculateTime;
    }

    public void setCaculateTime(String caculateTime) {
        this.caculateTime = caculateTime;
    }

    public Double getCaculateValue() {
        return caculateValue;
    }

    public void setCaculateValue(Double caculateValue) {
        this.caculateValue = caculateValue;
    }

    public int getSortIndex() {
        return sortIndex;
    }

    public void setSortIndex(int sortIndex) {
        this.sortIndex = sortIndex;
    }

    private String mediaId;
    
    private String caculateTime;
    
    private Double caculateValue;
    
    private int sortIndex;
}

      5、配置sql 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">
<mapper namespace="com.authority.model">
    <select id="getFormula" resultType="com.authority.model.Ems_Standard_FormulaRelation">
        select * from Ems_Standard_FormulaRelation
    </select>
</mapper>

    

<?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">
<mapper namespace="com.authority.model">
    <insert id="insertRuntime" parameterType="com.authority.model.Ems_StandardRuntime_Hour">
        insert into Ems_StandardRuntime_Hour (standardId,UnitId,MediaId,FormulaValue,
        CaculateValue,CaculateTime,SortIndex) VALUES (#{standardId},#{unitId},#{mediaId},
        #{formulaValue},#{caculateValue},#{caculateTime},#{sortIndex})
    </insert>
</mapper>

        6、main方法。用于提交

package com.authority.connection;

import java.io.IOException;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.authority.model.Ems_StandardRuntime_Hour;
import com.authority.model.Ems_Standard_FormulaRelation;

public class DataAccess {
	
	public static void main(String[] args) throws IOException {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		String resource = "config.xml";
		Reader reader = Resources.getResourceAsReader(resource); 	 	
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
		SqlSession session = sessionFactory.openSession();
		String sqlStatement = "com.authority.mapper.formulaRelation.getFormula";
		String insertStatement = "com.authority.mapper.runtimeHour.insertRuntime";
		@SuppressWarnings("unchecked")
		List<Ems_Standard_FormulaRelation> relationlist = session.selectList(sqlStatement);
		//单例的日期对象
		Calendar cld = Calendar.getInstance();
		//统计数据的时间单位是每小时。这个区间是10年的数据
		for (int i = 0; i < 24 * 365 * 10; i++) {
			//每次循环将当前时间的小时+1
			cld.set(Calendar.HOUR, cld.get(Calendar.HOUR)+1);	
			for (Ems_Standard_FormulaRelation obj : relationlist) {
				Ems_StandardRuntime_Hour runtime = new Ems_StandardRuntime_Hour();
				runtime.setCaculateTime(df.format(cld.getTime()));
				runtime.setCaculateValue(Math.random());
				runtime.setMediaId(obj.getMediaId());
				runtime.setStandardId(obj.getStandardId());
				runtime.setUnitId(obj.getUnitId());
				runtime.setStandardId(obj.getStandardId());
				runtime.setFormulaValue(obj.getFormulaId());
				int m = session.insert(insertStatement, runtime);
				System.out.println(m);
			}	
		}
		session.commit();
	}
}

  session.commit,我在测试过程中,必须加上这句话。否则会导致数据表为空。具体实现原理。还是需要跟源码看看的。不过mybatis 确实要比hibernate灵活度上高上不少。我个人还是喜欢比较直观的sql语句。

 

MyBatis学习笔记(3)—— 利用mybatis灌入假数据

标签:

原文地址:http://www.cnblogs.com/cklovefan/p/5436847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!