锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
/**
* 娣誨姞棰嗙敤鐢寵
*
* @param draw
* 棰嗙敤鐢寵淇℃伅(涓嶅彲涓簄ull)
* @return
*/
@SuppressWarnings("unchecked")
public boolean insertDraw(Carddrawapply draw, List<Carddrawapplydetail> drawdetailList) {
boolean flag = false;
Session session = null;
Transaction tx = null;
try {
session = this.getSession();
tx = session.beginTransaction();
long shopid = MyConstant.HTDZ_DEFAULT_SHOPID;//鍟嗗ID
String billtype = CardConstant.CARD_LY_ID;//涓氬姟緙栧彿
String engshort = MyConstant.HTDZ_STR;//鍟嗗鑻辨枃綆縐?
String busishort = CardConstant.CARD_LY_CODE;//涓氬姟鑻辨枃綆縐?
String formcode = CardCode.getFormCode(session,shopid,billtype,engshort,busishort);
if(!"".equals(formcode)){
draw.setCarddrawapplycode(formcode);
draw.setSourcetype("0");
draw.setApplydate(MyFormat.getToday());
draw.setStatus(StatusUtil.CARDDRAWAPPLY_STATUS01);
session.save(draw);
long carddrawapplyid = draw.getCarddrawapplyid();
session.flush();
if(drawdetailList.size()==0){
tx.rollback();
}else{
for(Carddrawapplydetail drawdetail : drawdetailList){
if(drawdetail!=null){
drawdetail.setCarddrawapplyid(carddrawapplyid);
session.save(drawdetail);
}
}
tx.commit();
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
flag = false;
} finally {
session.close();
}
return flag;
}
}
浠ヤ笂Dao鐨勬柟娉曞凡緇忔甯搞?
瀵規鏂規硶鐨勬祴璇曪紝鎴戞槸鍦ㄥ悗鏈熻ˉ涓婄殑銆備絾鏄眳鐒舵姤閿欍傚府鎴戠湅鐪嬶細
Junit4:(涓嶇敤緇ф壙TestCase)
public class TestDrawDao{
private DrawDao drawDao;
@Before
public void setUp(){
drawDao = new DrawDao();
}
/**
* 嫻嬭瘯'鍗¢鐢ㄧ敵璇蜂俊鎭殑娣誨姞'
*/
@Test
public void testInsertDraw(){
Carddrawapply draw = new Carddrawapply();
draw.setApplyoperid(1);
draw.setApplydesc("鐢寵璇存槑");
List<Carddrawapplydetail> drawdetailList = new ArrayList<Carddrawapplydetail>();
//IC鍗★紝100鍏冮潰鍊肩殑100寮?
Carddrawapplydetail drawdetail1 = new Carddrawapplydetail();
drawdetail1.setCardtype("1");
drawdetail1.setParvalue(100);
drawdetail1.setAmount(100);
//鍏呭煎崱錛?0鍏冮潰鍊肩殑50寮?
Carddrawapplydetail drawdetail2 = new Carddrawapplydetail();
drawdetail2.setCardtype("2");
drawdetail2.setParvalue(50);
drawdetail2.setAmount(50);
drawdetailList.add(drawdetail1);
drawdetailList.add(drawdetail2);
boolean flag = drawDao.insertDraw(draw, drawdetailList);
//鏂█
Assert.assertEquals(true,flag);
}
}
寮傚父濡備笅:
java.lang.NullPointerException
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.getSession(HibernateDaoSupport.java:142)
at com.htdz.card.admin.dao.DrawDao.insertDraw(DrawDao.java:35)
涔熷氨鏄皟鐢ㄥ埌DrawDao閲宨nsertDraw()鐨勮繖鍙ユ椂鎶ラ敊錛歴ession = this.getSession();
涓氬姟綾婚噷璋冪敤榪欎釜Dao閮借兘姝e父浣跨敤錛屼負浠涔堜粠嫻嬭瘯綾婚噷璋冪敤灝辨姤榪欎釜閿欒錛?
DrawDao緇ф壙浜咹ibernateDaoSupport浜嗗晩銆?
]]>
]]>
]]>
* MD5鍔犲瘑
*
* @param s
* 瑕佸姞瀵嗙殑瀛楃涓?涓嶅彲涓簄ull)
*
* @return 榪斿洖鍔犲瘑鍚庣殑瀵嗘枃
*/
public final static String MD5(String pwd) {
String Digest = "";
try {
MessageDigest currentAlgorithm = MessageDigest.getInstance("md5");
currentAlgorithm.reset();
byte[] mess = pwd.getBytes();// 鑾峰彇瑕佸姞瀵嗙殑byte鏁扮粍
byte[] hash = currentAlgorithm.digest(mess);// 鑾峰彇鍔犲瘑浠ュ悗鐨刡yte鏁扮粍
for (int i = 0; i < hash.length; i++) {
int v = hash[i];
if (v < 0) {
v = 256 + v; // 濡傛灉 v < 0 > 0 鐨勬暟錛屽惁鍒欎笉濂借漿鎹㈡垚瀛楃錛屽摢鏈堿SC浠g爜<0鐨勫憖
}
if (v < 16) {
Digest += "0"; // 濡傛灉璇<1616榪涘埗鏁板氨鏄彧鏈変釜浣嶏紝渚嬪15杞崲灝辨垚闀垮害灝辯浉絳変簡
}
Digest += Integer.toString(v, 16).toUpperCase();
}
mess = null;
hash = null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return Digest;
}
public class CheckCodeTag extends TagSupport {
private String id;
private String height;
private String width;
public CheckCodeTag() {
}
@SuppressWarnings( { "unchecked", "static-access" })
public int doStartTag() throws JspException {
StringBuffer html = new StringBuffer();
if (height == null || height.length() == 0)
height = "18";
if (width == null || width.length() == 0)
width = "60";
html.append("<img alt=\"閲嶆柊鑾峰彇楠岃瘉鐮乗"");
if (id != null && id.length() > 0) {
html.append(" id=\"");
html.append(id);
html.append("\"");
}
html.append(" height=\"");
html.append(height);
html.append("\" width=\"");
html.append(width);
html.append("\" src=\"/checkCodeImg\" onclick=\"this.src='/checkCodeImg?now='+new Date();\" style=\"cursor: pointer\" />");
try {
pageContext.getOut().println(html.toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return this.SKIP_BODY;
}
public void setId(String id) {
this.id = id;
}
public void setHeight(String height) {
this.height = height;
}
public void setWidth(String width) {
this.width = width;
}
}
public class CheckCodeImg extends HttpServlet {
private Font mFont = new Font("Arial black", Font.PLAIN, 16);
public void init() throws ServletException {
super.init();
}
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
int width = 60, height = 18;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}
char[] codes = { // 'a','b','c','d','e','f','h','k','m','n','r','s','t','x','y',//15涓?br />
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
// 'A','B','C','D','E','F','G','H','K','N','S','T','X','Y'//14
};
String sRand = "";
for (int i = 0; i < 4; i++) {
int j = random.nextInt(10);
String tmp = String.valueOf(codes[j]);
sRand += tmp;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(tmp, 15 * i + 2, 15);
}
HttpSession session = request.getSession(true);
session.setAttribute(MyConstant.SESSION_CHECKCODE, sRand);
g.dispose();
ImageIO.write(image, "PNG", response.getOutputStream());
}
}