SSM框架整合

xiaoxiao2021-02-27  208

SSM框架整合

一,基本概念

1,spring

Spring是一个轻量级别的控制反转(IOC)和面向切面(AOP)的容器框架

2,springmvc

SpringMVC分离了控制器,模型对象,分派器以及处理程序对象的角色,这种分离让他们更容易进行制定

3,MyBatis

基于java的持久层框架,MyBatis 使用简单的xml或者注解用于配置和原始映射,将接口和java的pojo映射成数据库中的记录

二,开发环境搭建

三,Maven Web项目搭建

四,SSM整合

1,Maven需要引入的jar包

​ ​

2,spring与MyBatis属性文件

1,建立JDBC属性文件

jdbc.properties

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost/mysql

username=root

password=root

maxActive=20

maxIdle=20

minidle=1

maxWait=60000

2,建立spring-mybatis.xml配置文件

spring-mybatis.xml是用来完成spring和mybatis的整合,主要包括:自动扫描,自动注入,配置数据库

3,Log4j的配置

通过配置Log4j,使用日志来输出信息,

#定义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

4,Junit测试

1,创建测试用表
2,利用Mybatis Generator自动创建代码

逆向工程可以根据表自动创建实体类,MyBatis映射文件以及DAO接口,

3,建立Service接口和实现类
4,建立测试类
package org.zsl.testmybatis; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.fastjson.JSON; import com.cn.hnust.pojo.User; import com.cn.hnust.service.IUserService; @RunWith(SpringJUnit4ClassRunner.class) //表示继承了SpringJUnit4ClassRunner类 @ContextConfiguration(locations = {“classpath:spring-mybatis.xml”}) public class TestMyBatis { ​ private static Logger logger = Logger.getLogger(TestMyBatis.class); // private ApplicationContext ac = null; ​ @Resource ​ private IUserService userService = null; // @Before // public void before() { // ac = new ClassPathXmlApplicationContext(“applicationContext.xml”); // userService = (IUserService) ac.getBean(“userService”); // } ​ @Test ​ public void test1() { ​ User user = userService.getUserById(1); ​ // System.out.println(user.getUserName()); ​ // logger.info(“值:”+user.getUserName()); ​ logger.info(JSON.toJSONString(user)); ​ } }

5,整合SpringMVC

1,配置spring-mvc.xml

springmvc.xml配置文件主要是,自动扫描控制器,视图模式,注解的启动

2,配置web.xml

​ 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 ​
3,测试
1,新建jsp页面
2,建立userController类
3,部署项目
转载请注明原文地址: https://www.6miu.com/read-12500.html

最新回复(0)