>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring》、《Spring Cloud Alibaba微服务开发零基础入门到实操》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 27002 个阅读者 刷新本主题
 * 贴子主题:  史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码) 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-01-12 17:44:50     消息  查看  搜索  好友  邮件  复制  引用

            

史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)

   前言:

网上很多整合SSM博客文章并不能让初探ssm的同学思路完全的清晰,可以试着关掉整合教程,摇两下头骨,哈一大口气,就在万事具备的时候,开整,这个时候你可能思路全无 <sub>中招了咩</sub> ,还有一些同学依旧在使用eclipse或者Myeclipse开发,我想对这些朋友说IDEA 的编译速度很快,人生苦短,来不及解释了,直接上手idea吧。这篇文章每一步搭建过程都测试过了,应该不会有什么差错。本文章还有个比较优秀的特点,就是idea的使用,基本上关于idea的操作都算是比较详细的,所以不用太担心不会撸idea!最后,本文章旨在清晰地整合各个框架之间的流程与思路。

     相信有很多小伙伴都是学了SSM框架,而且学的时候应该是用eclipse或者Myeclipse开发的,随着idea崛起,实力碾压eclipse,IDEA 的编译速度很快,通常比ec快2倍!外加丧心病狂的代码提示引起程序员的尖叫!当然,我不是说eclipse不好,只是idea更加便捷,更加便于开发,这是事实。我相信很多小伙伴都是从eclipse转向idea(包括我,普遍一开始上手不习惯idea,时刻保持一颗畏惧敬畏的心,生怕敲不出一行靓丽的Hello Word(你们都是大神,记得当时的我一直syso,敲不出一行输出语句QAQ),作为过来人,我告诉大家,这些都是没必要的担心,你只要记住,具(工具idea)在我手中,码(代码)就在我手中,管他三七四十九呢,拿起idea就是一顿撸码,我就是酱紫的,所以呢才有了这篇idea版的SSM框架整合,不然你以为怎么来的?(好像有点装bi,感觉要被打…)    

  文章目录

前言:
1. 搭建整合环境
1. 整合说明
2. 整合的思路:
3. 创建数据库和表结构语句:
4. 创建maven的工程
5. 编写实体类,在Twossm_domain项目中编写
6. 编写dao接口
7. 编写service接口和实现类
2、Spring框架代码的编写
1、创建resources的资源文件目录管理XML配置文件
2、编写applicationContext.xml的配置文件
3. 在项目中编写测试方法,进行测试
3、SpringMVC框架代码的编写
1. 在web.xml中配置DispatcherServlet前端控制器
2. 在web.xml中配置DispatcherServlet过滤器解决中文乱码
3. web.xml中配置的整体效果
4. 创建springmvc.xml的配置文件,编写配置文件
5.创建jsp页面,并编写controller代码
6.部署Tomcat进行测试
7.测试运行
4. Spring整合SpringMVC的框架
1、Spring整合SpringMVC的框架原理分析
2、在web.xml中配置ContextLoaderListener监听器
3. controller中注入service对象,调用service对象方法并测试
5、MyBatis框架代码的编写
1、在IAccountdao接口方法上添加注解,编写SQL语句
2.创建SqlMapConfig.xml的配置文件并编写
3. 创建并编写Mybatis测试方法
6. Spring整合MyBatis框架
1、在applicationContext.xml中配置数据库连接池
2、在applicationContext.xml中配置SqlSessionFactory工厂
3、在applicationContext.xml中配置IAccountdao接口所在包
4、小结上面的三个配置
5、完善Service层代码
6、完善Controller层代码
7、完善list.jsp页面
8、运行测试
7.spring整合mybatis框架配置事务(Spring的声明式事务管理)
1、在applicationContext.xml中配置Spring框架声明式事务管理
2、完善index.jsp页面
3、完善Service层、Controller层代码
4、测试运行
8、源码、源码、源码~重要的标题发三遍    

   1. 搭建整合环境

1. 整合说明

  整合说明:SSM整合可以使用多种方式,咱们选择XML + 注解的方式,不要觉得不妥,这样其实最便捷-

2. 整合的思路:

             1、先搭建整合的环境

             2、先把Spring的配置搭建完成
  
            3、再使用Spring整合SpringMVC框架
  
          4、之后使用Spring整合MyBatis框架

          5、最后spring整合mybatis框架配置事务(Spring的声明式事务管理)

3. 创建数据库和表结构语句:

  复制在MySQL中运行即可:

create database ssm;

use ssm;

create table account (

id int primary key auto_increment,

name varchar(50),

money double

);

4. 创建maven的工程

具体的使用idea创建maven,请看这篇使用IntelliJ IDEA创建第一个Mawen项目
  1. 创建Twossm_parent父工程(打包方式选择pom,必须的)
  2. 创建Twossm_web子模块(打包方式是war包)
  3. 创建Twossm_service子模块(打包方式是jar包)
  4. 创建Twossm_dao子模块(打包方式是jar包)
  5. 创建Twossm_domain子模块(打包方式是jar包)
  6. web依赖于service,service依赖于dao,dao依赖于domain
  7. 在Twossm_parent的pom.xml文件中引入坐标依赖

    找到对应的< properties >标签,以及< dependencies >标签,复制粘贴即可

    版本控制是在< properties >标签中控制,从坐标依赖中可以看出版本号:spring5X、MySQL3.1.6、mybatis3.4.5
   <properties >
     <project .build .sourceEncoding >UTF - 8 < /project .build .sourceEncoding >
     <maven .compiler .source > 1.7 < /maven .compiler .source >
     <maven .compiler .target > 1.7 < /maven .compiler .target >
      <spring .version > 5.0 .2 .RELEASE < /spring .version >
      <slf4j .version > 1.6 .6 < /slf4j .version >
      <log4j .version > 1.2 .12 < /log4j .version >
      <mysql .version > 5.1 .6 < /mysql .version >
      <mybatis .version > 3.4 .5 < /mybatis .version >
   < /properties >

      <dependencies >
      <dependency >
        <groupId >org .aspectj < /groupId >
        <artifactId >aspectjweaver < /artifactId >
        <version > 1.6 .8 < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -aop < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -context < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -web < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -webmvc < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -test < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -tx < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .springframework < /groupId >
        <artifactId >spring -jdbc < /artifactId >
        <version >$ {spring .version } < /version >
     < /dependency >
      <dependency >
        <groupId >junit < /groupId >
        <artifactId >junit < /artifactId >
        <version > 4.12 < /version >
        <scope >compile < /scope >
     < /dependency >
      <dependency >
        <groupId >mysql < /groupId >
        <artifactId >mysql -connector -java < /artifactId >
        <version >$ {mysql .version } < /version >
     < /dependency >
      <dependency >
        <groupId >javax .servlet < /groupId >
        <artifactId >servlet -api < /artifactId >
        <version > 2.5 < /version >
        <scope >provided < /scope >
     < /dependency >
      <dependency >
        <groupId >javax .servlet .jsp < /groupId >
        <artifactId >jsp -api < /artifactId >
        <version > 2.0 < /version >
        <scope >provided < /scope >
     < /dependency >
      <dependency >
        <groupId >jstl < /groupId >
        <artifactId >jstl < /artifactId >
        <version > 1.2 < /version >
     < /dependency >  < ! -- log start  -- >
      <dependency >
        <groupId >log4j < /groupId >
        <artifactId >log4j < /artifactId >
        <version >$ {log4j .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .slf4j < /groupId >
        <artifactId >slf4j -api < /artifactId >
        <version >$ {slf4j .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .slf4j < /groupId >
        <artifactId >slf4j -log4j12 < /artifactId >
        <version >$ {slf4j .version } < /version >
     < /dependency >  < ! -- log end  -- >
      <dependency >
        <groupId >org .mybatis < /groupId >
        <artifactId >mybatis < /artifactId >
        <version >$ {mybatis .version } < /version >
     < /dependency >
      <dependency >
        <groupId >org .mybatis < /groupId >
        <artifactId >mybatis -spring < /artifactId >
        <version > 1.3 .0 < /version >
     < /dependency >
      <dependency >
        <groupId >c3p0 < /groupId >
        <artifactId >c3p0 < /artifactId >
        <version > 0.9 .1 .2 < /version >
        <type >jar < /type >
        <scope >compile < /scope >
     < /dependency >
   < /dependencies >

  1. 部署Twossm_web的项目,只要把Twossm_web项目加入到tomcat服务器中即可

5. 编写实体类,在Twossm_domain项目中编写

在这里,我提醒一下,可能各位不熟悉idea快捷键,就比如说getset方法搞不出来哈哈,就这篇整合SSM的文章所用到的idea快捷键可以参考下面蓝色字体文章(点击蓝色字体即可),熟悉idea的哥们可以当我没说,当我在放pi(快快快捂住鼻子…)

IDEA快速实现接口、查找接口的实现、getSet方法快速生成等等常用快捷键    

  package com .gx .domain ;

import java .io .Serializable ;

public  class  Account  implements  Serializable  {
     private Integer id ;
     private String name ;
     private Double money ;

     public Integer  getId ( )  {
         return id ;
     }

     public  void  setId (Integer id )  {
         this .id  = id ;
     }

     public String  getName ( )  {
         return name ;
     }

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

     public Double  getMoney ( )  {
         return money ;
     }

     public  void  setMoney (Double money )  {
         this .money  = money ;
     }

    @Override
     public String  toString ( )  {
         return  "Account{"  +
                 "id="  + id  +
                 ", name='"  + name  +  '\''  +
                 ", money="  + money  +
                 '}' ;
     }
}

6. 编写dao接口

在dao包中编写dao接口IAccountdao    

  package com .gx .dao ;

import com .gx .domain .Account ;
import org .apache .ibatis .annotations .Insert ;
import org .apache .ibatis .annotations .Select ;
import org .springframework .stereotype .Repository ;

import java .util .List ;

public  interface  IAccountdao  {

     public List <Account >  findAll ( ) ;
  
     public  void  saveAccount (Account account ) ;
}

7. 编写service接口和实现类

service接口:    

  package com .gx .service ;

import com .gx .domain .Account ;

import java .util .List ;

public  interface  AccountService  {
     // 查询所有账户
     public List  <Account >  findAll ( ) ;

     // 保存帐户信息
     public  void  saveAccount (Account account ) ;
}

  service接口实现类:    

  package com .gx .service .Impl ;

import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .springframework .stereotype .Service ;

import java .util .List ;
@Service ( "accountService" )
public  class  AccountServiceImpl  implements  AccountService  {

     @Override
     public List  <Account >  findAll ( )  {
        System .out . println ( "Service业务层:查询所有账户..." ) ;
         return null ;
     }

     @Override
     public  void  saveAccount (Account account )  {
        System .out . println ( "Service业务层:保存帐户..." ) ;
     }
}

到这里,整合环境就搭建好了效果如下,接下来搭建Spring的配置!

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

   2、Spring框架代码的编写

搭建和测试Spring的开发环境

1、创建resources的资源文件目录管理XML配置文件

创建一个叫resources的资源文件目录,用来管理放置XML配置文件

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

2、编写applicationContext.xml的配置文件

在resources资源文件中创建applicationContext.xml的配置文件,编写具体的配置信息

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

applicationContext.xml中的配置信息    

  < ?xml version = "1.0" encoding = "UTF-8" ? >
<beans xmlns = "http://www.springframework.org/schema/beans"
       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 /aop
http : / /www .springframework .org /schema /aop /spring -aop .xsd
http : / /www .springframework .org /schema /tx
http : / /www .springframework .org /schema /tx /spring -tx .xsd" >

     < ! --开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理 -- >
     <context :component -scan base - package = "com.gx"  >
         < ! --配置哪些注解不扫描 -- >
         <context :exclude -filter type = "annotation" expression = "org.springframework.stereotype.Controller"  / >
     < /context :component -scan >
    
< /beans >

3. 在项目中编写测试方法,进行测试

1、创建Test包

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

2、在test包中创建一个叫TestSpring的class类,具体的内容如下:    

  package com .gx .test ;

import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .junit .Test ;
import org .springframework .context .ApplicationContext ;
import org .springframework .context .support .ClassPathXmlApplicationContext ;

public  class  TestSpring  {
     @Test
     public  void  run1 ( ) {
        ApplicationContext ac  =  new  ClassPathXmlApplicationContext ( "classpath:applicationContext.xml" ) ;
        AccountService as  =  (AccountService ) ac . getBean ( "accountService" ) ;
        as . findAll ( ) ;
     }
}

运行如下效果,说明搭建Spring的开发环境成功!

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

到这里,Spring的开发环境成功!接下来搭建SpringMVC框架环境。

   3、SpringMVC框架代码的编写

搭建和测试SpringMVC的开发环境

1. 在web.xml中配置DispatcherServlet前端控制器

    < ! --配置前端控制器 -- >
    <servlet >
      <servlet -name >dispatcherServlet < /servlet -name >
       <servlet - class >org .springframework .web .servlet .DispatcherServlet < /servlet - class >
       < ! --加载springmvc .xml配置文件 -- >
       <init -param >
           <param -name >contextConfigLocation < /param -name >
           <param -value >classpath :springmvc .xml < /param -value >
       < /init -param >
       < ! --启动服务器,创建该servlet -- >
       <load -on -startup > 1 < /load -on -startup >
   < /servlet >
     <servlet -mapping >
         <servlet -name >dispatcherServlet < /servlet -name >
         <url -pattern > / < /url -pattern >
     < /servlet -mapping >

2. 在web.xml中配置DispatcherServlet过滤器解决中文乱码

  < ! --解决中文乱码的过滤器 -- >
     <filter >
         <filter -name >characterEncodingFilter < /filter -name >
         <filter - class >org .springframework .web .filter .CharacterEncodingFilter < /filter - class >
         <init -param >
             <param -name >encoding < /param -name >
             <param -value > UTF - 8 < /param -value >
         < /init -param >
     < /filter >
     <filter -mapping >
         <filter -name >characterEncodingFilter < /filter -name >
         <url -pattern > /*</url-pattern>
    </filter-mapping>

3. web.xml中配置的整体效果

  < !DOCTYPE web -app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd"  >

<web -app xmlns :xsi = "http://www.w3.org/2001/XMLSchema-instance"
        xmlns = "http://java.sun.com/xml/ns/javaee"
        xsi :schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version = "3.0" >
   <display -name >Archetype Created Web Application < /display -name >
  
     < ! --配置前端控制器 -- >
    <servlet >
      <servlet -name >dispatcherServlet < /servlet -name >
       <servlet - class >org .springframework .web .servlet .DispatcherServlet < /servlet - class >
       < ! --加载springmvc .xml配置文件 -- >
       <init -param >
           <param -name >contextConfigLocation < /param -name >
           <param -value >classpath :springmvc .xml < /param -value >
       < /init -param >
       < ! --启动服务器,创建该servlet -- >
       <load -on -startup > 1 < /load -on -startup >
   < /servlet >
     <servlet -mapping >
         <servlet -name >dispatcherServlet < /servlet -name >
         <url -pattern > / < /url -pattern >
     < /servlet -mapping >

     < ! --解决中文乱码的过滤器 -- >
      <filter >
         <filter -name >characterEncodingFilter < /filter -name >
         <filter - class >org .springframework .web .filter .CharacterEncodingFilter < /filter - class >
         <init -param >
             <param -name >encoding < /param -name >
             <param -value >UTF - 8 < /param -value >
         < /init -param >
     < /filter >
     <filter -mapping >
         <filter -name >characterEncodingFilter < /filter -name >
         <url -pattern > /*</url-pattern>
    </filter-mapping>
</web-app>

4. 创建springmvc.xml的配置文件,编写配置文件

同样是在resources资源文件夹中创建springmvc.xml配置文件

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

springmvc.xml的配置文件内容:    

< ?xml version = "1.0" encoding = "UTF-8" ? >
<beans xmlns = "http://www.springframework.org/schema/beans"
       xmlns :mvc = "http://www.springframework.org/schema/mvc"
       xmlns :context = "http://www.springframework.org/schema/context"
       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 .xsd
        http : / /www .springframework .org /schema /mvc
        http : / /www .springframework .org /schema /mvc /spring -mvc .xsd
        http : / /www .springframework .org /schema /context
        http : / /www .springframework .org /schema /context /spring -context .xsd" >
     < ! --开启注解扫描,只扫描Controller注解 -- >
     <context :component -scan base - package = "com.gx" >
         <context :include -filter type = "annotation" expression = "org.springframework.stereotype.Controller" / >
     < /context :component -scan >

     < ! --配置的视图解析器对象 -- >
     <bean id = "internalResourceViewResolver"  class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
         <property name = "prefix" value = "/WEB-INF/pages/" / >
         <property name = "suffix" value = ".jsp" / >
     < /bean >
     < ! --过滤静态资源 -- >
     <mvc :resources location = "/css" mapping = "/css/**" / >
     <mvc :resources location = "/images/" mapping = "/images/**" / >
     <mvc :resources location = "/js/" mapping = "/js/**" / >
     < ! --开启SpringMVC注解的支持 -- >
     <mvc :annotation -driven / >
< /beans >

5.创建jsp页面,并编写controller代码

编写index.jsp页面    

  < %@ page contentType = "text/html;charset=UTF-8" language = "java"  % >
  <html >
  <body >
<a href = "account/findAll" >测试SpringMVC查询 < /a >
< /body >
< /html >

在controller层中的AccountController的class类中编写代码    

  package com .gx .controller ;

import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .stereotype .Controller ;
import org .springframework .ui .Model ;
import org .springframework .web .bind .annotation .RequestMapping ;

import java .util .List ;

@Controller
public  class  AccountController  {

     @RequestMapping ( "/account/findAll" )
     public String  findAll ( ) {
        System .out . println ( "Controller表现层:查询所有账户..." ) ;
         return  "list" ;   //在视图解析器中配置了前缀后缀
     }
}

这时候就要创建controller跳转的list.jsp页面了:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

list.jsp页面创建好了,编写一下内容,只是看是否跳转成功,输出一句话即可:    

  < %@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"  % >
< % --
  Created by IntelliJ IDEA .
  User : Bule
  Date :  2019 / 9 / 2
  Time :  7 : 32
  To change  this template use File  | Settings  | File Templates .
-- % >
< %@ page contentType = "text/html;charset=UTF-8" language = "java"  % >

  <html >
  <head >
      <title >Title < /title >
< /head >
  <body >
              <h2 >查询所有的账户 < /h2 >
          
< /body >
< /html >

6.部署Tomcat进行测试

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

7.测试运行

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

到这里,spring、springmvc的开发环境就都搭建好了,不容易啊,都坚持看到这里了,给你点个赞,接下来是整合spring和springmvc了!

   4. Spring整合SpringMVC的框架

整合之前,想一想,怎样去整合Spring、SpringMVC框架呢,怎么才算是整合成功了呢,带着问题,一起来吧!

1、Spring整合SpringMVC的框架原理分析

整合成功的表现:在controller(SpringMVC)中能成功的调用service(Spring)对象中的方法。要想在controller中调用service方法,就要注入service到controller中来,有service对象才可以调用service方法,方法是这样没有错,但是有一个问题,就是启动Tomcat之后试想一下,在web.xml中配置有前端控制器,web容器会帮我们加载springmvc.xml配置文件,在springmvc.xml配置文件中我们配置情况是只扫描controller,别的不扫,而spring.xml文件就从头到尾没有执行过,spring中的配置扫描自然也不会去扫描,就相当于没有将spring交到IOC容器当中去,所以,现在的解决方案就是,在 启动服务器时就加载spring配置文件,怎么实现呢?这时候监听器listener就派上用场了,具体实现如下:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

2、在web.xml中配置ContextLoaderListener监听器

在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。要想加载applicationContext.xml的配置文件有两种方法,第一种(不建议):

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

第二种(强烈建议):在web.xml中配置加载路径    

   < ! --配置Spring的监听器,默认只加载WEB -INF目录下的applicationContext .xml配置文件 -- >
      <listener >
         <listener - class >org .springframework .web .context .ContextLoaderListener < /listener - class >
     < /listener >
  < ! --设置配置文件的路径 -- >
     <context -param >
         <param -name >contextConfigLocation < /param -name >
         <param -value >classpath :applicationContext .xml < /param -value >
     < /context -param >

至于为啥强烈建议第二种呢,是因为我们在整合过程中会有许多配置文件,我们自定义一个类似pages资源文件夹专门管理这些配置文件,方便管理,方便维护!!!

3. controller中注入service对象,调用service对象方法并测试

这时候,启动服务器时也会加载spring配置文件了,那么,我们可以在controller中注入service了,于是开始编写controller代码:    

  package com .gx .controller ;

import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .stereotype .Controller ;
import org .springframework .ui .Model ;
import org .springframework .web .bind .annotation .RequestMapping ;

import java .util .List ;

@Controller
public  class  AccountController  {

     @Autowired    //按类型注入
     private AccountService accountService ;

     @RequestMapping ( "/account/findAll" )
     public String  findAll (Model model ) {
        System .out . println ( "Controller表现层:查询所有账户..." ) ;

        List  <Account > list  = accountService . findAll ( ) ;
         return  "list" ;
     }
}

编写完成,开始测试,启动Tomcat,效果

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

到这里,点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

总算是整合完了spring、springmvc,同学你还能看到这里,我也挺欣慰的哈哈,手动再给你点个赞,接下来编写MyBatis环境惹!

   5、MyBatis框架代码的编写

一看到Mybatis,就要想到dao,没错,MyBatis环境搭建首先是dao,搭建mybatis,之前要编写mapper映射的配置文件,其实挺麻烦的,所以我选择使用注解!

1、在IAccountdao接口方法上添加注解,编写SQL语句

  package com .gx .dao ;

import com .gx .domain .Account ;
import org .apache .ibatis .annotations .Insert ;
import org .apache .ibatis .annotations .Select ;
import org .springframework .stereotype .Repository ;

import java .util .List ;

@Repository   //此注解代表这是一个持久层,用法类似@controller、@service
public  interface  IAccountdao  {

     @Select ( "select * from account" )
     public List  <Account >  findAll ( ) ;
     @Insert ( "insert into account (name,money) value(#{name},#{money})" )
     public  void  saveAccount (Account account ) ;
}

2.创建SqlMapConfig.xml的配置文件并编写

和之前创建配置文件一样,创建:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

编写:    

  < ?xml version = "1.0" encoding = "UTF-8" ? >  < !DOCTYPE configuration PUBLIC  "-//mybatis.org//DTD Config 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-config.dtd" >
  <configuration >
     <environments  default = "mysql" >
         <environment id = "mysql" >
             <transactionManager type = "JDBC" / >
             <dataSource type = "POOLED" >
                 <property name = "driver" value = "com.mysql.jdbc.Driver" / >
                 <property name = "url" value = "jdbc:mysql:///ssm" / >
                 <property name = "username" value = "root" / >
                 <property name = "password" value = "root" / >
             < /dataSource >
         < /environment >
     < /environments >
     < ! -- 使用的是注解  -- >
      <mappers >
         < ! --  <mapper  class = "com.gx.dao.IAccountdao" / >  -- >  < ! -- 该包下所有的dao接口都可以使用  -- >
         < package name = "com.gx.dao" / >
     < /mappers >
< /configuration >

  因为我使用的是注解,我觉得还是有必要提一下以下三种方法:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

3. 创建并编写Mybatis测试方法

创建:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

编写:    

  package com .gx .test ;

import com .gx .dao .IAccountdao ;
import com .gx .domain .Account ;
import org .apache .ibatis .io .Resources ;
import org .apache .ibatis .session .SqlSession ;
import org .apache .ibatis .session .SqlSessionFactory ;
import org .apache .ibatis .session .SqlSessionFactoryBuilder ;
import org .junit .Test ;

import java .io .IOException ;
import java .io .InputStream ;
import java .util .List ;

public  class  TestMyBatis  {

     @Test
     public  void  run1 ( )  throws IOException  {
        Account account  = new  Account ( ) ;
        account . setName ( "杜永蓝" ) ;
        account . setMoney ( 200d ) ;
         // 加载配置文件
        InputStream in  = Resources . getResourceAsStream ( "SqlMapConfig.xml" ) ;
         // 创建SqlSessionFactory对象
        SqlSessionFactory factory  =  new  SqlSessionFactoryBuilder ( ) . build (in ) ;
         // 创建SqlSession对象
        SqlSession session  = factory . openSession ( ) ;
         // 获取到代理对象
        IAccountdao dao  = session . getMapper (IAccountdao . class ) ;

         // 保存
        dao . saveAccount (account ) ;

         // 提交事务
        session . commit ( ) ;

         // 关闭资源
        session . close ( ) ;
        in . close ( ) ;
     }
    
     @Test
     public  void  run2 ( )  throws Exception  {
        InputStream in  = Resources . getResourceAsStream ( "SqlMapConfig.xml" ) ;

        SqlSessionFactory factory  =  new  SqlSessionFactoryBuilder ( ) . build (in ) ;

        SqlSession session  = factory . openSession ( ) ;

        IAccountdao dao  = session . getMapper (IAccountdao . class ) ;

        List  <Account > list  = dao . findAll ( ) ;
         for  (Account account : list  )  {
            System .out . println (account ) ;
         }

        session . close ( ) ;
        in . close ( ) ;
     }

       }

运行测试:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

运行效果:

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

到这里,mybatis环境搭建算是完成了,点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小,哈哈接下来搭建最后整合spring、mybatis!(估计看到这里,你也饿了吧)

   6. Spring整合MyBatis框架

Spring整合MyBatis框架之前,先想一想,怎样才算整合成功呢?其实,这和之前的spring整合springMVC的套路差不多,其实就是,Service能成功调用dao对象,能够做查询操作或者新增数据能存进数据库。现在spring已经是在IOC容器中了,dao是一个接口,可以通过程序帮这个接口生成代理对象,我们要是可以把这个代理对象也放进IOC容器,那么service就可以拿到这个对象,之后在service中做一个注入,service从而调用dao代理对象的方法,那么我们怎么去实现dao接口生成的代理对象放入IOC容器呢?其实很简单,只需要如下操作!

整合目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中

1、在applicationContext.xml中配置数据库连接池

至于为啥要配池子,我不说大家应该也知道,毕竟各位都是学过ssm的大神了点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小    

  < ! --Spring整合MyBatis框架 -- >
     < ! --配置连接池 -- >
     <bean id = "dataSource"  class = "com.mchange.v2.c3p0.ComboPooledDataSource" >
         <property name = "driverClass" value = "com.mysql.jdbc.Driver" / >
         <property name = "jdbcUrl" value = "jdbc:mysql:///ssm" / >
         <property name = "user" value = "root" / >
         <property name = "password" value = "root" / >
     < /bean >

2、在applicationContext.xml中配置SqlSessionFactory工厂

没配置工厂之前,我们用Test测试的时候,每次都要先创建工厂,因为工厂能够给我们创建SqlSession,有了SqlSession就可以通过SqlSession拿到代理对象。现在我们直接在applicationContext.xml中配置SqlSessionFactory工厂,这就相当于IOC容器中有了工厂,就可以去创建SqlSession,进而通过SqlSession拿到代理对象,没必要每次测试都去创建工厂。    

   < ! --配置SqlSessionFactory工厂 -- >
<bean id = "sqlSessonFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
     <property name = "dataSource" ref = "dataSource" / >
  < /bean >

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

3、在applicationContext.xml中配置IAccountdao接口所在包

因为工厂有了,SqlSession也有了,那代理谁呢,所以我们要配置IAccountdao接口所在包,告诉SqlSession去代理接口所在包中的代理,从而存到IOC容器中    

   < ! --配置IAccountdao接口所在包 -- >
<bean id = "mapperScanner"  class = "org.mybatis.spring.mapper.MapperScannerConfigurer" >
     <property name = "basePackage" value = "com.gx.dao" / >
< /bean >

4、小结上面的三个配置

其实,上面的操作就是 把mybatis中的配置(SqlMapConfig.xml)转移到spring中去,让它产生代理并存到IOC容器中

5、完善Service层代码

在AccountServiceImpl实现类中编写代码:    

  package com .gx .service .Impl ;

import com .gx .dao .IAccountdao ;
import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .stereotype .Service ;

import java .util .List ;
@Service ( "accountService" )
public  class  AccountServiceImpl  implements  AccountService  {

     @Autowired
     private IAccountdao iaccountdao ;

     @Override
     public List  <Account >  findAll ( )  {
        System .out . println ( "Service业务层:查询所有账户..." ) ;
         return iaccountdao . findAll ( ) ;
     }

     @Override
     public  void  saveAccount (Account account )  {
        System .out . println ( "Service业务层:保存帐户..." ) ;
     }
}

6、完善Controller层代码

  package com .gx .controller ;

import com .gx .domain .Account ;
import com .gx .service .AccountService ;
import org .springframework .beans .factory .annotation .Autowired ;
import org .springframework .stereotype .Controller ;
import org .springframework .ui .Model ;
import org .springframework .web .bind .annotation .RequestMapping ;

import java .util .List ;

@Controller
public  class  AccountController  {

     @Autowired
     private AccountService accountService ;

     @RequestMapping ( "/account/findAll" )
     public String  findAll (Model model ) {   //存数据, Model对象
        System .out . println ( "Controller表现层:查询所有账户..." ) ;
         // 调用service的方法
        List  <Account > list  = accountService . findAll ( ) ;
        model . addAttribute ( "list" ,list ) ;
         return  "list" ;
     }
}

7、完善list.jsp页面

因为要使用到jstl显示数据库数据,所以list.jsp页面如下:    

  < % --
  Created by IntelliJ IDEA .
  User : Bule
  Date :  2019 / 9 / 2
  Time :  7 : 32
  To change  this template use File  | Settings  | File Templates .
-- % >
< %@ page contentType = "text/html;charset=UTF-8" language = "java" isELIgnored = "false"  % >
< %@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"  % >

  <html >
  <head >
      <title >Title < /title >
< /head >
  <body >
      <h2 >查询所有的账户 < /h2 >
     <c :forEach items = "${list}" var = "account" >
        $ {account .name }
     < /c :forEach >
< /body >
< /html >

8、运行测试

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

到这里,SSM整合就基本完成,各位可以去打王者楼喽,咳咳等等…还没完成,我只是说基本完成。点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

接下来,spring整合mybatis框架还需要配置事务(Spring的声明式事务管理),至于为啥,一张图告诉你!(哎呀…ai…ai…别打…别打…别打脸…)

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

   7.spring整合mybatis框架配置事务(Spring的声明式事务管理)

细心的小伙伴可能发现了,我在整合spring、mybatis测试的时候(TestMybatis中),新增数据保存的时候手动的提交过事务 session.commit(),如果不写这一句,就会出现数据没提交的情况,因此为了完美的整合ssm,我们必须配置Spring的声明式事务管理!

1、在applicationContext.xml中配置Spring框架声明式事务管理

如果对一些execution表达式什么的不太清除的或者AOP不是特别印象深刻,可以去看看我的这篇文章【Spring框架学习总结二】Spring的AOP通俗理解以及AOP的入门开发    

   < ! --配置Spring框架声明式事务管理 -- >
     < ! --配置事务管理器 -- >
     <bean id = "transactionManager"  class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         <property name = "dataSource" ref = "dataSource"  / >
     < /bean >

     < ! --配置事务通知 -- >
     <tx :advice id = "txAdvice" transaction -manager = "transactionManager" >
         <tx :attributes >
             <tx :method name = "find*" read -only = "true" / >
             <tx :method name = "*" isolation = "DEFAULT" / >
         < /tx :attributes >
     < /tx :advice >

     < ! --配置AOP增强 -- >
     <aop :config >
         <aop :advisor advice -ref = "txAdvice" pointcut = "execution(* com.gx.service.Impl.*ServiceImpl.*(..))" / >
     < /aop :config >

2、完善index.jsp页面

  < %@ page contentType = "text/html;charset=UTF-8" language = "java"  % >
  <html >
  <head >
      <title >Title < /title >
< /head >
  <body >

<a href = "account/findAll" >测试查询 < /a >

  <h3 >测试包 < /h3 >

<form action = "account/save" method = "post" >
    姓名: <input type = "text" name = "name"  / > <br / >
    金额: <input type = "text" name = "money"  / > <br / >
     <input type = "submit" value = "保存" / > <br / >
< /form >

< /body >
< /html >

3、完善Service层、Controller层代码

Service层:在AccountServiceImpl实现类中调用service中的saveAccount(account)方法    

   @Override
     public  void  saveAccount (Account account )  {
        System .out . println ( "Service业务层:保存帐户..." ) ;
        iaccountdao . saveAccount (account ) ;   //调用service中的saveAccount(account)方法
     }

Controller层代码:在AccountController类中添加一个保存save的方法    

      @RequestMapping ( "/account/save" )
     public  void  save (Account account , HttpServletRequest request , HttpServletResponse response )  throws IOException  {
        accountService . saveAccount (account ) ;
        response . sendRedirect (request . getContextPath ( ) + "/account/findAll" ) ;
         return ;
     }

4、测试运行

点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

OK ,到这里,能够坚持到这里的各位都是人才,说话有好听,敲的代码又好看,我超喜欢你们,所以,我要是哪里写错了,欢迎指出,还望不吝赐教!!!点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

   8、源码、源码、源码~重要的标题发三遍

同时附上源码给各位,毕竟坚持看到最后,也是真的不容易(点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

     链接:https://pan.baidu.com/s/1lYtogVNBvK2taR1luDCdoA

    提取码:htov

欢迎各位关注我的公众号,一起探讨技术,向往技术,追求技术,说好了来了就是盆友喔…

     点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

                                                                                                                
----------------------------
原文链接:https://blog.csdn.net/qq_44543508/article/details/100192558

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 sunshine 在 2020-01-13 12:19:00 重新编辑]
  Java面向对象编程-->流程控制
  JavaWeb开发-->Servlet技术详解(Ⅰ)
  JSP与Hibernate开发-->持久化层的映射类型
  Java网络编程-->Java反射机制
  精通Spring-->组合(Composition)API
  Vue3开发-->通过Vuex进行状态管理
  MessagePack反序列化使用示例
  面试官问什么是Spring循环依赖,该如何回答?
  如何编写优雅的Spring架构API
  微服务架构模型
  SSE(Server Sent Events) HTTP服务端推送详解 - hadoop_a9bb...
  微服务的拆分方式
  @InitBinder注解的用法
  通过Spring JDBC API访问数据库
  Spring MVC数据验证
  springboot集成通用mapper实现Echarts
  开发一个Java项目的基本流程
  Spring Boot集成EHCache实现缓存机制
  Spring Boot集成shiro ehcache(使用shiro的缓存管理)
  微架构 springcloud-07. springboot-静态资源处理
  一篇文章概括Spring Cloud微服务教程
  更多...
 IPIP: 已设置保密
楼主      
1页 4条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。