spring mysql_eclipse中spring访问mysql的简易实现-阿里云开发者社区

本文介绍如何在Eclipse中构建一个基于Maven的Spring项目,使用TestNG进行测试,实现访问MySQL数据库。主要涉及Java源码、测试源码、Spring配置文件和POM配置文件的详细内容,以及数据库连接配置和远程访问权限设置。
摘要由CSDN通过智能技术生成

eclipse中构建一个maven基础上的带有TestNG测试插件的spring项目

目录结构如下所示:

67014233d2ad1071698b3934195b0785.png

这个简易的实现,包括四个文件

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配置文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

classpath:/org/springframework/beans/factory/xml/spring-beans-4.0.xsd

http://www.springframework.org/schema/context

classpath:/org/springframework/context/config/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">

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="password" />

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dsmysql" />

p:dataSource-ref="dsmysql" />

4.maven配置文件

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

infotech.com

ruku

war

0.0.1-SNAPSHOT

ruku Maven Webapp

http://maven.apache.org

junit

junit

4.12

org.springframework

spring-context

4.3.11.RELEASE

net.sf.ucanaccess

ucanaccess

4.0.1

org.springframework

spring-jdbc

4.3.11.RELEASE

mysql

mysql-connector-java

8.0.7-dmr

org.testng

testng

6.8

test

org.springframework

spring-test

4.3.11.RELEASE

test

org.springframework

spring-core

4.3.11.RELEASE

commons-dbcp

commons-dbcp

1.4

org.springframework

spring-beans

4.3.11.RELEASE

ruku

上述几个文件的逻辑关系如下图:

da77126f9a080f1d538dd9e2d7aa0a28.png

上述spring配置文件中负责数据库连接的部分,url中使用的localhost而不是ip地址。如果你全新安装了mysql那么默认情况下是无法使用ip地址进行远程访问或者甚至本地访问的。

需要给mysql开通远程访问的权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值