Spring——Spring整合MyBatis

news2024/9/23 1:20:37

Spring整合MyBatis

1.创建工程

在这里插入图片描述

1.1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wt</groupId>
    <artifactId>Spring_MyBatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- 项目源码及编译输出的编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 项目编译JDK版本 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring常用依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</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>2.0.3</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>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

1.2.log4j.properties

log4j.rootLogger=DEBUG,A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

1.3.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">
    
    <context:component-scan base-package="com.wt"></context:component-scan>
</beans>

2.配置数据源

2.1.db.properties

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

2.2.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
       ">

    <!--加载配置文件-->
	<context:property-placeholder location="classpath:db.properties" />

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

3.整合MyBatis

3.1.applicationContext.xml

<!--会话工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="com.wt.pojo"></property>
</bean>

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

4.测试

4.1.创建表

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `money` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

4.2.pojo

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;
    }
}

4.3.mapper

public interface UserMapper {

    public void addUser(User user);
}
<?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.wt.mapper.UserMapper">
    <insert id="addUser" parameterType="User">
		insert into t_user(name,money) values(#{name},#{money})
	</insert>
</mapper>

4.4.service

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

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

4.5.junit

package com.by.test;

import com.by.pojo.User;
import com.by.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class ServiceTest {
    
    @Autowired
    private UserService userService;

    @Test
    public void testAdd(){
        userService.addUser(new User("张三丰",4000F));

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

R语言频率分布直方图绘制教程

本篇笔记分享R语言绘制直方图的方法&#xff0c;通过多种展示风格对数据进行可视化&#xff0c;主要用到ggplot、ggpubr等包。 什么是直方图&#xff1f; 直方图(Histogram)&#xff0c;又称质量分布图&#xff0c;是一种统计报告图&#xff0c;由一系列高度不等的柱子表示数…

前端适配750px设计稿

全局引入 (function(doc, win) {const docEl doc.documentElement,resizeEvt orientationchange in window ? orientationchange : resizeconst setFont function() {let clientWidth docEl.clientWidth;if (!clientWidth) return;if (clientWidth > 750) {docEl.styl…

社科院与美国杜兰大学金融管理硕士项目——金融在职人员的当下与未来

随着经济的蓬勃发展和全球化的疾驰&#xff0c;金融行业已稳坐现代经济的心脏位置。在这翻涌的时代浪潮中&#xff0c;金融从业人员的重要性愈发突出&#xff0c;他们不仅是企业的坚实支柱&#xff0c;更是推动经济前行的强大引擎。然而&#xff0c;科技进步和市场变幻的风云也…

深度解析Dubbo的基本应用与高级应用:负载均衡、服务超时、集群容错、服务降级、本地存根、本地伪装、参数回调等关键技术详解

负载均衡 官网地址&#xff1a; http://dubbo.apache.org/zh/docs/v2.7/user/examples/loadbalance/ 如果在消费端和服务端都配置了负载均衡策略&#xff0c; 以消费端为准。 这其中比较难理解的就是最少活跃调用数是如何进行统计的&#xff1f; 讲道理&#xff0c; 最少活跃数…

TSP(Python):Qlearning求解旅行商问题TSP(提供Python代码)

一、Qlearning简介 Q-learning是一种强化学习算法&#xff0c;用于解决基于奖励的决策问题。它是一种无模型的学习方法&#xff0c;通过与环境的交互来学习最优策略。Q-learning的核心思想是通过学习一个Q值函数来指导决策&#xff0c;该函数表示在给定状态下采取某个动作所获…

批量剪辑方法:掌握视频剪辑技巧,按指定时长轻松分割视频

在视频制作和编辑过程中&#xff0c;经常要批量处理和剪辑大量的视频片段。学会批量剪辑方法可以提高工作效率&#xff0c;还可以使视频编辑更加准确和高效。下面来看下云炫AI智剪如何按指定时长轻松分割视频的批量剪辑方法。 分割后的视频文件效果&#xff0c;已分割分段的视…

一、Mybatis 简介

本章概要 简介持久层框架对比快速入门&#xff08;基于Mybatis3方式&#xff09; 1.1 简介 https://mybatis.org/mybatis-3/zh/index.html MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投G…

Prometheus实战篇:Prometheus监控mongodb

Prometheus实战篇:Prometheus监控mongodb 准备环境 docker-compose安装mongodb docker-compose.yaml version: 3 services:mongo:image: mongo:4.2.5container_name: mongorestart: alwaysvolumes:- /data/mongo/db: /data/dbport:- 27017:27017command: [--auth]enviromen…

搭建一个手游平台的价格大概是多少?

随着智能手机的普及和移动互联网的发展&#xff0c;手游行业呈现出爆炸性的增长。许多人都看到了手游市场的巨大潜力&#xff0c;并考虑搭建一个自己的手游平台。那么&#xff0c;搭建一个手游平台的价格大概是多少呢&#xff1f; 首先&#xff0c;我们需要明确手游平台的搭建涉…

Halcon灰度共生矩阵

Halcon灰度共生矩阵 图像的纹理一般具有重复性&#xff0c;纹理单元往往会以一定的规律出现在图像的不同位置&#xff0c;即使存在一些形变或者方向上的偏差&#xff0c;图像中一定距离之内也往往有灰度一致的像素点&#xff0c;这一特性适合用灰度共生矩阵来表现。 灰度共生矩…

软件测试|Django 入门:构建Python Web应用的全面指南

引言 Django 是一个强大的Python Web框架&#xff0c;它以快速开发和高度可扩展性而闻名。本文将带您深入了解Django的基本概念和核心功能&#xff0c;帮助您从零开始构建一个简单的Web应用。 什么是Django&#xff1f; Django 是一个基于MVC&#xff08;模型-视图-控制器&a…

软件测试|MySQL算术运算符使用详解

简介 MySQL是一种流行的开源关系型数据库管理系统&#xff0c;广泛用于各种应用程序和网站的数据存储和管理。在MySQL中&#xff0c;算术运算符是执行数学计算的特殊符号&#xff0c;用于处理数字类型的数据。本文将详细介绍MySQL中常用的算术运算符及其使用方法。 常用算术运…

【仙丹秘法】如何炼制一颗稳定的仙丹

提示词始终保持不变 1&#xff1a;收集素材 制作lora_v1 2: 制作lora_v1 产生 1个人物 含 你想要的服装 导入 pose_1 到 control 1 生成人物 (white_background:1.1),front view,1boy,blue sleeveless t-shirt,blue shorts,detailed eyes,best quality,masterpiece,high res…

3d模型里显示了灯光怎么取消---模大狮模型网

要取消3D模型中的灯光显示&#xff0c;您可以尝试以下方法&#xff1a; 禁用灯光&#xff1a;在您使用的3D软件中&#xff0c;查找并选择灯光对象&#xff0c;然后禁用或隐藏它们。这样可以阻止灯光对场景的渲染影响。 调整灯光属性&#xff1a;如果您不想完全禁用灯光&#x…

模型创建与nn.Module

一、网络模型创建步骤 二、nn.Module 下面描述了在 PyTorch 中常见的一些属性和功能&#xff0c;用于存储和管理神经网络模型的参数、模块、缓冲属性和钩子函数。 parameters&#xff1a;用于存储和管理 nn.Parameter 类的属性。nn.Parameter 是一种特殊的张量&#xff0c;它被…

Spring Boot 接入 KMS 托管中间件密码第三方接口密钥

1. 需求 Nacos中关于中间件的密码&#xff0c;还有第三方API的密钥等信息&#xff0c;都是明文存储&#xff0c;不符合系统安全要求。现需对这些信息进行加密处理&#xff0c;Nacos只存储密文&#xff0c;并在服务启动时&#xff0c;调用云厂商的KMS接口进行解密&#xff0c;将…

【十九】【动态规划】518. 零钱兑换 II、279. 完全平方数、474. 一和零,三道题目深度解析

动态规划 动态规划就像是解决问题的一种策略&#xff0c;它可以帮助我们更高效地找到问题的解决方案。这个策略的核心思想就是将问题分解为一系列的小问题&#xff0c;并将每个小问题的解保存起来。这样&#xff0c;当我们需要解决原始问题的时候&#xff0c;我们就可以直接利…

LoRa网关在智能冷链物流中的应用解决方案

随着物联网技术的不断发展&#xff0c;智能冷链物流成为了物流行业的一个重要领域。在冷链物流中&#xff0c;对于货物的温度、湿度等环境变量的监测和控制非常关键&#xff0c;而这些数据的传输需要一个高效可靠的通信方式。LoRa技术作为一种低功耗广域网通信技术&#xff0c;…

详解java继承

目录 一 、为什么需要继承 二、准备工作&#xff1a;用java代码先定义狗类、猫类、动物类&#xff0c;这是代码准备如下 三、继承代码实现 四、 子类中访问父类的成员方法 4.1. 成员方法名字不同 4.2 成员方法名字相同 五、子类构造方法 扩展&#xff1a;如果你对子类和…

jvm虚拟机初识

JVM Java虚拟机就是二进制字节码的运行环境&#xff0c;负责装载字节码到其内部&#xff0c;解释/编译为对应平台上的机器指令执行。每一条Java指令&#xff0c;Java虚拟机规范中都有详细定义&#xff0c;如怎么取操作数&#xff0c;怎么处理操作数&#xff0c;处理结果放在哪…