首先说明一下,搭建springmvc+mybatis整整花费了两天的功夫,主要问题就是jar包冲突、配置文件有误等问题,也有教程本身可能存在问题,因此本次就重新搭建了一次。
首先看一下目录结构,这个就是完整的maven+springmvc+mybatis目录。
第一步:使用maven搭建springmvc动态网站
【注意】在完成后,还需要手动进入工程文件夹,从跟目录src开始新建三个文件夹
main/java
test/java
test/resources
刷新项目工程,就成为了动态web项目。
第二步:新建springmvc+mybatis
在完成动态web的搭建后,现在要加入springmvc和mybatis项目的支持。pom.xml文件如下:
4.0.0
jianghuiwen.youhua
springmvc
war
0.0.1-SNAPSHOT
springmvc Maven Webapp
http://maven.apache.org
UTF-8
3.1.1.RELEASE
3.8.1
junit
junit
3.8.1
test
log4j
log4j
1.2.17
org.mybatis
mybatis
3.3.0
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.30
org.springframework
spring-jdbc
${springversion}
org.springframework
spring-context-support
${springversion}
org.springframework
spring-web
${springversion}
org.springframework
spring-webmvc
${springversion}
org.springframework
spring-core
${springversion}
commons-dbcp
commons-dbcp
1.4
javax.servlet
servlet-api
2.5
provided
org.springframework
spring-dao
2.0.8
org.springframework
spring-context
${springversion}
springmvc
index.jsp
org.springframework.web.util.Log4jConfigListener
book
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring-servlet.xml
1
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
book
/
#定义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
/springmvc/src/main/java/click/youhua/mapper/StudentMapper.java
package click.youhua.mapper;
import click.youhua.model.Student;
/*
* mybatis接口,完成对数据库的映射,即在service层调用该接口的时候,直接可以操作对应的Mapper.xml中的SQL语句
* 因此要求该类名需要和数据库映射文件名字相同.例如:StudentMapper.java -- studentMapper.xml
* */
public interface StudentMapper {
public Student findById(Long id);
public void addStudent(Student student);
}
studentMapper.xml
insert into student(name, age) values(#{name}, #{age})
/springmvc/src/main/java/click/youhua/service/impl/StudentServiceImpl.java
package click.youhua.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import click.youhua.mapper.StudentMapper;
import click.youhua.model.Student;
import click.youhua.service.StudentService;
@Service("studentService")
public class StudentServiceImpl implements StudentService{
@Resource
private StudentMapper studentMapper;
@Override
public Student findById(Long id){
return studentMapper.findById(id);
}
@Override
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
}
package click.youhua.controller;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import click.youhua.model.Student;
import click.youhua.service.StudentService;
@Controller
@RequestMapping(value = "/index")
public class IndexController {
@Autowired
private StudentService studentService;
@RequestMapping(value="/list")
public ModelAndView list(){
System.out.println("index...");
Map data = new HashMap();
data.put("name",studentService.findById(3L).getName());
return new ModelAndView("index",data);
}
@RequestMapping(value = "/add")
public String add(){
System.out.println("add...");
Student student = new Student();
student.setId(3L);
student.setName("hongwen");
student.setAge(20L);
studentService.addStudent(student);
return "index";
}
}
/springmvc/src/main/java/click/youhua/interceptor/LoginInterceptor.java
package click.youhua.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("拦截器拦截啦!");
return false;
}
}
启动tomcat访问,可以看到: