Spring整合MyBatis框架!!!

搭建环境:

pom.xml:

<properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- Spring常用依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.2</version>
        </dependency>
        <!--日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <build>
        <!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

db.properties:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3305/spring?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=

log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--ioc-->
    <context:component-scan base-package="com.by.service"></context:component-scan>
    <!--加载db.properties-->
    <context:property-placeholder location="db.properties"></context:property-placeholder>

    <!--配置数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName();
        dataSource.setUrl();
        dataSource.setUsername();
        dataSource.setPassword();
    -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--
        SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = sessionFactoryBuilder.build("");
        SqlSession sqlSession = sessionFactory.openSession();
        Object mapper = sqlSession.getMapper();
    -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--扫描basePackage所指定的包下的所有接口,生成代理类并交给spring管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--mapper所在的包-->
        <property name="basePackage" value="com.by.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

 UserMapper:

package com.by.mapper;

import com.by.pojo.User;

public interface UserMapper {

    public void addUser(User user);
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.by.mapper.UserMapper">

    <insert id="addUser" parameterType="com.by.pojo.User">
        insert into t_user(name,money) values(#{name},#{money})
    </insert>
</mapper>

 User:(实体类)

package com.by.pojo;

public class User {
    private Integer id;
    private String name;
    private Float money;

    public User(String name, Float money) {
        this.name = name;
        this.money = money;
    }

    public User() {
    }

    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 Float getMoney() {
        return money;
    }

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

UserService:

package com.by.service;

import com.by.pojo.User;

/**
 * <p>Project: Spring - UserService</p>
 * <p>Powered by scl On 2024-01-05 21:24:07</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public interface UserService {
    void addUser(User user);
}

UserServiceImpl

package com.by.service;

import com.by.mapper.UserMapper;
import com.by.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }
}

测试类:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.web;

import com.alibaba.druid.pool.DruidDataSource;
import com.by.pojo.User;
import com.by.service.UserService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Project: Spring - Client</p>
 * <p>Powered by scl On 2024-01-05 20:12:56</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = ac.getBean("userServiceImpl", UserService.class);
        userService.addUser(new User("张三丰",4000F));

        userService.addUser(new User("宋远桥",2000F));
    }
}

结果展示:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/296994.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spring 整合MyBatis

创建工程 pom.xml <?xml version"1.0" encoding"UTF-8"?> 4.0.0 <groupId>com.by</groupId> <artifactId>Spring_MyBatis</artifactId> <version>1.0-SNAPSHOT</version><properties><!-- 项目源码…

高可用分布式部署Spark、完整详细部署教程

前言 Spark 是 UC Berkeley AMP Lab 开源的通用分布式并行计算框架。 Spark基于map reduce算法实现的分布式计算&#xff0c;拥有Hadoop MapReduce所具有的优点&#xff1b;但不同于MapReduce的是Job中间输出和结果可以保存在内存中&#xff0c;从而不再需要读写HDFS&#xff…

数据采集有哪些方法?HTTP代理起到什么作用?

在这个数字化的时代&#xff0c;数据就如同生活中不可或缺的元素&#xff0c;我们的行为、喜好、甚至是想法都被转化成了数字化的信息。那么&#xff0c;现代社会是如何进行数据的采集的呢&#xff1f;让我们一同来看看&#xff01; 1. 网络浏览行为的追踪 在我们浏览互联网的…

【Windows】之微软输入法配置小鹤双拼

前言 Windows 自带的输入法微软输入法本身就是个最简洁、最方便的输入法&#xff0c;不需要去安装多余的第三方输入法软件。同时&#xff0c;微软中文拼音输入法支持双拼输入法&#xff0c;但微软自带的双拼输入法不包含小鹤双拼方案的。所以&#xff0c;在这里将会讲解如何配置…

原生微信小程序如何动态修改svg图片颜色及尺寸、宽高(封装svgIcon组件)解决ios不显示问题

最终效果 前言 动态设置Svg图片颜色就是修改Svg源码的path中的fill属性&#xff0c; 通过wx.getFileSystemManager().readFile读取.xlsx文件 ios不显示需要把encoding设置 binary 把文件转成base64 封装svg-icon组件 1、在项目的components下新建svg-icon文件夹&#xff0c;新…

antd Table 动态数据 合并单元格(合并行)

antd Table 组件动态合并单元格 最近处理table的时候 遇到了要合并同一列的几行的情况&#xff0c;比如第一列的前面三行都是同一个对象的名字&#xff0c;此时合并显示比较妥当&#xff0c;但是数据是后端接口来的&#xff0c;而且可以筛选条件&#xff0c;搜索出来的数据就是…

目标检测 | YOLOv5 训练自标注数据集实现迁移学习

Hi&#xff0c;大家好&#xff0c;我是源于花海。本文主要了解 YOLOv5 训练自标注数据集&#xff08;自行车和摩托车两种图像&#xff09;进行目标检测&#xff0c;实现迁移学习。YOLOv5 是一个非常流行的图像识别框架&#xff0c;这里介绍一下使用 YOLOv5 给使用 Labelme 标注…

AI模型部署落地综述(ONNX/NCNN/TensorRT等)

导读 费尽心血训练好的深度学习模型如何给别人展示&#xff1f;只在服务器上运行demo怎么吸引别人的目光&#xff1f;怎么才能让自己的成果落地&#xff1f;这篇文章带你进入模型部署的大门。 0 前言 模型部署的步骤&#xff1a; 训练一个深度学习模型&#xff1b; 使用不同…

NNDL总结

第四章 前馈神经网络 4.1 神经元 人工神经元&#xff0c;简称神经元&#xff0c;是构成神经网络的基本单元。 当>0时&#xff0c;为1&#xff0c;兴奋&#xff1b; 当<0时&#xff0c;为0&#xff0c;抑制。 激活函数的性质 1、连续可导的非线性函数。 2、激活函数及其导…

C语言 B树的分析与实现

本文主要说明了B树的概念、应用以及如何用C语言实现B树。 概述 有使用过数据库的朋友都知道&#xff0c;数据库需要存储大量的数据&#xff0c;并且查询数据的性能也需要一定的保证。那么数据库的底层数据结构是如何实现的呢&#xff0c;就是我们要讨论的B树和B树&#xff0c…

【电源专题】电池充放电中常说的0.2C是什么概念

在工作中我们时常会听到老员工说拿这个电池去做一下充放电,以0.2C充,0.2C放。那么这个0.2C到底是啥? 这就要说到电池C-rate概念。在《GB 31241:便携式电子产品用锂离子电池和电池安全要求》中我们可以看到3.7中写了额定容量为C,也就是制造商标明的电池或电池组容量。 那么…

src refspec master does not match any

新项目推送至 Git 空仓库时抛出如下异常 src refspec master does not match any 初始化 init 都做了但反复尝试 git push -u origin master 均无果 后发现权限不够 .... 起初设置为开发者,后变更为了主程序员再次尝试 push 成功 .... 以上便是此次分享的全部内容&#xff0c;…

MyBatisPlus学习二:常用注解、条件构造器、自定义sql

常用注解 基本约定 MybatisPlus通过扫描实体类&#xff0c;并基于反射获取实体类信息作为数据库表信息。可以理解为在继承BaseMapper 要指定对应的泛型 public interface UserMapper extends BaseMapper<User> 实体类中&#xff0c;类名驼峰转下划线作为表名、名为id的…

etcd储存安装

目录 etcd介绍: etcd工作原理 选举 复制日志 安全性 etcd工作场景 服务发现 etcd基本术语 etcd安装(centos) 设置&#xff1a;etcd后台运行 etcd 是云原生架构中重要的基础组件&#xff0c;由 CNCF 孵化托管。etcd 在微服务和 Kubernates 集群中不仅可以作为服务注册…

单片机大小端模式

单片机大小端模式 参考链接 单片机干货-什么是大小端_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1Ju4y1M7Tx/?spm_id_from333.337.search-card.all.click&vd_sourcee821a225c7ba4a7b85e5aa6d013ac92e 特此记录 anlog 2024年1月2日

C语言——内存函数【memcpy,memmove,memset,memcmp】

&#x1f4dd;前言&#xff1a; 在之前的文章C语言——字符函数和字符串函数&#xff08;一&#xff09;中我们学习过strcpy和strcat等用来实现字符串赋值和追加的函数&#xff0c;那么除了字符内容&#xff0c;其他的数据&#xff08;例如整型&#xff09;能否被复制或者移动呢…

IDEA2023 最新版详细图文安装教程(Java环境搭建+IDEA安装+运行测试+汉化+背景图设置)

IDEA2023 最新版详细图文安装教程 名人说&#xff1a;工欲善其事&#xff0c;必先利其器。——《论语》 作者&#xff1a;Code_流苏(CSDN) o(‐&#xff3e;▽&#xff3e;‐)o很高兴你打开了这篇博客&#xff0c;跟着教程去一步步尝试安装吧。 目录 IDEA2023 最新版详细图文安…

Linux第15步_安装FTP客户端

安装完FTP服务器后&#xff0c;还需要安装FTP客户端&#xff0c;才可以实现Ubuntu系统和Windows系统进行文件互传。 1、在STM32MP157开发板A盘基础资料\03软件中&#xff0c;找到“FileZilla_3.51.0_win64-setup.exe”&#xff0c;双击它&#xff0c;就可以安装。 2、点击“I …

吉他打谱软件Guitar Pro8苹果Mac电脑简体中文特别版

Guitar Pro 8 Mac是一款吉他编曲学习软件&#xff0c;用于吉他、贝和其他弦乐器的制谱和演奏&#xff0c;这是一个多轨编辑器&#xff0c;具有集成的 MIDI 编辑器、合唱绘图仪、吉他、节拍器和其他音乐家工具。它使您能够编辑吉他、贝司和尤克里里、乐谱、指法谱&#xff0c;并…

浅谈云可观测性的关键组件及重要性

在云计算时代&#xff0c;企业和开发团队不仅需要关注应用程序的功能性&#xff0c;还需要关心系统在生产环境中的性能、可用性和可靠性。云可观测性成为一项关键的技术实践&#xff0c;它涵盖了日志、指标、追踪等多个方面&#xff0c;为系统监控和故障诊断提供了全方位的支持…