话不多说开整
1.项目结构
2.配置文件
a.web.xml
b.springmvc.xml
c.sqlmapper.xml(鸡肋鸡肋)
d.applicationContext-dao.xml
e.applicationContext-service.xml
f.applicationContext-trans.xml
3.pojo实体类(数据库就省了吧!)
package com.ithyl.pojo;
public class User {
private int id;
private String username;
private String password;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
4.dao与数据库建立联系
a...
package com.ithyl.mapper;
import com.ithyl.pojo.User;
public interface UserDao {
public User selectByUN(String username);
public User selectByPW(String password);
}
b:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace用于与DAO层的接口类进行绑定,这样我们无需实现DAO层的接口
类,其接口类就能够自动的找到相关的SQL语句进行绑定实现-->
<mapper namespace="com.ithyl.mapper.UserDao">
<!--select表示查询,它的id名称必须与DAO层接口的方法名相同,否则无法绑定-->
<select id="selectByUN" parameterType="string" resultType="com.ithyl.pojo.User">
select * from tb_user where username = #{username}
</select>
<select id="selectByPW" parameterType="int" resultType="com.ithyl.pojo.User">
select * from tb_user where password= #{password}
</select>
</mapper>
5.service业务层先来个借口写抽象方法
package com.ithyl.service;
public interface UserService {
public boolean login(String username,String password);
}
在写个实现类、、、、、、、、、、、、
package com.ithyl.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ithyl.mapper.UserDao;
import com.ithyl.pojo.User;
//@Service("UserService") 注解用于标示此类为业务层组件,在使用时会被注解的类会自动由
//spring进行注入,无需我们创建实例
@Service("UserService")
public class UserServiceImpl implements UserService {
//自动注入iuserdao 用于访问数据库
@Autowired
UserDao Mapper;
//登录方法的实现,从jsp页面获取username与password
public boolean login(String username, String password) {
// System.out.println("输入的账号:" + username + "输入的密码:" + password);
//对输入账号进行查询,取出数据库中保存对信息
User user = Mapper.selectByUN(username);
if (user != null) {
// System.out.println("查询出来的账号:" + user.getUsername() + "密码:" + user.getPassword());
// System.out.println("---------");
if (user.getUsername().equals(username) && user.getPassword().equals(password))
return true;
}
return false;
}
}
6.业务当然要有控制类controller来控制
package com.ithyl.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ithyl.pojo.User;
import com.ithyl.service.UserService;
//@Controller注解用于标示本类为web层控制组件
@Controller
//@RequestMapping("/user")用于标定访问时对url位置
//@RequestMapping("/user")
//在默认情况下springmvc的实例都是单例模式,所以使用scope域将其注解为每次都创建一个新的实例
@Scope("prototype")
public class Login {
//自动注入业务层的userService类
@Autowired
UserService userService;
//login业务的访问位置为/user/login
@RequestMapping("/login")
public String login(User user,HttpServletRequest request){
//调用login方法来验证是否是注册用户
boolean loginType = userService.login(user.getUsername(),user.getPassword());
if(loginType){
//如果验证通过,则将用户信息传到前台
request.setAttribute("user",user);
//并跳转到success.jsp页面
return "success";
}else{
//若不对,则将错误信息显示到错误页面
request.setAttribute("message","用户名密码错误");
return "error";
}
}、
jsp就算了吧!!!!