spring+mybatis+springmvc 三大框架的整合(SSM)超级详细教程

第一步:使用intellij Idea搭建 maven web 项目
spring+mybatis+springmvc 三大框架的整合(SSM)超级详细教程_第1张图片

第二步:引入依赖

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

           4.0.0

 new
news
1.0-SNAPSHOT
 war

 news Maven Webapp
    http://www.example.com


 
UTF-8
1.7
1.7
5.0.8.RELEASE
 

 

  junit
  junit
  4.12
  test



  org.springframework
  spring-core
  ${spring.version}


  org.springframework
  spring-context
  ${spring.version}


  org.springframework
  spring-context-support
  ${spring.version}


  org.springframework
  spring-beans
  ${spring.version}


  org.springframework
  spring-web
  ${spring.version}


  org.springframework
  spring-webmvc
  ${spring.version}


  org.springframework
  spring-oxm
  ${spring.version}


  org.springframework
  spring-aop
  ${spring.version}


  org.springframework
  spring-aspects
  ${spring.version}


  org.springframework
  spring-jdbc
  ${spring.version}


  org.springframework
  spring-tx
  ${spring.version}


  org.springframework
  spring-test
  4.2.4.RELEASE





  com.fasterxml.jackson.core
  jackson-databind
  2.9.3


  com.fasterxml.jackson.core
  jackson-core
  2.9.3


  com.fasterxml.jackson.core
  jackson-annotations
  2.9.3


  net.sf.json-lib
  json-lib
  2.4
  jdk15



  org.codehaus.jackson
  jackson-core-asl
  1.9.2


  org.codehaus.jackson
  jackson-mapper-asl
  1.9.2




  commons-fileupload
  commons-fileupload
  1.3.1




  org.mybatis
  mybatis
  3.4.1


  mysql
  mysql-connector-java
  5.1.38



  org.slf4j
  slf4j-api
  1.7.12


  org.slf4j
  slf4j-log4j12
  1.7.12


  log4j
  log4j
  1.2.17




  com.mchange
  c3p0
  0.9.2.1



  org.mybatis
  mybatis-spring
  1.3.0



  javax.servlet
  javax.servlet-api
  3.1.0
  provided


  javax.servlet.jsp
  javax.servlet.jsp-api
  2.3.1
  provided


  javax.servlet
  jstl
  1.2




  joda-time
  joda-time
  2.9.9



  com.github.pagehelper
  pagehelper
  5.1.2




  commons-codec
  commons-codec
  1.10

  
      junit
      junit
      4.12
  

 

 
news

  
    
      maven-clean-plugin
      3.1.0
    
    
    
      maven-resources-plugin
      3.0.2
    
    
      maven-compiler-plugin
      3.8.0
    
    
      maven-surefire-plugin
      2.22.1
    
    
      maven-war-plugin
      3.2.2
    
    
      maven-install-plugin
      2.5.2
    
    
      maven-deploy-plugin
      2.8.2
    
  

 

第三步:创建jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm(这个是数据库名称)
username=root
password=123
#定义初始连接数
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

#mybatis显示SQL语句日志配置
log4j.logger.com.sz.mapper=DEBUG

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录 这里会放在tomcat之下
log4j.appender.File.File=D:/log.log
#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-mybatis.xml文件进行相关的配置

  
 
(扫描所有需要spring管理的包.类)


*****(引入jdbc.properties文件)*****

    

  
       
    
    
    
    
    
    
    
    
    
    
    
    
    






    
    
    




    
    




    

第六步:创建一个数据表进行测试

spring+mybatis+springmvc 三大框架的整合(SSM)超级详细教程_第2张图片

A : 创建实体类
package com.hk.pojo;

public class User {
private String name;
private String password;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}
B: 写mapper接口(注意@Component和@Mapper这两个注解至关重要
package com.hk.mapper;

import com.hk.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

@Component
@Mapper
public interface UserMapeer {
User select();
}

mapper.xml

 

 
   

C:编写servie 接口(注意命名规则
package com.hk.service;
import com.hk.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
public interface IUserService {
public User getUser();
}
D:编写service实现类(请认真看注释)
package com.hk.service;

import com.hk.mapper.UserMapeer;
import com.hk.pojo.User;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@Service(“userService”) //service注解必须要有
public class UserServiceImpl implements IUserService {
//自动装配
@Autowired
private UserMapeer usermapper;

//这种方式是进行手动装配的不推荐使用
/* ApplicationContext ac = new ClassPathXmlApplicationContext(“spring-mybatis.xml”);
UserMapeer dao = ac.getBean(UserMapeer.class);*/

@Override
public User getUser() {
    //System.out.print(this.usermapper.select());
    return this.usermapper.select();
}

//下面这个是我进行测试的但是不成功,读者请忽略 ,如果非要使用的话必须在类前面加两个注解@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {“classpath:spring-mybatis.xml”})
@Test
public void test1(){
System.out.print(usermapper.select().getName());
// System.out.print(usermapper.select().getName());

}

}

第七步:编写测试类进行测试

package com.hk.test;

import com.hk.pojo.User;
import com.hk.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
//如果编写测试类必须加上这两个注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {“classpath:spring-mybatis.xml”})
public class DBtest {
@Resource
private IUserService userService=null;

@Test
public void test1() {
    User user = userService.getUser();
    System.out.println(user.getName() + "::::" + user.getPassword());
}

}

成功整合spring和mybatis下面开始整合springmvc

第八步:配置spring-mvc.xml

 
 

<!--自动扫描controlle包下面的XXXcontroller-->



    
        
            
                
                    
                        text/html; charset=UTF-8
                        application/json;charset=UTF-8
                    
                
            
            
                
                    
                        text/html; charset=UTF-8
                        application/json;charset=UTF-8
                    
                
            
        
    


***
***

    




    
    
    




    
    
    
    
    
    


 

第九步配置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
  

第十步:开始测试

在jsp文件夹下新建showuser.jsp文件
spring+mybatis+springmvc 三大框架的整合(SSM)超级详细教程_第3张图片

建立UserController类

package com.hk.controller;

import com.hk.pojo.User;
import com.hk.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user") //指的是控制器的标识
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/showuser") //指的是类中方法的标识
public String toIndex(HttpServletRequest request, Model model){

    User user = this.userService.getUser();
    model.addAttribute("user", user);
    return "showuser";
}

}

部署项目测试

你可能感兴趣的:(spring+mybatis+springmvc 三大框架的整合(SSM)超级详细教程)