springMVC的业务层就是spring的事务处理了吗

springMVC的业务层就是spring的事务处理了吗  

控制器

package com.boco;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.boco.DaoLoginCheck;

@Controller // 用于标识是处理器类
public class LoginController {

	@RequestMapping("/login") // 请求到处理器功能方法的映射规则
	public ModelAndView check(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("用户" + username + "密码" + password);
		// 调用业务处理DaoLoginCheck
		if (DaoLoginCheck.check(username, password)) {
			// request.getSession().setAttribute("username", username);
			request.setAttribute("username", username);
			System.out.println(new ModelAndView("ok", "username", username));
			return new ModelAndView("ok", "username", username);
		}
		System.out.println("密码错误");
		return new ModelAndView("no", "username", username);
	}

	@RequestMapping("/reg")
	public ModelAndView save(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		request.setAttribute("password", password);
		System.out.println("申请的用户" + username + "密码" + password);
		if (DaoLoginCheck.insert(username, password)) {
			// 登入成功
			return new ModelAndView("ok", "username", username);
		}
		// 登入失败
		return new ModelAndView("regno", "username", username);
	}
}

业务层

package com.boco;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DaoLoginCheck {

	public static boolean check(String username, String password) {
		// 处理业务逻辑
		try {
			Connection conn = DataBaseUtil.getConnection();
			PreparedStatement p = conn.prepareStatement("select * from user where username = ? and password = ?");
			p.setString(1, username);
			p.setString(2, password);
			// 根据获得的数据查询获取结果集
			ResultSet rs = p.executeQuery();
			// 判断结果集是否有效,如过有效
			while (rs.next()) {
				DataBaseUtil.close(rs, p, conn);
				return true;
			}
			DataBaseUtil.close(rs, p, conn);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("数据库连接出错");
		}
		return false;
	}

	public static boolean insert(String username, String password) {
		// 处理业务逻辑
		try {
			Connection conn = DataBaseUtil.getConnection();
			PreparedStatement p = conn.prepareStatement("select * from user where username = ?");
			p.setString(1, username);
			ResultSet rs = p.executeQuery();
			while (rs.next()) {
				// 如果有不能注册
				return false;
			}
			p = conn.prepareStatement("insert into user(username,password) values(?,?)");
			p.setString(1, username);
			p.setString(2, password);
			p.executeUpdate();
			DataBaseUtil.close(rs, p, conn);
			System.out.println("Save_OK");

		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("数据库连接出错");

		}
		return true;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值