栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SSM整合(Eclipse)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SSM整合(Eclipse)

            SSM整合步骤

1.创建maven工程引入相关依赖


    org.springframework
    spring-webmvc
    5.3.15





    org.springframework
    spring-jdbc
    5.3.15




    org.springframework
    spring-aspects
    5.3.15




    org.springframework
    spring-test
    5.3.15
    test





    org.mybatis
    mybatis
    3.5.6




    org.mybatis
    mybatis-spring
    2.0.6




    c3p0
    c3p0
    0.9.1.2



    mysql
    mysql-connector-java
    8.0.28




    javax.servlet
    jstl
    1.2



    javax.servlet
    servlet-api
    2.5
    provided






    junit
    junit
    4.12
    test





    org.mybatis.generator
    mybatis-generator-core
    1.4.0



  
2.引入前端框架bootstrap


3 配置web.xml

启动Spring容器
使项目一启动加载某处的spring配置文件
在类路径下创建Spring配置文件


 contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener


类路径下的Spring配置文件

4.在web.xml配置SpringMVC前段控制器拦截所有请求

1)若不指定SpringMVC配置文件需要删掉中间的< init-parm>标签并在web-inf 下新建一个web.xml命名为XXX-servlet.xml
xxx使你servlet标签中的名字

 
  springDispatcherServlet
  org.springframework.web.servlet.DispatcherServlet
  1
  
  
  springDispatcherServlet
   / 
  

2)顺手配置上字符过滤器 放在所有过滤器之前‘


		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
		
			encoding
			UTF-8
		
			
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	
	

3)配置Rest 风格

	
  HiddenHttpMethodFilter
  org.springframework.web.filter.HiddenHttpMethodFilter
  
  
  HiddenHttpMethodFilter
  /*
  
4.配置SpringMVC

头部标签



1)SpringMVC不能扫描所有组件让其只扫描前段的控制器




	

2)配置视图解析器



        
        

3).两个标准配置
1.将springmvc不能处理的请求交给tomcat
2.支持SpringMVC更高级的一些功能 JSR303校验, 快捷的ajax 映射动态请求功能

 

 
5.配置Spring


1)让其开启扫描 除了SprngMVC扫描的Controller控制器其它都扫描

 
 
 
 

2)配置数据源 (数据库连接池)

类路径下创建一个jdbc.properties

  
     
     
     
     
     
                
     

3)配置和MyBatis的整合
类路径下新建mybatis-config.xml (mybatis的核心配置文件)


 
 
 
 
 

4)配置扫描器,将mybatis接口的实现加入到ioc中






5)事物控制的配置
1)控制数据源 pooledDataSource也是上面数据源的id







 2)   



  
  


  
  
  
  
   
  

  

6.mybatis-config.xml的配置

可在applicationContext中配置也可在mybatis-config.xml中配置











       


	


7.使用mybatis的逆向工程

导入jar包


    org.mybatis.generator
    mybatis-generator-core
    1.4.0

和pow.xml同级下创建配置文件 并配置 看看各个包的路径





  
  
  
  

  
    
    

    
      
    
      
    
      
      
    
      
    
      
    
  
    
      
    


  

2)测试生成类

package com.ycx.test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MBGTest {
 
	public static void main(String[] args) throws Exception {
		  List warnings = new ArrayList();
		   boolean overwrite = true;
		   File configFile = new File("mbg.xml");
		   ConfigurationParser cp = new ConfigurationParser(warnings);
		   Configuration config = cp.parseConfiguration(configFile);
		   DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		   MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		   myBatisGenerator.generate(null);
	}
}

运行完成后注意要去实体类中生成构造器 和toString方法

8 、搭建Spring单元测试

1)导依赖


    org.springframework
    spring-test
    5.3.15
    test

2)使用@ContextConfiguration指定Spring配置文件位置
@ContextConfiguration(locations={“classpath:applicationContext.xml”})
指定用哪个Test进行单元测试
@RunWith(SpringJUnit4ClassRunner.class)

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ycx.bean.Dept;
import com.ycx.bean.Employee;
import com.ycx.dao.DeptMapper;
//推荐Spring的项目可以使用Spring的单元测试 可以自动注入我们需要的组件
//导入SpringTest模块
//使用@ContextConfiguration指定Spring配置文件的位置
//直接autowired要使用的组件
import com.ycx.dao.EmployeeMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class MapperTest {
    @Autowired
	DeptMapper   d;
    @Autowired
    EmployeeMapper e;
    @Test
    public  void  testCRUD(){  
//    	d.insertSelective(new Dept(null, "测试部"));
//    	d.insertSelective(new Dept(null, "电竞部"));
//      e.insertSelective(new Employee(null, "Jerry", "m", "123@qq.com", 1));   	
    	//3批量插入 
    	System.out.println(d);
    }
    
	
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号