package cn.mob.jekin.entity;
import java.io.Serializable;
public class User implements Serializable{
private Integer userId;
private String userName;
private String userPass;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
}
公共接口:BaseMapper.java
package cn.mob.jekin.dao;
public interface BaseMapper {
//添加单个对象
public int insert(T entity);
//修改单个对象
public int update(T entity);
//删除单个对象
public int delete(T entity);
//查询单个对象
public T select(T entity);
}
UserMapper.java
package cn.mob.jekin.dao;
import cn.mob.jekin.entity.User;
public interface UserMapper extends BaseMapper {
public User login(User user);
}
UserMapper.xml
注意:1、映射文件命名要和接口对象UserMapper一致,xml文件必须声明定义
package cn.mob.jekin.modal;
public interface BaseService {
// 添加单个对象
public int insert(T entity) throws Exception;
// 修改单个对象
public int update(T entity) throws Exception;
// 删除单个对象
public int delete(T entity) throws Exception;
// 查询单个对象
public T select(T entity);
}
UserService:
package cn.mob.jekin.modal;
import cn.mob.jekin.entity.User;
public interface UserService extends BaseService {
public User login(User user);
}
上面写起来是不是感觉在复述上面内容呢低耦合嘛,are you understand???package cn.mob.jekin.modal.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.mob.jekin.dao.UserMapper;
import cn.mob.jekin.entity.User;
import cn.mob.jekin.modal.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public int insert(User entity) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public int update(User entity) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public int delete(User entity) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public User select(User entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public User login(User user) {
// TODO Auto-generated method stub
return userMapper.login(user);
}
}
如果想理解更深,可以查看注解@源码了解它们之间的交互联系。
package cn.mob.jekin.control;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/base")
public class BaseAction {
@Resource
ServletContext application;
// 方法参数folder通过@PathVariable指定其值可以从@RequestMapping的{folder}获取,同理file也一样
@RequestMapping("/goURL/{folder}/{file}")
public String goURL(@PathVariable String folder, @PathVariable String file) {
System.out.println("goURL.folder|file===" + folder + "/" + file);
return "forward:/WEB-INF/" + folder + "/" + file + ".jsp";
}
}
package cn.mob.jekin.control;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.mob.jekin.entity.User;
import cn.mob.jekin.modal.UserService;
@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction {
@Resource
private UserService userService;
@RequestMapping(value="/login")
public String login(User user,HttpServletRequest request,HttpSession session){
System.out.println("---action.user:"+user);
User acc= userService.login(user);
if(acc!=null){
//存session
session.setAttribute("user", acc);
return "forward:/main.jsp";
}else{
request.setAttribute("msg", "用户名或密码错误!");
return "forward:/login.jsp";
}
}
}
drop database if exists ssm;
create database ssm default character set utf8;
use ssm;
drop table if exists user;
/*============================*/
/* Table: 用戶表结构 */
/*============================*/
create table user
(
/* 用戶编号,自动增长 */
user_id int not null auto_increment,
/* 用戶登录名 */
user_name varchar(20),
/* 用戶姓名 */
user_pass varchar(20),
/* 用戶密码 */
primary key (user_id)
);
/* 添加用戶数据, */
insert into user (user_name,user_pass) values ('admin','123');
insert into user (user_name,user_pass) values ('Jekin','123');
select * from user;
# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=debug,stdout
# My logging configuration...
log4j.logger.cn.jbit.mybatisdemo=DEBUG
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.logger.org.apache.ibatis=DEBUG
## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
如果出现Tomcat内存溢出,直接删除Tomcat/logs/localhost_access_log.2017-01-04文件,其他原因参考:
注意:将数据库、用户名、密码修改成个人的数据库。
spring-mvc.xml
context:component-scan 对指定的包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。
视图层:/WebRoot/common/common.jspf抽取公共部分<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
easyui下载地址:http://www.jeasyui.net/download/如果觉得需要,可以将整个导入。项目中js存放路径/WebRoot/jquery-easyui-1.3.5<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/common/common.jspf"%>
Login page
${requestScope.msg}
主界面/WebRoot/main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
Main
登录成功,欢迎您 :${sessionScope.user.userName}
web.xml
login.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
mvc
*.action
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
这里面主要是spring监听器、servlet的配置,还有编码过滤器。