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;
}
}