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("成功了!");
}
}