SpringMVC框架:负责请求的转发和视图管理
Mybatis框架:作为数据对象的持久化引擎,主要是通过配置mapper.xml文件来直接给Dao接口提供对数据库进行操作的sql语句,使数据库底层操作变得更加透明
整合框架过程中还需要用到四个层:Dao层(数据访问层)、Service层(业务层)、Pojo层(实体层)、Controller层(控制层)
Dao层:主要作用是封装对数据库操作的方法(增删查改)
Service层:主要作用是封装具体的业务方法,然后在实现类中调用Dao层的方法来对数据进行操作
Pojo层:没啥好说的…个人理解就是对象实体
Controller层:主要作用是负责调用业务层的方法来实现具体的功能
4.0.0
com.nrt.ssm
MySSM
war
0.0.1-SNAPSHOT
MySSM Maven Webapp
http://maven.apache.org
junit
junit
4.11
test
org.slf4j
slf4j-api
1.7.12
ch.qos.logback
logback-core
1.1.1
ch.qos.logback
logback-classic
1.1.1
mysql
mysql-connector-java
5.1.35
runtime
c3p0
c3p0
0.9.1.2
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.3
taglibs
standard
1.1.2
jstl
jstl
1.2
com.fasterxml.jackson.core
jackson-databind
2.5.4
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-core
4.2.0.RELEASE
org.springframework
spring-beans
4.2.0.RELEASE
org.springframework
spring-context
4.2.0.RELEASE
org.springframework
spring-jdbc
4.2.0.RELEASE
org.springframework
spring-tx
4.2.0.RELEASE
org.springframework
spring-web
4.2.0.RELEASE
org.springframework
spring-webmvc
4.2.0.RELEASE
org.springframework
spring-test
4.2.0.RELEASE
MySSM
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
package com.nrt.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return username;
}
public void setName(String username) {
this.username = username;
}
public String getPwd() {
return password;
}
public void setPwd(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
package com.nrt.dao;
import com.nrt.pojo.User;
public interface UserDao {
/**
*
* 查找用户
*/
User findByUser(User user);
}
package com.nrt.service;
import com.nrt.pojo.User;
public interface UserService {
/*
* 登录验证
* */
User checkLogin(User user);
}
package com.nrt.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.nrt.dao.UserDao;
import com.nrt.pojo.User;
import com.nrt.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
public User checkLogin(User user) {
User u = userDao.findByUser(user);
return u;
}
}
package com.nrt.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
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 org.springframework.web.bind.annotation.SessionAttributes;
import com.nrt.pojo.User;
import com.nrt.service.UserService;
@Controller
@RequestMapping("/")
@SessionAttributes("username")
public class UserController {
@Resource(name = "userService")
private UserService userService;
//测试前台能否正常访问
@RequestMapping(value = "/toLogin")
public String toIndex() {
return "login";
}
//登录功能实现
@RequestMapping(value = "/checkLogin")
public String checkLogin(@RequestParam("username")String username,@RequestParam("password")String password,Model model) {
User user = new User();
user.setName(username);
user.setPwd(password);
if(userService.checkLogin(user) != null) {
model.addAttribute("username",username);
return "homePage";
}else {
return "fail";
}
}
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
MySSM
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
1
MySSM
/
Archetype Created Web Application
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=各位的数据库密码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录界面
homePage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
失败啦!!!