ssm框架整合,基本增删改查

首先,我们需要导如相关jar包,并且build Path一下
ssm框架整合,基本增删改查_第1张图片

看一下我们demo的总体结构

ssm框架整合,基本增删改查_第2张图片

接下来配置web.xml



	ssm2
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	
	
	
		contextConfigLocation
		classpath:applicationContext-*.xml
	
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		encoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		encoding
		/*
	
	
		
	  
  	ssm
  	org.springframework.web.servlet.DispatcherServlet
   
  	 	contextConfigLocation
  	 	classpath:springmvc.xml
  	 
  	 1
  
  
  	ssm
  	/
  
	

	

导入log4j日志文件
ssm框架整合,基本增删改查_第3张图片

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

给出数据库链接配置文件
ssm框架整合,基本增删改查_第4张图片

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=1234

接下来首先配置dao层
ssm框架整合,基本增删改查_第5张图片



	
	
	
	
	
	
	
		
		
		
		
	
		
	
		
	
	
	
		
	



	
	
		
		
		
		
	
	
	 
	
	 
	 
	
	

然后是service层

ssm框架整合,基本增删改查_第6张图片



	
	
	
	


sqlMapconfig开启别名

ssm框架整合,基本增删改查_第7张图片




 
 
 	
 
 


接下来配置springmvc.xml
ssm框架整合,基本增删改查_第8张图片



	
	
	
	
	
	
	
	
		
		
	
	
	
		
		
		
		

	
	
	


配置大致完成,接下来我们开始一个测试

ssm框架整合,基本增删改查_第9张图片
ssm框架整合,基本增删改查_第10张图片
在mapper.xml文件中书写好sql语句,并且绑定到mapper接口中去

ssm框架整合,基本增删改查_第11张图片
service层接口

package com.it.lpp.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.it.lpp.mapper.UserInformationDao;
import com.it.lpp.pojo.UserVo;
@Service
public class UserInformationServiceImpl implements UserInformationService {
	@Autowired
private UserInformationDao userInformationDao;


public List selectUserInformationByAll(){
	return userInformationDao.selectUserInformationByAll();
}



public void updateUserInformation(UserVo user) {

 userInformationDao.updateUserInformation(user);
}




public void deleteInformationByName(UserVo user) {
	// TODO Auto-generated method stub
	userInformationDao.deleteInformationByName(user);
}
}

实现接口,使用自动装配一个UserInformationDao对象

在controller层中创建一个处理类

ssm框架整合,基本增删改查_第12张图片

装配一个UserInformationService对象

调用我们写好的查询方法并输出

浏览器中输入http://localhost:8080/ssm2/user/list

查看控制台
ssm框架整合,基本增删改查_第13张图片
可以看到 查询完成

在此过程中遇到的一个问题,
ssm框架使用5.0版本jar包链接数据库的时候,会报无法链接,但单独不使用框架,或者单个框架的时候,没用出现这种情况,或许和你的数据库版本有关,换一个匹配的数据库版本的sql包试试。

总结:
1,配置的时候,首先需要配置dao层,也就是mybatis的mapper文件以及sqlMapconfig.xml文件
使用spring开启扫描,并且在spring中配置 数据库,事务,sqlSessionFactory

2.在Service层的配置中 开启扫描注解

3.在mvc.xml中配置controller层,例视图解析器,处理器映射器,处理器适配器,处理器,异常处理器,以及文件上传,拦截器等。

看似复杂,但只要弄清了脉络,瞬间就能掌握

你可能感兴趣的:(学习日记)