spring與JDBC整合操作
spring依賴注入jdbc:
jdbc配置通過硬編碼方式實現:
首先例項類
package com.offcn.pojo;
import java.io.Serializable;
public class ACount implements Serializable {
private int id;
public int getId() {
return id;
}
public String getSname() {
return sname;
}
public double getMoney() {
return money;
}
private String sname;
private double money;
public void setId(int id) {
this.id = id;
}
public void setSname(String sname) {
this.sname = sname;
}
public void setMoney(double money) {
this.money = money;
}
}
dao介面:
package com.offcn.dao;
import com.offcn.pojo.ACount;
import java.util.List;
public interface CountDdao {
//根據id來查詢
public ACount selectCount(int id);
//插尋所有
public List<ACount> selectAll();
//修改
public int updateAcount(ACount aCount);
//增加
public int insertAcount(ACount aCount);
public void add();
}
dao實現:
package com.offcn.countdao.impl;
import com.offcn.dao.CountDdao;
import com.offcn.pojo.ACount;
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 CountDaoImpl implements CountDdao {
//操作資料庫的類
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/* @Override
public ACount selectCount(int id) {
String sql="select * from count where id=?";
Object[] objects={id};
RowMapper<ACount> rowMapper= new RowMapper<ACount>() {
@Override
public ACount mapRow(ResultSet resultSet, int i) throws SQLException {
System.out.println(i+"i");
ACount count=new ACount();
count.setId(resultSet.getInt("id"));
count.setMoney(resultSet.getDouble("money"));
return count;
}
};
ACount count1 = jdbcTemplate.queryForObject(sql,Objects,rowMapper);
return count1;
}*/
@Override
public ACount selectCount(int id) {
String sql="select * from count where id=?";
Object[] objects={id};
RowMapper<ACount> rowMapper= new RowMapper<ACount>() {
@Override
public ACount mapRow(ResultSet resultSet, int i) throws SQLException {
System.out.println(i+"i");
ACount count=new ACount();
count.setId(resultSet.getInt("id"));
count.setMoney(resultSet.getDouble("money"));
return count;
}
};
ACount object = jdbcTemplate.queryForObject(sql,objects,rowMapper);
return object;
}
@Override
public List<ACount> selectAll() {
return null;
}
//@Override
/*public List<ACount> selectAll() {
String sql="select * from count ";
RowMapper rowMapper=new RowMapper() {
@Override
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
ACount aCount=new ACount();
aCount.setId(resultSet.getInt("id"));
aCount.setMoney( resultSet.getDouble("money"));
return aCount;
}
};*/
/* List<ACount> list = jdbcTemplate.query(sql, rowMapper);
return list;
}*/
//修改
@Override
public int updateAcount(ACount aCount) {
Object[] objects={aCount.getMoney(),aCount.getSname(),aCount.getId()};
String sql="update Acount set money=? where id=?";
/*RowMapper rowMapper=new RowMapper() {
@Override
public Object mapRow(ResultSet resultSet, int i) throws SQLException {
ACount aCount=new ACount();
aCount.setId(resultSet.getInt("id"));
aCount.setMoney( resultSet.getDouble("money"));
return aCount;
};
};*/
int update = jdbcTemplate.update(sql,objects);
return update;
}
@Override
public int insertAcount(ACount aCount) {
Object[] objects={aCount.getSname(),aCount.getMoney()};
String sql = "insert into `Acount`(sname,money) values(?,?)";
//String sql="insert into count(sname,money) values(?,?)";
int update = jdbcTemplate.update(sql, objects);
return update;
}
@Override
public void add() {
}
}
xml檔案配置容器:
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<!--連線資料庫地址-->
<property name="url" value="jdbc:mysql://localhost:3306/count?useUnicode=true&characterEncoding=utf-8"></property>
<!--為連線資料庫載入驅動-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!--通過xml檔案容器屬性獲取資料庫的使用者名稱-->
<property name="username" value="root"></property>
<!--通過xml檔案容器屬性獲取資料庫的使用者名稱的密碼-->
<property name="password" value="1234"></property>
<property name="dataSource" ref="datasource"></property>
</bean>
<!--配置dao層,建立dao層物件-->
<bean id="countDao" class="com.offcn.countdao.impl.CountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--配置service層,建立service層物件-->
<bean id="countService" class="com.offcn.countservice.impl.CountServiceImpl">
<property name="countDdao" ref="countDao"></property>
</bean>
**測試:Text ** package com.offcn.test;
import com.offcn.countservice.CountService;
import com.offcn.pojo.ACount;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLOutput;
import java.util.List;
public class Text {
@Test
public void test(){
//查詢一個
/*ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
CountService count1 = (CountService) app.getBean("countService");
ACount count = count1.selectCount(1);
System.out.println(count);*/
//查詢所有
/*ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
CountService count1 = (CountService) app.getBean("countService");
List<ACount> list = count1.selectAll();
for (ACount a:list
) {
System.out.println(a);
}*/
//新增一個ACount物件
/* ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
CountService count1 = (CountService) app.getBean("countService");
ACount a=new ACount();
a.setId(3);
a.setMoney(200.0);
a.setSname("dd");
int i = count1.insertAcount(a);
System.out.println(i);*/
//修改一個ACount的資訊
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
CountService count1 = (CountService) app.getBean("countService");
ACount a=new ACount();
a.setMoney(3022.00);
a.setId(1);
a.setSname("張丹");
int i = count1.updateAcount(a);
System.out.println(i);
}
}