• Spring整合Mybatis案例(XML格式)


    目录

    Spring整合Mybatis案例(XML格式)

    案例分析

    基础准备工作

    整合准备工作

    整合工作


    • Spring整合Mybatis案例(XML格式)

    • 案例分析

    • 使用spring整合mybatis技术,完成账户模块(Account)的基础增删改查功能
    • 账户模块对应字段
    • 编号:id
    • 账户名:name
    • 余额:money
    • 非spring环境
    • 1.实体类与表
    • 2.业务层接口与实现
    • 3.数据层接口
    • 4.Mybatis核心配置
    • 5.Mybatis映射配置
    • 6.客户端程序测试功能
    • spring环境
    • 1.实体类与表
    • 2.业务层接口与实现(提供数据层接口的注入操作)
    • 3.数据层接口
    • 4.Mybatis核心配置(交给spring控制,该文件省略)
    • 5.Mybatis映射配置
    • 6.客户端程序测试功能(使用spring方式获取bean)
    • 7.Spring核心配置文件
    • 8.Druid数据源的应用(可选)
    • 9.Spring整合Mybatis
    • 基础准备工作

    • 环境准备
    • 1.导入Spring坐标,Mybatis坐标,MySQL坐标,Druid坐标
      1. "1.0" encoding="UTF-8"?>
      2. <project xmlns="http://maven.apache.org/POM/4.0.0"
      3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      5. <modelVersion>4.0.0modelVersion>
      6. <groupId>org.examplegroupId>
      7. <artifactId>springdemo2artifactId>
      8. <version>1.0-SNAPSHOTversion>
      9. <properties>
      10. <maven.compiler.source>8maven.compiler.source>
      11. <maven.compiler.target>8maven.compiler.target>
      12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
      13. properties>
      14. <dependencies>
      15. <dependency>
      16. <groupId>junitgroupId>
      17. <artifactId>junitartifactId>
      18. <version>4.13.2version>
      19. <scope>testscope>
      20. dependency>
      21. <dependency>
      22. <groupId>org.springframeworkgroupId>
      23. <artifactId>spring-contextartifactId>
      24. <version>5.3.22version>
      25. dependency>
      26. <dependency>
      27. <groupId>org.springframeworkgroupId>
      28. <artifactId>spring-jdbcartifactId>
      29. <version>5.3.22version>
      30. dependency>
      31. <dependency>
      32. <groupId>com.alibabagroupId>
      33. <artifactId>druidartifactId>
      34. <version>1.2.11version>
      35. dependency>
      36. <dependency>
      37. <groupId>org.mybatisgroupId>
      38. <artifactId>mybatisartifactId>
      39. <version>3.5.10version>
      40. dependency>
      41. <dependency>
      42. <groupId>mysqlgroupId>
      43. <artifactId>mysql-connector-javaartifactId>
      44. <version>8.0.30version>
      45. dependency>
      46. <dependency>
      47. <groupId>org.mybatisgroupId>
      48. <artifactId>mybatis-springartifactId>
      49. <version>2.0.7version>
      50. dependency>
      51. dependencies>
      52. <build>
      53. <pluginManagement>
      54. <plugins>
      55. <plugin>
      56. <artifactId>maven-clean-pluginartifactId>
      57. <version>3.1.0version>
      58. plugin>
      59. <plugin>
      60. <artifactId>maven-resources-pluginartifactId>
      61. <version>3.0.2version>
      62. plugin>
      63. <plugin>
      64. <artifactId>maven-compiler-pluginartifactId>
      65. <version>3.8.0version>
      66. plugin>
      67. <plugin>
      68. <artifactId>maven-surefire-pluginartifactId>
      69. <version>2.22.1version>
      70. plugin>
      71. <plugin>
      72. <artifactId>maven-jar-pluginartifactId>
      73. <version>3.0.2version>
      74. plugin>
      75. <plugin>
      76. <artifactId>maven-install-pluginartifactId>
      77. <version>2.5.2version>
      78. plugin>
      79. <plugin>
      80. <artifactId>maven-deploy-pluginartifactId>
      81. <version>2.8.2version>
      82. plugin>
      83. <plugin>
      84. <artifactId>maven-site-pluginartifactId>
      85. <version>3.7.1version>
      86. plugin>
      87. <plugin>
      88. <artifactId>maven-project-info-reports-pluginartifactId>
      89. <version>3.0.0version>
      90. plugin>
      91. plugins>
      92. pluginManagement>
      93. build>
      94. project>
    • 业务类与接口准备
    • 2.创建数据库表,并制作相应的实体类Account
      1. CREATE TABLE account(
      2. id INT PRIMARY KEY auto_increment,
      3. name VARCHAR(20),
      4. money DOUBLE(10,2)
      5. );
      6. INSERT INTO account VALUES (NULL,'Mike',888.88),(NULL,'Jock',666.66);
      1. package com.superdemo.domain;
      2. public class Account {
      3. private Integer id;
      4. private String name;
      5. private Double money;
      6. public Integer getId() {
      7. return id;
      8. }
      9. public void setId(Integer id) {
      10. this.id = id;
      11. }
      12. public String getName() {
      13. return name;
      14. }
      15. public void setName(String name) {
      16. this.name = name;
      17. }
      18. public Double getMoney() {
      19. return money;
      20. }
      21. public void setMoney(Double money) {
      22. this.money = money;
      23. }
      24. @Override
      25. public String toString() {
      26. return "Account{" +
      27. "id=" + id +
      28. ", name='" + name + '\'' +
      29. ", money=" + money +
      30. '}';
      31. }
      32. }
    • 3.定义业务层接口与数据层接口
      1. package com.superdemo.dao;
      2. import com.superdemo.domain.Account;
      3. import java.util.List;
      4. public interface AccountDao {
      5. void save(Account account);
      6. void delete(Integer id);
      7. void update(Account account);
      8. List findAll();
      9. Account findByid(Integer id);
      10. }
      1. package com.superdemo.service;
      2. import com.superdemo.domain.Account;
      3. import java.util.List;
      4. public interface AccountService {
      5. void save(Account account);
      6. void delete(Integer id);
      7. void update(Account account);
      8. List findAll();
      9. Account findByid(Integer id);
      10. }
    • 4.在业务层调用数据层接口,并实现业务方法的调用
      1. package com.superdemo.service.impl;
      2. import com.superdemo.dao.AccountDao;
      3. import com.superdemo.domain.Account;
      4. import com.superdemo.service.AccountService;
      5. import java.util.List;
      6. public class AccountServiceImpl implements AccountService {
      7. private AccountDao accountDao;
      8. public void setAccountDao(AccountDao accountDao) {
      9. this.accountDao = accountDao;
      10. }
      11. public void save(Account account){
      12. accountDao.save(account);
      13. }
      14. public void delete(Integer id){
      15. accountDao.delete(id);
      16. }
      17. public void update(Account account){
      18. accountDao.update(account);
      19. }
      20. public List findAll(){
      21. return accountDao.findAll();
      22. }
      23. public Account findByid(Integer id){
      24. return accountDao.findByid(id);
      25. }
      26. }
    • 基础配置文件
    • 5.jdbc.properties
      1. jdbc.driver=com.mysql.cj.jdbc.Driver
      2. jdbc.url=jdbc:mysql://localhost:3306/dp1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
      3. jdbc.username=root
      4. jdbc.password=123456
    • 6.Mybatis映射配置文件
      1. "1.0" encoding="UTF-8" ?>
      2. mapper
      3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      5. <mapper namespace="com.superdemo.dao.AccountDao">
      6. <select id="findAll" resultType="account">
      7. SELECT * FROM account
      8. select>
      9. <select id="findByid" resultType="account" parameterType="int">
      10. SELECT * FROM account WHERE id = #{id}
      11. select>
      12. <insert id="save" parameterType="account">
      13. INSERT INTO account(name, money) VALUES (#{name},#{money})
      14. insert>
      15. <update id="update" parameterType="account">
      16. UPDATE account SET name = #{name},money = #{money} WHERE id = #{id}
      17. update>
      18. <delete id="delete" parameterType="int">
      19. DELETE FROM account WHERE id = ${id}
      20. delete>
      21. mapper>
    • 整合准备工作

    • 1.spring配置文件,加上context命名空间,用于加载properties文件
    • 2.开启加载properties文件
    • 3.配置数据源Druid(备用)
    • 4.定义service层bean,注入dao层bean
    • 5.dao的bean无需定义,使用代理自动生成
    • 整合工作

    • 1.导入Spring整合Mybatis坐标
    • 2.将mybatis配置成spring管理的bean(SqlSessionFactoryBean)
      • 将原始配置文件中的所有项,转入到当前配置中
        • 数据源转换
        • 映射转换
    • 3.通过spring加载mybatis的映射配置文件到spring环境中
    • 4.设置类型别名
      1. "1.0" encoding="UTF-8"?>
      2. <beans xmlns="http://www.springframework.org/schema/beans"
      3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
      4. xmlns:p="http://www.springframework.org/schema/p"
      5. xmlns:context="http://www.springframework.org/schema/context"
      6. xsi:schemaLocation="http://www.springframework.org/schema/beans
      7. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
      8. http://www.springframework.org/schema/context
      9. https://www.springframework.org/schema/context/spring-context.xsd">
      10. <context:property-placeholder location="classpath:*.properties"/>
      11. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
      12. <property name="driverClassName" value="${jdbc.driver}"/>
      13. <property name="url" value="${jdbc.url}"/>
      14. <property name="username" value="${jdbc.username}"/>
      15. <property name="password" value="${jdbc.password}"/>
      16. bean>
      17. <bean id="accountService" class="com.superdemo.service.impl.AccountServiceImpl">
      18. <property name="accountDao" ref="accountDao"/>
      19. bean>
      20. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
      21. <property name="dataSource" ref="dataSource"/>
      22. <property name="typeAliasesPackage" value="com.superdemo.domain"/>
      23. bean>
      24. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      25. <property name="basePackage" value="com.superdemo.dao"/>
      26. bean>
      27. beans>
    • 5.测试结果;使用spring环境加载业务层bean,执行操作
      1. import com.superdemo.domain.Account;
      2. import com.superdemo.service.AccountService;
      3. import org.springframework.context.ApplicationContext;
      4. import org.springframework.context.support.ClassPathXmlApplicationContext;
      5. public class App {
      6. public static void main(String[] args) {
      7. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      8. AccountService accountService = (AccountService) ctx.getBean("accountService");
      9. Account ac = accountService.findByid(2);
      10. System.out.println(ac);
      11. Account account = new Account();
      12. account.setName("Tom");
      13. account.setMoney(1314.52);
      14. accountService.save(account);
      15. ac = accountService.findByid(3);
      16. System.out.println(ac);
      17. }
      18. }
    • 小节:
    • 需要专用的spring整合mybatis的jar包
    • Mybatis核心配置文件消失
      • 环境environment转换成数据源对象
      • 映射Mapper扫描工作交由spring处理
      • 类型别名交由spring处理
    • 业务发起使用spring上下文对象获取对应的bean
  • 相关阅读:
    Day03-Python分支结构循环语句与异常处理
    九九重阳,永恒之约 | AIGC数字永生时代,揭示永生探索的全新维度!
    Linux_一款好用的查看系统信息的桌面软件_包名hardinfo、软件名system profiler and Benchmark
    一款简单漂亮的WPF UI - AduSkin
    常用数学点回顾
    yocto(六)——搭建yocto环境
    6. 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务
    nodejs+vue 校园通勤车-计算机毕业设计
    【简易 教程:Pytorch 配置 GPU版本】
    Linux查找命令
  • 原文地址:https://blog.csdn.net/weixin_59624686/article/details/126636210