一.对数据库操作需要引入的jar包
二.配置xml文件
这里需要一层一层的注入
1.拿到dataSource
需要注意的是,这里加载驱动是driverClassName,而不是driver
& 在xml配置都是 &;
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8" />
2.将dataSource注入jdbcTemplate
3.将jdbcTemplate注入dao层
4.将dao层注入service层
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--在xml中获取数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--拿到连接数据库的信息-->
<property name="url" value="jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8" />
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--spring 给我们提供操作数据库的类-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置DAO层-->
<bean id="accountDao" class="com.offcn.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--配置service层-->
<bean id="accountService" class="com.offcn.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
</beans>
三.dao层实现类
需要注意的是:
只有查询才需要用到RowMapper这个对象,它返回了是每行得到的数据,使用的是jdbcTemplate(spring提供操作数据库的类)的query方法(queryForObject查询一个,返回的是一行信息的对象;query查询所有,返回的是一个集合)
增删改都
pjavaackage com.offcn.dao.impl;
import com.offcn.dao.AccountDao;
import com.offcn.entity.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
//spring提供操作数据库的类
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/*c查询单个*/
@Override
public Account selectById(int aid) {
String sql = "select * from account where aid=?";
Object[] objects = {aid};
/*返回的每一行数据都在这个类里,第一个参数是返回的结果集,第二个人参数是当前的索引*/
RowMapper<Account> rowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
System.out.println(i+"是索引");
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
Account account1 = jdbcTemplate.queryForObject(sql, objects, rowMapper);
return account1;
}
/*查询所有*/
@Override
public List<Account> selectAll() {
String sql = "select * from account";
RowMapper<Account> rowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
List<Account> list = jdbcTemplate.query(sql, rowMapper);
return list;
}
/*修改一个*/
@Override
public int update(Account account) {
String sql = "update account set abalance=? where aid=?";
Object [] objects = {account.getAbalance(),account.getAid()};
int num = jdbcTemplate.update(sql, objects);
return num;
}
/*增加一个*/
@Override
public int save(Account account) {
String sql = "insert into account (aid,abalance)values (?,?)";
Object [] objects = {account.getAid(),account.getAbalance()};
int num = jdbcTemplate.update(sql, objects);
return num;
}
/*删除一个*/
@Override
public int delete(int aid) {
String sql = "delete from account where aid=?";
Object [] objects = {aid};
int num = jdbcTemplate.update(sql, objects);
return num;
}
}