SSM整合小案例

SSM(Spring+SpringMVC+Mybatis)

    一、添加jar包

            ①直接添加

                目前整合所用到的jar包:

                        aspectjweaver-1.8.9.jar
        c3p0-0.9.1.jar
        commons-logging-1.2.jar
        jackson-annotations-2.8.0.jar
        jackson-core-2.8.8.jar
        jackson-databind-2.8.8.jar
        jsqlparser-0.9.5.jar
        jstl-1.2.jar
        mybatis-3.4.2.jar
        mybatis-generator-core-1.3.5.jar
        mybatis-spring-1.3.1.jar
        mysql-connector-java-5.1.41.jar
        pagehelper-5.0.0.jar
        spring-aop-4.3.7.RELEASE.jar
        spring-aspects-4.3.7.RELEASE.jar
        spring-beans-4.3.7.RELEASE.jar
        spring-context-4.3.7.RELEASE.jar
        spring-core-4.3.7.RELEASE.jar
        spring-expression-4.3.7.RELEASE.jar
        spring-jdbc-4.3.7.RELEASE.jar
        spring-tx-4.3.7.RELEASE.jar
        spring-web-4.3.7.RELEASE.jar

        spring-webmvc-4.3.7.RELEASE.jar

SSM整合小案例_第1张图片

            ②Maven方式添加:pom.xml

                


com.github.pagehelper
pagehelper
5.0.0


org.springframework
spring-web
4.3.7.RELEASE



org.springframework
spring-webmvc
4.3.7.RELEASE





org.springframework
spring-jdbc
4.3.7.RELEASE




org.springframework
spring-aspects
4.3.7.RELEASE



org.springframework
spring-test
4.3.7.RELEASE
test





org.mybatis
mybatis
3.4.2





org.mybatis
mybatis-spring
1.3.1





c3p0
c3p0
0.9.1



mysql
mysql-connector-java
5.1.41




jstl
jstl
1.2


javax.servlet
javax.servlet-api
3.1.0
provided




junit
junit
4.12
test



org.mybatis.generator
mybatis-generator-core
1.3.5




com.fasterxml.jackson.core
jackson-databind
2.8.8


二、配置文件

                数据库相关信息:dbconfig.properties

                        jdbc.driver=com.mysql.jdbc.Driver
                        jdbc.url=jdbc:mysql://192.168.233.128:3306/mybatis
                        jdbc.username=root
                        jdbc.password=*********

                        #jdbc.driver=oracle.jdbc.OracleDriver
                        #jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
                        #jdbc.username=scott

                        #jdbc.password=scott

SSM整合小案例_第2张图片

               mybatis的配置文件:mybatis-config.xml


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

SSM整合小案例_第3张图片


web.xml的配配置:



  mybatis_configure
 
    index.html
    index.jsp
 

 
  contextConfigLocation
  classpath:applicationContext.xml
 

 
  org.springframework.web.context.ContextLoaderListener
 

 
 
  dispatcherServlet
  org.springframework.web.servlet.DispatcherServlet
  1
 

 
  dispatcherServlet
  /
 

SSM整合小案例_第4张图片

web.xml没有配置字符编码过滤器,需要自己添加

SpringMVC的配置:dispatcherServlet-servlet.xml

【注:当wen.xml不指定springmvc配置文件位置的时候,可以用这种方式来进行配置(servlet名-servlet.xml)】

SSM整合小案例_第5张图片


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">












SSM整合小案例_第6张图片

spring的配置文件:applicationContext.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-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">















































SSM整合小案例_第7张图片

SSM整合小案例_第8张图片

mybatis的逆向工程配置:MBG.xml

官网:http://www.mybatis.org/mybatis-3/


  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

 
 
               
           

 
            connectionURL="jdbc:mysql://192.168.233.128:3306/mybatis"
        userId="root"
        password="koonisession97">
   


   
     
   


        targetProject=".\src">
     
     
   


        targetProject=".\conf">
     
   


          targetProject=".\src">
     
   


   
     


 

逆向工程代码:TestMBG.java

官网:http://www.mybatis.org/generator

package com.hz.mybatis.test;

import java.io.File;
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 TestMBG {
public static void main(String[] args) throws Exception {
  List warnings = new ArrayList();
   boolean overwrite = true;
   File configFile = new File("mybatis-generator.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);
}

}

SSM整合小案例_第9张图片

到此配置文件就编写完成,接下来进行测试:

SSM整合小案例_第10张图片


SSM整合小案例_第11张图片

JSP页面:

SSM整合小案例_第12张图片

效果:

SSM整合小案例_第13张图片

点击:显示数据

SSM整合小案例_第14张图片

点击:清除数据


over~~~

你可能感兴趣的:(备忘,代码)