如何在春天中使用SpringMVC与Mybatis框架

  介绍

今天就跟大家聊聊有关如何在春天中使用SpringMVC与Mybatis框架,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。


基本目录结构

├──, src #,   │,├──,main #,   │,│└──,java #,java代码目录   │,│└──,resources  #,配置文件目录,,存放下面弹簧配置文件   │,├──,test #,单元测试目录   ├──,web #,web目录   │,└──,WEB-INF #,web.xml 配置文件目录

1。Mybatis层编写

1,在<代码> 资源目录下新建数据库配置文件<代码>数据库。属性

 jdbc.driver=com.mysql.jdbc.Driver
  #,如果是使用,MySQL8.0 +,那么还需要增加一个时区的配置;serverTimezone=亚洲/上海
  jdbc.url=jdbc: mysql://localhost: 3306/ssmbuild ? useSSL=true& useUnicode=true& characterEncoding=utf8
  jdbc.username=根
  jdbc。密码=123456 

2,在<代码> 资源目录下创建Mybatis配置文件<代码> mybatis-config。xml代码

 & lt; ? xml  version=?.0“,编码=癠TF-8", ?比;
  & lt; ! DOCTYPE 配置
  ,PUBLIC “-//mybatis.org//DTD  Config  3.0//EN"
  ,“http://mybatis.org/dtd/mybatis-3-config.dtd"比;
  & lt; configuration>
  ,& lt; !——配置数据源,,交给春天——比;
  ,
  ,& lt; !——配置日志——比;
  ,& lt; settings>
  ,& lt; !——STDOUT_LOGGING:标准的日志工厂实现——比;
  ,& lt; setting  name=發ogImpl",价值=https://www.yisu.com/zixun/" STDOUT_LOGGING "/>
  
  
  
  
  <包名=" com.pro.pojo "/>
  
  
  
  <映射器>
  
  
  
   

2。弹簧层编写

1。春天整合Mybatis

配置弹簧整合Mybatis,这里数据源使用c3p0连接池;

编写春天整合Mybatis的相关的配置文件;在资源<代码> 目录下创建<代码> Spring dao。xml代码

注意:这里要引入上面Mybatis层的两个配置文件,配置文件的名称不要写错

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   ,xmlns:上下文=癶ttp://www.springframework.org/schema/context"   xsi: schemaLocation=癶ttp://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"的在      ,& lt; !——1只关联数据库配置文件——比;   ,& lt;上下文:property-placeholder 位置=袄嗦肪?database.properties"/比;      ,& lt; !——2只连接池   ,dbcp:半自动化操作,,不能自动连接   ,c3p0:自动化操作,(自动加载配置文件并设置到对象中)   “光之轮”,德鲁伊,   ,——比;   ,& lt; bean  id=癲ataSource",类=癱om.mchange.v2.c3p0.ComboPooledDataSource"比;   ,& lt; property  name=癲riverClass",价值=https://www.yisu.com/zixun/" $ {jdbc.driver} "/>   <属性名=" jdbcUrl " value=" $ {jdbc.url} "/>   <属性名="用户" value=" $ {jdbc.username} "/>   <属性名="密码" value=" $ {jdbc.password} "/>         <属性名=" maxPoolSize " value=" 30 "/>   <属性名=" minPoolSize " value=" 10 "/>      <属性名=" autoCommitOnClose " value=" false "/>      <属性名=" checkoutTimeout " value=" 10000 "/>      <属性名=" acquireRetryAttempts " value=" 2 "/>                  <属性名=" configLocation " value="类路径:mybatis-config.xml "/>                  <属性名=" sqlSessionFactoryBeanName " value=" sqlSessionFactory "/>   

如何在春天中使用SpringMVC与Mybatis框架