QueryRunner的基本練習:
public class DBUtilDemo {
private QueryRunner qr=new QueryRunner(DBCPUtil.getDataSource());
@Test
public void testAdd() throws SQLException{
qr.update("insert into account(name,money) values(?,?)", "fff",1000);
}
@Test
public void testFindOne() throws SQLException{
Account a=qr.query("select *from account where id=?", new BeanHandler<Account>(Account.class),1);
System.out.println(a);
}
@Test
public void testFindAll() throws SQLException{
qr.query("select *from account", new BeanListHandler<Account>(Account.class));
}
@Test
//批處理
public void testBath() throws SQLException{
Object params[] []=new Object[10][];
for(int i=0;i<params.length;i++){
params[i]=new Object[]{i+1,i+1};
}
qr.batch("insert into account(name,money) values(?,?)",params);
}
}java
結果集的詳細:
public class ResultSetHandlerDemo {
private QueryRunner qr=new QueryRunner(DBCPUtil.getDataSource());
@Test
//ArrayHandler 將獲取到的第一個結果集封裝到一個數組中
public void test1() throws SQLException{
Object objs[]=qr.query("selcet *from account where id=?",new ArrayHandler() , 1);
for(Object obj:objs){
System.out.println(obj);
}
}
//把結果集中的每一行數據封裝到數據中
public void test2() throws SQLException{
List<Object[]> list=qr.query("selcet *from account",new ArrayListHandler());
for(Object[] objs:list){
for(Object obj:objs){
System.err.println(obj);
}
}
}
//將某一列數據封裝到數據
public void test3() throws SQLException{
List<Object> list=qr.query("selcet *from account",new ColumnListHandler("name"));
for(Object objs:list){
System.err.println(objs);
}
}
//KeyedHandler,將獲取到的數據封裝到map中
public void test4() throws SQLException{
Map<Object, Map<String, Object>> bmap=qr.query("selcet *from account",new KeyedHandler("id"));
for(Map.Entry<Object, Map<String, Object>> maps:bmap.entrySet()){
for(Map.Entry<String, Object> map:maps.getValue().entrySet()){
System.out.println(map.getKey()+map.getValue());
}
}
}
//將獲取到的第一行數據封裝到map中
public void test5() throws SQLException{
Map<String, Object> map=qr.query("selcet *from account where id=?",new MapHandler(),1);
for(Map.Entry<String, Object> lme:map.entrySet()){
System.out.println(lme.getKey()+lme.getValue());
}
}
//將數據封裝到map裏,再放到list中
public void test6() throws SQLException{
List<Map<String, Object>> list=qr.query("selcet *from account",new MapListHandler());
for(Map<String, Object> lme:list){
for(Map.Entry<String, Object> map:lme.entrySet()){
System.out.println(map.getKey()+map.getValue());
}
}
}
//只有一條記錄的投影查詢(就顯示列的數據)
public void test7() throws SQLException{
Object obj=qr.query("selcet count(*) from account",new ScalarHandler(1));
System.out.println(obj);
}
}編程
ThreadLocal控制事物和AOP編程:
代碼體現:
1TransactionManager工具類:
public class TransactionManager {
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getConnection(){
try {
Connection conn = tl.get();//從當前線程上獲取連接
if(conn==null){
conn = DBCPUtil.getDataSource().getConnection();//沒有就從池中取一個
tl.set(conn);//綁定到當前線程上
}
return conn;
} catch (SQLException e) {
throw new RuntimeException("從數據源獲取連接失敗");
}
}
public static void startTransaction(){
Connection conn = getConnection();
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new RuntimeException(e);
}//開始事務
}
public static void commit(){
Connection conn = getConnection();
try {
conn.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void rollback(){
Connection conn = getConnection();
try {
conn.rollback();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void release(){
Connection conn = getConnection();
try {
conn.close();
tl.remove();//從當前線程上解綁。(服務器用到了線程池的技術)
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}數組
2配置文件:
object.properties
accountDao=/day_20/src/cn/itcast/dao/impl/AccountDaoImpl.java
businessService=/day_20/src/cn/itcast/businessService/impl/AccountBusinessServiceImpl.java服務器
3BeanFactory
public class BeanFactory {
//返回BusinessService的實現類的代理對象
//面向切面編程:AOP
public static BusinessService getBusinessServiceImpl(){
final BusinessService s = new BusinessServiceImpl();
BusinessService proxyS = (BusinessService)Proxy.newProxyInstance(s.getClass().getClassLoader(),
s.getClass().getInterfaces(), new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try{
TransactionManager.startTransaction();//開始事務
Object rtValue = method.invoke(s, args);
TransactionManager.commit();
return rtValue;
}catch(Exception e){
TransactionManager.rollback();
throw new RuntimeException(e);
}finally{
TransactionManager.release();
}
}
});
return proxyS;
}
}工具
4業務實現類:
public class BusinessServiceImpl implements BusinessService {
private AccountDaoImpl dao = new AccountDaoImpl();
public void transfer(String sourceAccountName, String targAccountName,float money) {
Account sAccount=dao.findAccountByName(sourceAccountName);
Account tAccount=dao.findAccountByName(targAccountName);
sAccount.setMoney(sAccount.getMoney()-money);
tAccount.setMoney(tAccount.getMoney()+money);
dao.updateAccount(tAccount);
dao.updateAccount(sAccount);
TransactionManager.commit();
}
}
.net