Spring4快速入门第五章操作数据库

之前有讲过jdbc操作mysql数据库http://blog.csdn.net/qq_19558705/article/details/49947317,现在我们聊聊spring4是如何操作数据库的

项目是maven版本的,在spring快速入门第一章HelloWorld有需要的jar包,但jdbc的操作还需要添加spring-jdbc的jar包:

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>


创建User表:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

配置连接数据库:

db.properties文件:

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
#jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test,如果是localhost:3306是可以省略不写的,初学者建议不要偷懒
jdbc.jdbcUrl=jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</span>
c3p0连接池:

<!-- 配置c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	</bean>
测试是否连接成功

@Test
	public void C3p0Connection() throws SQLException{
		DataSource dataSource = (DataSource) ctx.getBean("dataSource");
		System.out.println("connection : "+dataSource.getConnection());
	}
connection : com.mchange.v2.c3p0.impl.NewProxyConnection@11c413d


要操作数据库就要想配置JdbcTemplate

<!-- 配置 Spirng 的 JdbcTemplate -->
	<bean id="jdbcTemplate" 
		class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>


crud测试方法:
package com.spring.test;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.spring.anno.controller.UserController;
import com.spring.anno.repository.UserRepository;
import com.spring.anno.service.UserService;
import com.spring.entity.Address;
import com.spring.entity.Company;
import com.spring.entity.Main;
import com.spring.entity.Person;
import com.spring.entity.Position;
import com.spring.entity.User;

public class BeanTest {
	
	private ApplicationContext ctx = null;
	private JdbcTemplate jdbcTemplate = null;
	
	{
		ctx = new ClassPathXmlApplicationContext("beans.xml");
		jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
	}
	
	@Test
	public void saveUser(){
		String sql = "insert into user (name,password) values(?,?)";
		jdbcTemplate.update(sql, "ITDragon","123456789");
	}
	
	@Test
	public void updateUser(){
		String sql = "update user set name = ? , password = ? where id = ?";
		jdbcTemplate.update(sql, "ITDragon","987654321",2);
	}
	
	@Test
	public void deleteUser(){
		String sql = "delete from user where id = ?";
		jdbcTemplate.update(sql,2);
	}
	
	@Test
	public void saveUsers(){
		String sql = "insert into user (name,password) values(?,?)";
		List<Object[]> users = new ArrayList<Object[]>();
		users.add(new Object[]{"ITDragon","123456789"});
		users.add(new Object[]{"ITDragon1","1234567891"});
		users.add(new Object[]{"ITDragon2","1234567892"});
		jdbcTemplate.batchUpdate(sql, users);
	}
	
	@Test
	public void updateUsers(){
		String sql = "update user set name = ? , password = ? where id = ?";
		List<Object[]> users = new ArrayList<Object[]>();
		users.add(new Object[]{"ITDragon0","123456789",3});
		users.add(new Object[]{"ITDragon01","1234567891",4});
		users.add(new Object[]{"ITDragon02","1234567892",5});
		jdbcTemplate.batchUpdate(sql, users);
	}
	
}
因为没有创建实体类,所以不好显示查看用户,原理是一样的,后面两个测试方法是批量操作,以上代码都是读者亲测可用的,大家放心使用吧。


下一章就可以spring的简单实训案例微笑

欢迎各位转载我的博客,但希望转载时注明博客来源。一点点成长,一点点优秀。如果有什么建议和疑问可以留言。






你可能感兴趣的:(spring,jdbc)