mybatis+spring组合使用

本文详细介绍了如何在SpringBoot项目中配置C3P0数据源、整合MyBatis的SqlSessionFactoryBean以及设置事务管理,包括SpringXML配置文件的应用和Mapper的扫描与切面编程的运用。
摘要由CSDN通过智能技术生成

1、mybatis+spring

  1. 配置 applicationContext.xml 数据库使用c3p0 sqlSessionFactory被集成为一个bean

    <?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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        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">
        
        <!-- 引入数据库配置文件 -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        <!-- 数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <!-- 初始连接池大小  -->
            <property name="initialPoolSize" value="2"/>
            <!-- 连接池中连接最小个数 -->
            <property name="minPoolSize" value="1"/>
            <property name="maxPoolSize" value="5"/>
        </bean> 
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!--扫描mapper 并称为bean-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="com.mapper"></property>
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    
        </bean>
        
      <!-- 配置事务管理 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 注入数据源  保证了数据库的四大特性-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 通知 -->     
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为 每次调用这些方法都会启动 事务管理  每个在config里配置的方法都要以以下方式开头-->
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            </tx:attributes>
        </tx:advice>
        <!-- 切点 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.buyerApp.*.*.*.service.*(..))"/>
        </aop:config> <!--每次调用service层的数据库语句都会收到上面各个方法的通知,若代码错误 数据库就会回滚  -->
        
         <bean id="user" class="com.pojo.User"></bean>
    </beans>

  2. 配置数据库连接信息 jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/buyerapp?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123456
    ​
    jdbc.initialPoolSize=5
    jdbc.minPoolSize=1
    jdbc.maxPoolSize=10
  3. 配置逆向工程文件 generator.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>
       <!--
                    suppressAllComments属性值:
                        true:自动生成实体类、SQL映射文件时没有注释
                        false:自动生成实体类、SQL映射文件,并附有注释
                  -->
       <property name="suppressAllComments" value="true" />
      </commentGenerator>
      <!-- 数据库连接信息 -->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
       connectionURL="jdbc:mysql://localhost:3306/buyerapp?characterEncoding=UTF-8" 
       userId="root"  password="123456">
      </jdbcConnection>
      <!-- 
                forceBigDecimals属性值: 
                    true:把数据表中的DECIMAL和NUMERIC类型,
    解析为JAVA代码中的java.math.BigDecimal类型 
                    false(默认):把数据表中的DECIMAL和NUMERIC类型,
    解析为解析为JAVA代码中的Integer类型 
            -->
      <javaTypeResolver>
       <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>
       
          <!-- 
                targetProject属性值:实体类pojo的生成位置  
                targetPackage属性值:实体类所在包的路径
            --> 
      <javaModelGenerator targetPackage="com.pojo"
                                 targetProject="Spring-Mybatis/src">
       <!-- trimStrings属性值:
                    true:对数据库的查询结果进行trim操作
                    false(默认):不进行trim操作
                  -->
       <property name="trimStrings" value="true" />
      </javaModelGenerator>
       <!-- 
                targetProject属性值:SQL映射文件的生成位置(写数据库语句类)  
                targetPackage属性值:SQL映射文件所在包的路径(xml)
            -->
       
      <sqlMapGenerator targetPackage="com.mapper" targetProject="Spring-Mybatis/src">
      </sqlMapGenerator>
       
      <!-- 生成动态代理的接口  -->
      <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="Spring-Mybatis/src">
      </javaClientGenerator>
     
      <!-- 指定数据库表  -->
      <table tableName="admin">
      <property name="useActualColumnNames" value="false"/>
       </table>
          
     </context>
    </generatorConfiguration>
  4. 测试

        public static void main(String[] args) {
            // TODO Auto-generated method stub
              ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
              UserMapper userMapper =  (UserMapper) ac.getBean("userMapper");
              User user = (User)ac.getBean("user");
              user.setUserPhone("123");
              user.setUserPwd("321");
              int result = userMapper.insert(user);
              if (result>0) {
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        }
  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
layui是一款基于JavaScript的前端UI框架,用于快速构建美观、交互友好的网页界面。它提供了丰富的UI组件和交互功能,可以帮助开发人员高效地进行前端开发Spring Boot是一个用于简化Spring应用程序开发的框架,它通过自动配置和约定优于配置的方式,让开发人员可以更专注于业务逻辑的实现,而不是繁琐的配置。Spring Boot还提供了很多常用的功能库和第三方插件的集成,可以大大提高开发效率。 MyBatis是一款优秀的持久层框架,可以帮助开发人员将数据库操作与业务逻辑分离,提供了灵活、简单且强大的数据访问方式。MyBatis提供了很多注解和XML配置文件的方式,可以方便地进行SQL语句的编写和执行。同时,MyBatis还提供了缓存机制和插件机制,可以进一步优化数据库操作的性能。 MySQL是一种关系型数据库管理系统,它被广泛应用于各种规模的应用程序中。MySQL提供了稳定、可靠和高性能的数据库服务,支持标准的SQL查询语言和事务处理。在开发过程中,我们可以通过连接MySQL数据库来存储和管理应用程序的数据。 综上所述,layui、Spring Boot、MyBatis和MySQL可以一起使用来构建具有美观、高效和可靠性的Web应用程序。layui提供了丰富的前端UI组件和交互功能,Spring Boot可以简化后端业务逻辑的开发MyBatis可以实现数据库操作的分离和优化,而MySQL可以提供稳定和高性能的数据库服务。这样的组合可以大大提高开发效率和系统性能,是一种常见的技术栈选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小陈编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值