SSM整合(springmvc + spring + mybatis)

1.1. 需要的jar包

1.     spring(包括springmvc)

2.     mybatis

3.     mybatis-spring整合包

4.     数据库驱动

5.     第三方连接池。

6.     Json依赖包Jackson

SSM整合(springmvc + spring + mybatis)_第1张图片

1.2. 整合思路

Dao层:

1、SqlMapConfig.xml,

2、applicationContext.xml(和service是同一个,这里我没分开)

a)       数据库连接Druid(阿里巴巴的连接池)

b)       SqlSessionFactory对象,需要spring和mybatis整合包下的。

c)       配置mapper文件扫描器。Mapper动态代理开发增强版

 

Service层:

1、applicationContext-trans.xml配置事务。

 

Controller层:

1、Springmvc.xml

a)       包扫描器,扫描@Controller注解的类。

b)       配置注解驱动

c)       配置视图解析器

d)       配置放行静态资源


Web.xml文件:

1、配置spring监听器

2、配置前端控制器。

3、配置加载读取spring的配置

4、 配置post提交乱码问题

详细配置如下:

web.xml配置


 
  contextConfigLocation
  classpath:applicationContext.xml
 

 
 
  org.springframework.web.context.ContextLoaderListener
 


 
 
  encoding
  org.springframework.web.filter.CharacterEncodingFilter
 
  encoding
  UTF-8
 

 

 
  encoding
  /*
 

  
  
 
 
springmvc  
  org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml


1 
 

 
  springmvc
  /
 


 加入配置文件(mybatis)

1、SqlMapConfig.xml


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">







2、applicationContext.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">




























3、 db.properties(数据库连接配置)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=321

4、log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:\\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout

5、 applicationContext-trans.xml(事物配置)


xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
























pointcut="execution(* cn.itcast.crm.service.*.*(..))"/>

6、 Springmvc.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">









class="org.springframework.web.servlet.view.InternalResourceViewResolver">






这样就把现在最流行的ssm框架大致整合完毕了 ! 



你可能感兴趣的:(SSM整合(springmvc + spring + mybatis))