閱讀1011 返回首頁    go 技術社區[雲棲]


eclipse中spring訪問mysql的簡易實現

eclipse中構建一個maven基礎上的帶有TestNG測試插件的spring項目
目錄結構如下所示:
_

這個簡易的實現,包括四個文件
1.java源碼(訪問數據庫代碼)
2.測試源碼(TestNG測試插件的代碼)
3.spring配置文件(讓spring起作用)
4.pom配置文件(基於maven構建項目)

下麵羅列一下四個文件的代碼:

1.java源碼

package com.infotech.access.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.infotech.access.domain.CheckedList;

@Repository
public class CheckedlistDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * @param skostl
     * @return
     */
    public String getData(String smname) {

        final String sr =null;

        jdbcTemplate.query("select * from ruku_log where SN =?", new Object[] { smname }, new RowCallbackHandler() {

            public void processRow(ResultSet rs) throws SQLException {
              String sr2 = rs.getString("logcode");
            }

        });

        return sr;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

2.測試源碼

package com.infotech.access.dao;

import java.util.Date;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.infotech.access.domain.CheckedList;
import static org.testng.Assert.*;

@ContextConfiguration("classpath*:/ruku-context.xml")
public class CheckedlistDaoTest extends AbstractTransactionalTestNGSpringContextTests{

    private CheckedlistDao cld;

    @Autowired
    public void setCheckedlistDao(CheckedlistDao cldlocal){
        this.cld = cldlocal;
    }

    @Test
    public void testAcess() {

        String sb1 = this.cld.getData("1");
        System.out.println(sb1);

    }

}

3.spring配置文件


<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="https://www.springframework.org/schema/beans"  
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"  xmlns:p="https://www.springframework.org/schema/p"
    xmlns:context="https://www.springframework.org/schema/context"
    xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx"
    xsi:schemaLocation="https://www.springframework.org/schema/beans
        classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd     
        https://www.springframework.org/schema/context
        classpath:/org/springframework/context/config/spring-context-4.0.xsd
        https://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        https://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx-4.0.xsd">     

    <!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 -->
    <context:component-scan base-package="com.infotech.access.dao"/>

    <!-- 配置ucanaccess數據源 -->
    <bean  
        destroy-method="close" 
        p:driverClassName="com.mysql.cj.jdbc.Driver"
        p:url="jdbc:mysql://localhost:6062/ruku_v1?serverTimezone=GMT"
        p:username="root"
        p:password="piner789" />

    <!-- 配置事務管理器 -->
    <bean 
        
        p:dataSource-ref="dsmysql" />

    <!-- 配置JdbcTemplate  -->
    <bean  
        p:dataSource-ref="dsmysql" />

</beans>  

4.maven配置文件

最後更新:2017-09-27 12:03:46

  上一篇:go  點對點交易係統開發,數字資產點對點交易係統開發成趨勢
  下一篇:go  做一個網站一般需要多少費用