• spring和jdbc结合的一个小例子


    1.新建一个SpringJdbc的maven项目。
    2.引入需要的jar包的依赖

    <span style="white-space:pre">		</span><dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.8</version>
    			<scope>test</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>commons-logging</groupId>
    			<artifactId>commons-logging</artifactId>
    			<version>1.2</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-expression</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjweaver</artifactId>
    			<version>1.8.8</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjrt</artifactId>
    			<version>1.8.8</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>4.2.5.RELEASE</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.38</version>
    		</dependency>

    3.在src/java/resources下面新建一个jdbcApplication.xml

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

    4.新建一个接口,JdbcService(这里就不使用dao层了),里面有个save方法


    5.写个实现类,JdbcServiceImpl(这里首先要建好数据库),然后注入JbcTemplate


    6.在xml中配置相关资源

    ①配置dataSource,这里使用的是Spring提供的DriverManagerDataSource

    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath:resources.properties</value>
    			</list>
    		</property>
    	</bean>
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${driverClassName}"></property>
    		<property name="url" value="${url}"></property>
    		<property name="username" value="${username}"></property>
    		<property name="password" value="${password}"></property>
    	</bean>

    这种是使用读取配置文件获取信息的方式,也可以直接写这些参数

    resources.properties

    <span style="white-space:pre">	</span>driverClassName:com.mysql.jdbc.Driver
    <span style="white-space:pre">	</span>url:jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
    <span style="white-space:pre">	</span>username:root
    <span style="white-space:pre">	</span>password:root
    ②配置jdbcTemplate

    <span style="white-space:pre">	</span><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    ③配置jdbcService

    	<bean id="jdbcService" class="com.qiao.springjdbc.JdbcServiceImpl">
    		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
    	</bean>

    7.写测试类

    运行后,数据库会多出一天数据


    就表示成功了。



  • 相关阅读:
    libcurl库进行http通讯-开篇
    hdu4059The Boss on Mars 容斥原理
    “最大子序列和”算法 java
    Android 的事件传递机制,详解
    POJ
    hdu 5652 India and China Origins
    解决Swap file &quot;.ceshi.c.swp&quot; already exists!问题
    3DMax脚本插件--改动材质&amp;贴图名称
    ASP.NET MVC+Bootstrap 实现短信验证
    [从头学数学] 第176节 概率
  • 原文地址:https://www.cnblogs.com/Iqiaoxun/p/5350580.html
Copyright © 2020-2023  润新知