????1??Dao????????????

????Dao???????????????????????????????顣?????SQL???????У?????????????????????????Dao??????????????У???????????????

????1????????????????и????????????????????????????????ò?????????????????Dao?????????????????????????????????????????????????????£???????????????????????????????????????

????2?????ε?Dao???????????Spring?????????Dao??????????ЩSupport?????????????????Spring?????????????Dao??

????3?????????????????????????????????????????????????????????????????????????????????????У???????????

????4???????????????ж?????????????????????ж??????????????????????????????????????и??????????????????????????????????????

????2?????????

??????????????????????????Unitils??????Spring??Dbunit??????Dao??????????????????Maven????????????á?

????Unitils????ú?????????????????????????

????Dao?????????????????????????????????

????Java????


public class AccountDao extends JdbcDaoSupport {

    public Account getAccount(String accountId) {
        List<Account> list = null;

        list = getJdbcTemplate().query("select account_id??balance from tb_account where account_id=?"??
                new Object[] { accountId }?? new RowMapper<Account>() {

                    @Override
                    public Account mapRow(ResultSet rs?? int rowNum) throws SQLException {
                        Account acc = new Account();
                        acc.setAccountId(rs.getString("account_id"));
                        acc.setBalance(rs.getInt("balance"));
                        return acc;
                    }
                });
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public int updateAccount(String accountId?? int balance) {
        int ret = getJdbcTemplate().update("update tb_account set balance = ? where account_id =?"??
                new Object[] { balance?? accountId });
        return ret;
    }
}
 


????1??Maven??POM??????

??????Dao?????е?POM???????????

????Java????


  <dependency>
   <groupId>org.unitils</groupId>
   <artifactId>unitils-dbunit</artifactId>
   <version>${unitils.version}</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.unitils</groupId>
   <artifactId>unitils-spring</artifactId>
   <version>${unitils.version}</version>
   <scope>test</scope>
  </dependency>


????unitils.version???μ??3.3?汾

????2??Unitils?????????

????Unitils????????????????????unitils.properties?????????????????classpath?£?????????test/resources/unitils.properties????????????????

????Java????


database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.100.242:3306/test
database.userName=mantis
database.password=mantispw
database.schemaNames=test
database.dialect=mysql
DatabaseModule.Transactional.value.default=rollback


????database.driverClassName???????????Jdbc????
????database.url????????????????
????database.userName???????????????
????database.schemaNames???????????schema??mysql??????????Oracle???
????database.dialect??д???????????????mysql??oracle??derby??
????DatabaseModule.Transactional.value.default????????????????????????????????rollback??disable??commit????????????????? rollback

????3??Spring?????

????Unitils????Spring?????????????????????????Spring????????Dao??????????????DataSource???

???????Spring????????????Щ???????

??????1????Dao??????Spring?????????Property??????DataSource????????????????Щ???????÷??Maven?????test/resources/testapplication/appContext-common.xml?С?

????Java????