一:maven引入jar包,配置pox.xml
4.0.0 maimai.yourMvc yourMvc war 0.0.1-SNAPSHOT yourMvc Maven Webapp http://maven.apache.org 4.0.2.RELEASE 3.2.6 1.7.7 1.2.17 junit junit 4.10 test org.springframework spring-core ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-oxm ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-test ${spring.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.2.2 javax javaee-api 7.0 mysql mysql-connector-java 5.1.30 commons-dbcp commons-dbcp 1.2.2 jstl jstl 1.2 log4j log4j ${log4j.version} com.alibaba fastjson 1.1.41 org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.codehaus.jackson jackson-mapper-asl 1.9.13 commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.4 commons-codec commons-codec 1.9 org.hamcrest hamcrest-core 1.3.RC2 org.hamcrest hamcrest-library 1.3.RC2 commons-logging commons-logging 1.1.3 yourMvc
二:在resources下面创建下面配置文件
jdbc.properties
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/maimaiusername=maimaipassword=123456#定义初始连接数 initialSize=0#定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20#定义最小空闲 minIdle=1#定义最长等待时间 maxWait=60000
log4j.properties
#定义LOG输出级别 log4j.rootLogger=INFO,Console,File #定义日志输出目的地为控制台 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以灵活地指定日志输出格式,下面一行是指定具体的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定输出目录 log4j.appender.File.File = logs/ssm.log #定义文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
spring-mvc.xml
spring-mybatis.xml
配置web.xml
Archetype Created Web Application contextConfigLocation classpath:spring-mybatis.xml encodingFilter org.springframework.web.filter.CharacterEncodingFilter true encoding UTF-8 encodingFilter /* org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 true SpringMVC / /index.jsp
下面的工作就是创建数据接口的服务类了
创建数据服务接口
com.maimai.service
IStudentService
package com.maimai.service;import com.maimai.pojo.Student;;public interface IStudentService { public Student getStudentById(int studentId); }
创建数据接口实现
com.maimai.service.impl
studentServiceImpl
package com.maimai.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;import com.maimai.IDao.StudentMapper;import com.maimai.pojo.Student;import com.maimai.service.IStudentService;@Service("studentService")public class studentServiceImpl implements IStudentService { @Resource private StudentMapper studentDao; @Override public Student getStudentById(int studentId) { return this.studentDao.selectByPrimaryKey(studentId); }}
数据接口创建完毕,接下来就改MVC登场了
创建controller
com.maimai.controller
StudentController
package com.maimai.controller;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.maimai.pojo.Student;import com.maimai.service.IStudentService;@Controller @RequestMapping("/student") public class StudentController { @Resource private IStudentService studentService; @RequestMapping("/studentView") public String toIndex(HttpServletRequest request,Model model){ int studentId = Integer.parseInt(request.getParameter("id")); Student user = this.studentService.getStudentById(studentId); model.addAttribute("student", user); return "studentView"; } }
创建View
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>student ${student.id}---${student.userName}---${student.password}--${student.age}
注意 isELIgnored="false" ,要不表达式不解析
测试,看到结果,成功