spring通过jdbc连接数据库

首先看下整个工程的架构目录:

需要的jar包:


一、建表

create table student(
	id int primary key auto_increment,
	name varchar(32),
	age int,
	phone varchar(32)
);
二、新建与数据库对应JavaBean

package com.etoak.bean;

public class Student {
	/**
	 * 一个标准的javaBean对象 : 
	 * 		表字段对应的属性
	 * 		属性对应的getter、setter方法
	 * 		无参构造器
	 * 		除id[主键]之外其他参数组成的构造器
	 * 		所有参数组成的构造器
	 */

	private Integer id;
	private String name;
	private Integer age;
	private String phone;
	public Student() {
		super();
	}
	public Student(String name, Integer age, String phone) {
		super();
		this.name = name;
		this.age = age;
		this.phone = phone;
	}
	public Student(Integer id, String name, Integer age, String phone) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.phone = phone;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
}

三、spring的applicationContext配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd">
		
	<!-- 
		使用spring提供的整合jdbc功能
		需要导入DAO层提供的两个jar包[spring-jdbc spring-tx]
		
		通过ioc依赖注入 将JdbcTemplate注入给StuDaoImpl
	 -->
	<bean id="dao" class="com.etoak.dao.StuDaoImpl">
		<!-- 
			name="jt"   setJt(JdbcTemplate jt)
			ref="jt"    id="jt"
			自定义对象  ref=""
		 -->
		<property name="jt" ref="jt"></property>
	</bean>
	
	<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="ds"></property>
	</bean>
	<!-- 
		此时的JdbcTemplate还不具备数据库连接能力
		为了让其具备数据库连接能力,需要为其提供DataSource 连接池、数据源
			setDataSource(DataSource ds)
		需要在ioc容器中再配置一个DataSource对象:
			driverClassName
			url
			username
			password
			maxIdle
			maxActive
			maxWait
		
		DataSource 接口
			1 实现类   
				BasicDataSource   commons-dbcp.jar
				spring框架自带了DataSource实现类
				DriverManagerDataSource
					setDriverClassName(String driver)
					setUrl(String url)
					setUsername(String u)
					setPassword(String p)
				[
					ref属性 : 表示调用该方法需要注入的数据类型 : 自定义类型/引用类型
					value属性 : 表示调用该方法需要注入的数据类型 : 基本数据类型/String类型/Class类型
				]  
			2 工厂bean
	 -->
	<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/etoak"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
	</bean>

</beans>
四、编写Dao

package com.etoak.dao;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.etoak.bean.Student;

/**
 * 使用jdbc方式对student表数据进行CRUD操作
 * 		1 传统的jdbc开发方式 [ConFactory ...]
 * 		2 spring提供的整合方案   JdbcTemplate
 */
public class StuDaoImpl {
	
	private JdbcTemplate jt;
	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
	
	/**
	 * JdbcTemplate将连接数据库执行添加操作的流程封装在其update(sql)
	 */
	public boolean addStu(Student stu){
		String sql = "insert into student values(null,?,?,?)";
		Object[] args = {stu.getName() , stu.getAge() , stu.getPhone()};
		int result = jt.update(sql , args);
		// result 执行当前操作影响的数据量
		return result==1;
	}
	
	public boolean delStuById(Integer id){
		String sql = "delete from student where id="+id;
		return jt.update(sql)==1;
	}
	public boolean updateStu(Student stu){
		String sql = "update student set name=?,age=?,phone=? where id=?";
		Object[] args = {stu.getName() , stu.getAge() , stu.getPhone() , stu.getId()};
		return jt.update(sql , args)==1;
	}

	/**
	 * jt.queryForMap(sql) - Map 
	 * 		Jdbc不是ORM工具,不知道sql查询的对应哪个对象
	 * 		只能将查询出的关系型数据封装在一个Map集合中返回
	 * 		{字段名=字段值,...}
	 * 		map.get("id/name/age/phone") 
	 * 注意 : 
	 * 		在使用queryForMap(sql)查询单条数据时
	 * 		必须能够确保根据传入的sql语句能够并且只能查询出单条数据
	 * 		否则使用该方法会抛出异常
	 */
	public Map selStuById(Integer id){
		String sql = "select * from student where id="+id;
		Map map = jt.queryForMap(sql);
		return map;
	}
	
	// List<Map>  每一个student被封装成了一个Map对象
	public List selectAllStus(){
		String sql = "select * from student";
		return jt.queryForList(sql);
	}
	
	public int selectStuCount(){
		String sql = "select count(*) from student";
		return jt.queryForInt(sql);
	}
	
	public List selectStusByPage(int start , int max){
		String sql = "select * from student limit ?,?";
		Object[] args = {start , max};
		return jt.queryForList(sql , args);
	}
}

五、测试

package com.etoak.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.etoak.bean.Student;
import com.etoak.dao.StuDaoImpl;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		StuDaoImpl dao = (StuDaoImpl)ac.getBean("dao");
		Student stu = new Student("sheldon",30,"111");
		boolean flag = dao.addStu(stu);
		System.out.println(flag);
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值