javaee spring整合mybatis

news2024/10/7 10:23:02

案例一

包含dao层

创建maven webapp项目

maven仓库需要改为阿里云
在这里插入图片描述

引入依赖

<?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>org.example</groupId>
  <artifactId>testSSM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>testSSM Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 1.导入Spring相关的jar包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

    <!-- 导入mybatis的jar包 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.37</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- 配置日志信息-->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- spring 整合 mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

  </dependencies>

  <build>
    <resources>
      <resource>
        <!-- 将Mapper的映射文件拷贝出来 -->
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
    <finalName>testSSM</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

spring配置文件

<?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:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <!-- 1.扫描包中的注解 -->
    <context:component-scan base-package="com.test" />

    <!--导入db.properties文件-->
    <context:property-placeholder location="classpath:db.properties" />

    <!--创建了数据源 -->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />
    </bean>

    <!--创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="comboPooledDataSource" />
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    </bean>

    <!-- 扫描mapper文件 生成代理对象 替代之前dao层的操作 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.test.mapper" />
    </bean>

</beans>

数据库信息

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/itstar
user=itstar
password=yyy123456

mybatis配置文件

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

    <!--配置db.properties  转移到spring中-->
    <!--<properties resource="db.properties" />-->
    <!-- 配置日志管理 -->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!-- 设置别名-->
    <typeAliases>
        <package name="com.test.pojo" />
    </typeAliases>

    <!-- 转移到spring配置文件中 -->
    <!--<environments default="development">-->
        <!--<environment id="development">-->
            <!--<transactionManager type="JDBC"/>-->
            <!--<dataSource type="POOLED">-->
                <!--<property name="driver" value="${driverClass}"/>-->
                <!--<property name="url" value="${url}"/>-->
                <!--<property name="username" value="${user}"/>-->
                <!--<property name="password" value="${password}"/>-->
            <!--</dataSource>-->
        <!--</environment>-->
    <!--</environments>-->

     <!--注册mapper文件 转移到spring中-->
    <!--<mappers>-->
       <!--<package name="com.test.mapper" />-->
    <!--</mappers>-->

</configuration>   

dao层

package com.test.dao;

import com.test.pojo.Items;

import java.util.List;

public interface IItemsDao {

    public List<Items> selectItems();
}

package com.test.dao.impl;

import com.test.dao.IItemsDao;
import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

//实现类
@Component
public class ItemsDao implements IItemsDao {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    //转移到Spring的配置文件中  让Spring创建SqlSessionFactory对象
//    public ItemsDao()
//    {
//        InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("sqlMapConfig.xml");
//
//        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
//
//        this.sqlSessionFactory= sqlSessionFactoryBuilder.build(inputStream);
//    }

    //查询所有的商品信息
        @Override
        public List<Items> selectItems() {

        SqlSession sqlSession= sqlSessionFactory.openSession();

        ItemsMapper itemsMapper= sqlSession.getMapper(ItemsMapper.class);

        return itemsMapper.selectItems();

        }
}

mapper

package com.test.mapper;

import com.test.pojo.Items;

import java.util.List;

//创建一个接口  代理ItemsMapper.xml文件
public interface ItemsMapper {

    public List<Items> selectItems();
}

<?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.test.mapper.ItemsMapper">

    <!-- 查询 -->
    <select id="selectItems" resultType="Items">
        select * from items
    </select>


</mapper>

service层

package com.test.service;

import com.test.pojo.Items;

import java.util.List;

public interface IItemsService {

    public List<Items> selectItems();
}

package com.test.service.impl;

import com.test.dao.IItemsDao;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//service层的实现类

@Service
public class ItemsService implements IItemsService {

    @Autowired
    private IItemsDao itemsDao;

    public IItemsDao getItemsDao() {
        return itemsDao;
    }

    public void setItemsDao(IItemsDao itemsDao) {
        this.itemsDao = itemsDao;
    }

    @Override
    public List<Items> selectItems() {
        return itemsDao.selectItems();
    }
}

测试类

package com.test.service;

import com.test.pojo.Items;
import com.test.service.impl.ItemsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestItemsService {

    @Test
    public void test()
    {
        //测试service层的查询方法
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        IItemsService itemsService= applicationContext.getBean("itemsService",IItemsService.class);

        List<Items> itemsList= itemsService.selectItems();

        System.out.println(itemsList);
    }
}

案例二

省略dao层,由spring代替

在案例一的基础上,修改spring配置文件

<?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:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

    <!-- 1.扫描包中的注解 -->
    <context:component-scan base-package="com.test" />

    <!--导入db.properties文件-->
    <context:property-placeholder location="classpath:db.properties" />

    <!--创建了数据源 -->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />
    </bean>

    <!--创建SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="comboPooledDataSource" />
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    </bean>

    <!-- 扫描mapper文件 生成代理对象 替代之前dao层的操作 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.test.mapper" />
    </bean>

</beans>

删除dao层

修改service层

package com.test.service.impl;

import com.test.mapper.ItemsMapper;
import com.test.pojo.Items;
import com.test.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//service层的实现类

@Service
public class ItemsService implements IItemsService {

    //spring会帮我们创建一个代理对象( 实现了ItemsMapper接口的),代理对象代理的是dao层的功能
    @Autowired
    private ItemsMapper itemsMapper;

    public ItemsMapper getItemsMapper() {
        return itemsMapper;
    }

    public void setItemsMapper(ItemsMapper itemsMapper) {
        this.itemsMapper = itemsMapper;
    }

    @Override
    public List<Items> selectItems() {
        return itemsMapper.selectItems();
    }
}

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

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

相关文章

【赠书活动第3期】《构建新型网络形态下的网络空间安全体系》——用“价值”的视角来看安全

目录 一、内容简介二、读者受众三、图书目录四、编辑推荐五、获奖名单 一、内容简介 经过30多年的发展&#xff0c;安全已经深入到信息化的方方面面&#xff0c;形成了一个庞大的产业和复杂的理论、技术和产品体系。 因此&#xff0c;需要站在网络空间的高度看待安全与网络的…

面向无线传感器网络WSN的增强型MODLEACH设计与仿真(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

Spring Cloud Gateway实现数字签名与URL动态加密

文章目录 什么是数字签名&#xff1f;Spring Cloud Gateway的基础实现数字签名与URL动态加密步骤1&#xff1a;添加依赖步骤2&#xff1a;配置路由步骤3&#xff1a;实现数字签名过滤器步骤4&#xff1a;实现数字签名验证步骤5&#xff1a;实现URL动态加密 结论 &#x1f389;欢…

FFmpeg 命令:从入门到精通 | FFmpeg 解码流程

FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 解码流程 FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 解码流程流程图FFmpeg 解码的函数FFmpeg 解码的数据结构补充小知识 FFmpeg 命令&#xff1a;从入门到精通 | FFmpeg 解码流程 本内容参考雷霄骅博士的 FFmpeg 教程。 流…

视频降噪一些原理

视频降噪&#xff0c;除去部分有可能错误的信息&#xff0c;替换为猜测的可能正确的信息。 真实的细节减少。 亮度低是因为光子少。光子多亮度高。 误差存在&#xff0c;放大倍数越大&#xff0c;误差越大&#xff0c;就会显得噪点越多。 减少噪点&#xff1a; 增加进光量&a…

动图gif怎么做?分享一招超简单方法

常见的图片格式有jpg、png以及gif格式&#xff0c;其中gif格式的图片因为其画面内容丰富生动所以深受大家的喜爱。那么&#xff0c;如何将jpg、png格式的图片转换成gif格式动图呢&#xff1f;通过使用GIF中文网的gif制作&#xff08;https://www.gif.cn/&#xff09;功能&#…

[管理与领导-113]:IT人看清职场中的隐性规则 - 10 - 看清人的行动、行为、手段、方法背后的动机与背景条件

目录 前言&#xff1a; 一、冰山模型 1.1 冰山模型&#xff0c;系统思考的工具 1.2 冰山模型&#xff1a;发现人行为背后的动机 二、动机、行为模型 "说一套"&#xff1a; "做一套"&#xff1a; "演一套"&#xff1a; "学一套&quo…

C++笔记之不同buffer数量下的生产者-消费者机制

C笔记之不同buffer数量下的生产者-消费者机制 文章目录 C笔记之不同buffer数量下的生产者-消费者机制0.在不同的缓冲区数量下&#xff0c;生产者-消费者机制的实现方式和行为的区别1.最简单的生产者-消费者实现&#xff1a;抄自 https://mp.weixin.qq.com/s/G1lHNcbYU1lUlfugXn…

手机或者电脑连接局域网内的虚拟机(网桥)

手机或者电脑连接局域网内的虚拟机&#xff08;网桥&#xff09; 手机软件&#xff1a;ConnectBot&#xff0c;Termius&#xff0c;JuiceSSH … 1.虚拟机vmware中添加桥接网卡 这里桥接网卡选择的是自动&#xff0c;是自动生成动态IP&#xff0c;如果不需要动态生成&#xff…

通达信和同花顺能否实现程序化自动交易股票,量化交易如何实现?

以下写给正在寻找自动交易接口的朋友&#xff0c;首先&#xff0c;不是那种设置个简单条件的条件单&#xff0c;或者某些客户端上形同鸡肋的策略交易&#xff0c;那些策略根本称不上策略&#xff0c;还有各种限制&#xff0c;不支持这个不支持那个&#xff0c;可设置的参数也不…

通过融合UGV的地图信息和IMU的惯性测量数据,实现对车辆精确位置和运动状态的估计和跟踪研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

【MySQL】表的基础增删改查

前面我们已经知道怎么来创建表了&#xff0c;接下来就来对创建的表进行一些基本操作。 这里先将上次创建的表删除掉&#xff1a; mysql> use test; Database changedmysql> show tables; ---------------- | Tables_in_test | ---------------- | student | -----…

redis持久化与调优

一 、Redis 高可用&#xff1a; 在web服务器中&#xff0c;高可用是指服务器可以正常访问的时间&#xff0c;衡量的标准是在多长时间内可以提供正常服务&#xff08;99.9%、99.99%、99.999%等等&#xff09;。但是在Redis语境中&#xff0c;高可用的含义似乎要宽泛一些&#x…

联想M7216NWA一体机连接WiFi及手机添加打印机方法

联想M7216NWA一体机连接WiFi方法&#xff1a; 1、首先按打印机操作面板上的“功能键”&#xff1b;【用“”&#xff08;上翻页&#xff09;“-”&#xff08;下翻页&#xff09;来选择菜单的内容】 2、下翻页键找到并选择“网络”&#xff0c;然后“确认键”&#xff1b; 3…

javaee ssm框架整合例子 ssm例子,需要哪些依赖,配置文件如何配置

项目结构 步骤一&#xff0c;创建springmybatis项目 参考上一篇博客 步骤二&#xff0c;融入SpringMVC 添加依赖 <?xml version"1.0" encoding"UTF-8"?><project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http:…

ArcGIS Engine:视图菜单的创建和鹰眼图的实现

目录 01 创建项目 1.1 通过ArcGIS-ExtendingArcObjects创建窗体应用 1.2 通过C#-Windows窗体应用创建窗体应用 1.2.1 创建基础项目 1.2.2 搭建界面 02 创建视图菜单 03 鹰眼图的实现 3.1 OnMapReplaced事件的触发 3.2 OnExtentUpdated事件的触发 04 稍作演示 01 创建项目…

Centos7 安装mysql 8.0.34并设置不区分大小写

索引 Centos7 安装mysql 8.0.34准备工作安装教程安装并配置配置MySQL配置远程访问重新启动MySQL服务 为已安装的MySQL8设置不区分大小写背景操作步骤 Centos7 安装mysql 8.0.34 准备工作 centos7 服务器 xshell 安装教程 安装并配置 在安装MySQL之前&#xff0c;我们应该…

CSS 实现:常见布局

1 设备与视口 设备屏幕尺寸是指屏幕的对角线长度。像素是计算机屏幕能显示一种特定颜色的最小区域&#xff0c;分为设备像素和逻辑像素。 在 Apple 的视网膜屏&#xff08;Retina&#xff09;中&#xff0c;默认每 4 个设备像素为一组&#xff0c;渲染出普通屏幕中一个像素显示…

Eyeshot Fem 2023.3 Crack Eyeshot Ultimate

添加新的 PrintSimulationMesh 和 MultiFastMesh 实体并改进 NURBS 曲面三角测量。 2023 年 10 月 4 日 - 11:09新版本 特征 PrintSimulationMesh 实体预览。MultiFastMesh 实体预览。FEM 模态分析预览。有限元分析结果的动画。assemblySelectionType.Leaf 模式下的几何选择。编…

python修改unittestreport中的用例条数

背景: 自动化框架中使用yaml文件作为数据配置&#xff0c;使用ddt作为数据驱动来运行测试用例&#xff0c;由于测试用例都是基于场景去编写&#xff0c;目前都是一个测试类算是一条测试用例&#xff0c;但基于测试报告里面一个类运行的测试方法有多个&#xff0c;因此统计的测试…