阅读1011 返回首页    go 阿里云 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  做一个网站一般需要多少费用