目录

1.配置SpringMVC

新建一个dispatcherServlet-servlet.xml的配置文件

在src/main/java下创建自己要编写的包

编写一下Spring配置文集applicationContext.xml

在src/main/resources下的mapper下建一个mybatis-config.xml配置文件

编写dbconfig.properties文件

编写mybatis的逆向工程配置文件在maven项目工程下面文件名是mbg.xml

在包名为com.dalu.test下面创建一个类名为test的测试类,使用mybatis的逆向工程生成对应的bean及mapper


1.配置SpringMVC

新建一个dispatcherServlet-servlet.xml的配置文件

在src/main/webapp/WEB-INF/dispatcherServlet-servlet.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd>
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
      <!-- springMVC配置文件,包含 网站跳转 逻辑的配置 -->

<!--让它扫描com.dalu.sui下的包-->

<context:component-scan base-package=" com.dalu.sui" use-default-filters="false">
      <!-- 只扫描控制器,控制器是代表转换逻辑的 -->
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
      <!-- 配置视图解析器,方便页面返回解析 -->
      <bean     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
        </bean>
        <!--两个标准配置  -->
        <!-- 将springMVC不能处理的请求交给 tomcat-->
        
        <mvc:default-servlet-handler/>
        <!-- 能支持springmvc更高级的一些功能,JSR303 校验,快速的ajax...来映射我们的动态请求-->
        <mvc:annotation-driven/>

</beans>

在src/main/java下创建自己要编写的包

com.dalu.sui.bean(包含javabean)

com.dalu.sui.controller(所有个控制器)

com.dalu.sui.dao(和数据交互的业务逻辑组件)

com.dalu.sui.service(所有的业务逻辑组件)

com.dalu.sui.test(项目测试类可以放在这里面)

com.dalu.sui.util(工具类可以放在这里面)

编写一下Spring配置文集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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd>
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util-3.0.xsd">
      <!-- 开启组件扫描 -->

<!--Spring配置文件,在这里主要配置业务逻辑有关-->

<!--数据源,事务控制,xxx-->

<!-- 把业务逻辑组件也扫描进来 -->
      <context:component-scan 
      base-package="com.dalu">

<!-- Spring和SpringMVC相比是spring让它不扫控制器,其他的业务逻辑都要扫描进来,而springMVC是全部都要扫描进来 -->
      <context:exclude-filter type="annotation" expression="org.springframework.sterotype.Controller"/>

</context:component-scan >

<!--把写的数据文件也扫描进来dbconfig.peorerties-->

<context:property-placeholder location="classpath:dbconfig.properties"/>

<!--id等于的是数据池包里面的类名首字母要小写,class是包及类名在一起。这是一个数据池-->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <!--注入属性-->
       <property name="driverClass" value="${jdbc.driverClass}"></property>
         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
         <property name="user" value="jdbc.user"></property>
         <property name="password" value="jdbc.password"></property>
     </bean>

<!-- 配置mybatis的整合 -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
         <!-- 指定mybatis全局配置文件的位置 -->
        <property name="configLocation" value="mybatis-config.xml" />  
         <property name="dataSource" ref="dataSource" />  
         <!-- 指定mybatis,mapper文件的位置 -->
         <property name="mapperLocations" value="classpath:mapper/*xml"></property>
    </bean>  
    
      <!--配置拦截器,将Mybatis接口实现接入到ioc容器中  -->
       <bean class="org.mybatis,spring.Mapper.MapperScannerConfiguer">
       <!-- 扫描的所有的dao接口的实现,加入到ioc容器中 -->
       <property name="basePackage" value="com.dalu"></property>
       </bean>

<!-- 事务控制的配置 -->

<bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制住数据源  -->
        <property name="dataSource" ref="dataSource" />
    </bean>

<!-- 开启基于注解的事务,使用xml配置形式的事务(必要主要都是使用配置式) -->
 <aop:config>
   <!-- 切入点表达式 -->
   <aop:pointcut expression="execution(* com.dalu.service..*(..))" 
   id="txPomnit"/>
   <!-- 配置事务增强 -->
   <aop:advisor advice-ref="txPomnit"/>
 </aop:config>
 
 <!-- 配置事务增强,事务如何切入 -->
 <tx:advice id="txAdvice">
   <tx:attributes>
   <!--所有个方法都是事务方法 -->
   <tx:method name="*"/>
   <!--以get开始的所有方法  -->
   <tx:method name="get" read-only="true"/>
   </tx:attributes> 
 </tx:advice>

<!-- Spring配置 文件的核心点(数据源,与mybatis的整合,事务控制)-->

</beans>

在src/main/resources下的mapper下建一个mybatis-config.xml配置文件

<?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">

<!--把setting不好配置的配置到全局文件当中-->

<!--配置setting是在Mybatis官方文档下http://www.mybatis.org/generator/index.html-->
  <configuration>
   <settings>
   <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>

<typeAliases>
     <package name="com.atguigu.cusc"/>
   </typeAliases>
  
  </configuration>

我们不要把数据源写死,写个文件名字为:dbconfig.properties 路径是在src/main/resources/mapper/dbconfig.properties,新建个mapper的文件夹把dbconfig.properties放在mapper下面。

编写dbconfig.properties文件

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sui
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

编写mybatis的逆向工程配置文件在maven项目工程下面文件名是mbg.xml

点进去就是下面这个案例

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

<context id="DB2Tables" targetRuntime="MyBatis3">
  
  
  <commentGenerator>
  <property name="suppressAllComments" value="true" />
</commentGenerator>
  
  <!-- 配置数据库连接 -->
  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
        connectionURL="jdbc:mysql://localhost:3306/ssm_cusc" 
        userId="root"
        password="123456">
    </jdbcConnection>

<javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    
    <!-- 指定javabean生成的位置 -->
    <javaModelGenerator targetPackage="com.dalu.sui.bean"
     targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    
    <!-- 指定sql映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper"  
    targetProject="。\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
     <!-- 指定dao接口生成的位置,mapper生成位置 -->
    <javaClientGenerator type="XMLMAPPER"
     targetPackage="com.dalu.sui.dao"  
     targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    
    
     <!-- table指定每个表生成策略 -->
    <table tableName="tbl_emp" domainObjectName="Employee" >
    </table>
    <table tableName="tbl_dept" domainObjectName="Department"></table>
  </context>
</generatorConfiguration>

mapper文件对应的一个数据类Dao的一个接口

在包名为com.dalu.test下面创建一个类名为test的测试类,使用mybatis的逆向工程生成对应的bean及mapper

要编辑mybatis的逆向工程首先我们要从MyBatis Generator的地址:http://www.mybatis.org/generator/running/runningWithJava.html中找到

publlc void test(){

public static void main(String[] args){

List<String> warnings = new ArrayList<String>();

boolean overwrite = true;

File configFile = new File("generatorConfig.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);

}

}

先执行一下,然后点击项目刷新

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

留给自己观看,同时也代表一种心意吧!希望我的努力能看到成果。谢谢!

Spring,SpringMVC,Mybatis(第二节)相关推荐

  1. Spring+SpringMVC+Mybatis SSM框架详解

    一.JDBC编程 1.JDBC 简介 JDBC其实就是 Java 官方提供的一套规范(接口),用于帮助开发人员快速实现不同关系型数据库的连接. 程序运行的时候,数据都是在内存中的.当程序终止的时候,通 ...

  2. SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    登录 | 注册 收藏成功 确定 收藏失败,请重新收藏 确定 查看所有私信查看所有通知 暂没有新通知 想要绕过微信小程序开发中的坑吗?不妨来听这个,今晚8点,1小时帮你搞定! 14小时以前 CSDN日报 ...

  3. SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

    使用IDEA创建Spring + SpringMVC + MyBatis 框架的Maven的项目. 一. 创建maven项目 1. File -> New Module,进入创建项目窗口. 2. ...

  4. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...

  5. maven多模块项目部署到服务器,GitHub - baxias/foweb: 一个基于 Spring+SpringMVC+Mybatis 的Maven多模块项目。(实现前后端分离的服务器端)...

    Foweb Framework A multi-modules maven project base on Spring+SpringMVC+Mybatis. 一个基于 Spring+SpringMV ...

  6. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十三)数据层优化-表规范、索引优化

    本文提要 最近写的几篇文章都是关于数据层优化方面的,这几天也在想还有哪些地方可以优化改进,结合日志和项目代码发现,关于数据层的优化,还是有几个方面可以继续修改的,代码方面,整合了druid数据源也开启 ...

  7. 如何部署SSM框架(Spring+SpringMVC+MyBatis)到SAE(新浪云服务器)图文教程

    在学习cocos2dx手游开发的过程中,为了实现用户注册.用户登陆和世界排行榜这些模块,需要用到服务器来搭建平台.以前都是 在本地搭建服务器,在本科期间使用过IIS和Tomcat,感觉在本地搭建服务器 ...

  8. SSM框架+WebSocket实现网页聊天(Spring+SpringMVC+MyBatis+WebSocket)

    建站不止于增删改查,还有很多很有魅力的地方.对于通信聊天这块已经青睐好久了,前段时间在做的j2ee项目运用到Spring+SpringMVC+MyBatis的框架集合,是关于一个社交平台的网站,类似于 ...

  9. SMM(Spring+SpringMVC+MyBatis)

    Spring & SpringMVC & MyBatis 一.Spring的体系结构 自下往上: Test Core Container 核心容器 Beans :容器 Core :核心 ...

最新文章

  1. python杨辉三角居中center_python经典---杨辉三角(两种方法)
  2. noip2010关押罪犯
  3. [蓝桥杯][算法提高VIP]分分钟的碎碎念(dfs)
  4. 锐捷官方提供122套实验题.
  5. 在deepin 15.5中安装vs code并配置c/c++环境
  6. C++学习之路 | PTA乙级—— 1041 考试座位号 (15 分)(精简)
  7. 一文读懂 IPv4 到 IPv6 的过渡技术
  8. 浏览器css透明属性opacity
  9. 安装LR提示“此计算机缺少 vc2005_sp1_with_atl_fix_redist,请安装所有缺少的必要组件,然后重新运行此安装“
  10. 机器学习(周志华)——决策树问题
  11. oracle中主键的建立,oracle 建立主键与索引
  12. 【gcc】warning信息梳理
  13. solaris 命令大全
  14. 常见的SSL证书错误代码及解决方法
  15. Android 11 新特性和API兼容
  16. MySQL 从 8.0.31 开始从原来的 mysql:mysql-connector-java 改为 com.mysql:mysql-connector-j
  17. 【pip】raise MaxRetryError(_pool, url, error or ResponseError(cause))
  18. KEBA机器人控制器简介
  19. Kubernetes — CNI 网络插件规范
  20. 【分享】Heic图片如何批量转换成jpg格式?

热门文章

  1. c语言调用函数的方法案例,C语言经典例题100例——C语言练习实例34解答(函数调用)...
  2. Kubernetes集群功能演示:deployment的管理和kubectl的使用
  3. Unity3D 动态加载资源方式
  4. CSDN 第六期编程竞赛做题记录
  5. android 获取设备的mac地址,Android编程获取设备MAC地址的实现方法
  6. 金融交易报文ISO8583协议
  7. spring boot 访问路径404是会转到/error路径,倒是拦截器失效
  8. VC中常见的108个问题
  9. SQL的LEN函数用法及实例
  10. 简历上的项目经历怎么写 ?这 3 条原则不可忽视 !...