Java EE-Spring数据库开发

1、JDBC连接数据库

在这里插入图片描述

2、Spring JDBC配置

在这里插入图片描述
扩充:利用property-placeholder提取数据库配置参数
applicationContext.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context.xsd">
      <!-- 指定需要扫描的包,使注解生效 -->
      <context:component-scan base-package="cn.edu.ujn.ch4" />
      <context:property-placeholder location="jdbc.porperties"/>
      <bean id="dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">
		<!--数据库驱动 -->
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<!--连接数据库的url -->
		<property name="url" value="${jdbc.url}" />
		<!--连接数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!--连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 2配置JDBC模板 -->
	<bean id="jdbcTemplate" 
		   class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 默认必须使用数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>	
      
</beans>

jdbc.porperties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java_ee
jdbc.username=root
jdbc.password=

3、Spring JdbcTemplate的常用方法

Account.java

package cn.edu.ujn.ch4.jdbc;

public class Account {

	private int id;
	private String username;
	private double balance;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
	}
	
	
}

IAccountDao.java

package cn.edu.ujn.ch4.jdbc;

import java.util.List;

public interface IAccountDao {

	public int addAccount(Account account);
	
	public List<Account> findAllAccount();
	
}

AccountDaoImpl.java

package cn.edu.ujn.ch4.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int addAccount(Account account) {
		// TODO Auto-generated method stub
		String sql ="insert into account(username,balance) value(?,?)";
		Object[] obj=new Object[] {account.getUsername(),account.getBalance()};
		int update = this.jdbcTemplate.update(sql,obj);
		return update;
	}

	public List<Account> findAllAccount() {
		// TODO Auto-generated method stub
		return null;
	}

}

Test.java

package cn.edu.ujn.ch4.jdbc;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class Test {

	public static void main(String[] args) {
		
		String sql="create table account(id int primary key auto_increment,"
				+"username varchar(50),balance double)";
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
		jdbcTemplate.execute(sql);
		System.out.println("成功了!");
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值