spring整合mybatis案例(IOC)
标签搜索
侧边栏壁纸
  • 累计撰写 21 篇文章
  • 累计收到 390 条评论

spring整合mybatis案例(IOC)

limei
2023-08-21 / 31 评论 / 35 阅读 / 正在检测是否收录...

spring整合mybatis案例

案例目录结构

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</groupId>
    <artifactId>java-01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
    </dependencies>
</project>
package com.limei.domain;

import java.io.Serializable;

public class Account implements Serializable {

    private Integer id;
    private String name;
    private Double money;

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

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

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

2.编写业务层接口与实现类

package com.limei.service;

import com.limei.domain.Account;

import java.util.List;

public interface AccountService {

    void save(Account account);

    void delete(Integer id);

    void update(Account account);

    List<Account> findAll();

    Account findById(Integer id);

}
package com.limei.service.impl;

import com.limei.dao.AccountDao;
import com.limei.domain.Account;
import com.limei.service.AccountService;

import java.util.List;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void save(Account account) {
        accountDao.save(account);
    }

    public void update(Account account){
        accountDao.update(account);
    }

    public void delete(Integer id) {
        accountDao.delete(id);
    }

    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    public List<Account> findAll() {
        return accountDao.findAll();
    }
}

3.编写持久层接口与对应的xml文件

package com.limei.dao;

import com.limei.domain.Account;

import java.util.List;

public interface AccountDao {

    void save(Account account);

    void delete(Integer id);

    void update(Account account);

    List<Account> findAll();

    Account findById(Integer id);
}
<?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.limei.dao.AccountDao">

    <!--配置根据id查询-->
    <select id="findById" resultType="account" parameterType="int">
        select * from account where id = #{id}
    </select>

    <!--配置查询所有-->
    <select id="findAll" resultType="account">
        select * from account
    </select>

    <!--配置保存-->
    <insert id="save" parameterType="account">
        insert into account(name,money)values(#{name},#{money})
    </insert>

    <!--配置删除-->
    <delete id="delete" parameterType="int">
        delete from account where id = #{id}
    </delete>

    <!--配置更新-->
    <update id="update" parameterType="account">
        update account set name=#{name},money=#{money} where id=#{id}
    </update>
</mapper>

4.建立applicationContext.xml配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载perperties配置文件的信息,spring的加载方式-->
    <context:property-placeholder location="classpath:*.properties"/>

    <!--加载druid资源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!--配置service作为spring的bean,注入dao-->
    <bean id="accountService" class="com.limei.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--spring整合mybatis后控制的创建连接用的对象-->
    <bean  class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.limei.domain"/>
    </bean>

    <!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.limei.dao"/>
    </bean>
</beans>

5.jdbc.properties资源

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/lianxi
jdbc.username=root
jdbc.password=123456

6.Test类

import com.limei.domain.Account;
import com.limei.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml");
        AccountService accountService = (AccountService) ctx.getBean("accountService");
        Account ac = accountService.findById(3);
        System.out.println(ac);

//        Account account = new Account();
//        account.setName("Tom");
//        account.setMoney(123456.78);
//
//        accountService.save(account);

    }
}

0

评论 (31)

取消
  1. 头像
    zconfevuse
    Windows 10 · Google Chrome

    《神武天尊》国产动漫高清在线免费观看:https://www.jgz518.com/xingkong/11801.html

    回复
  2. 头像
    gpjvatskqc
    Windows 10 · Google Chrome

    《酒店实习生第一季》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/87189.html

    回复
  3. 头像
    lelqnmxpkm
    Windows 10 · Google Chrome

    《神探二又二分之一》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/48601.html

    回复
  4. 头像
    vhlwihsree
    Windows 10 · Google Chrome

    看到你的文章,我仿佛感受到了生活中的美好。 https://www.yonboz.com/video/29701.html

    回复
  5. 头像
    szcpggsehu
    Windows 10 · Google Chrome

    你的文章总是能给我带来欢乐,谢谢你! https://www.yonboz.com/video/60669.html

    回复
  6. 头像
    xnazxotmfx
    Windows 10 · Google Chrome

    看到你的文章,我仿佛感受到了生活中的美好。 https://www.yonboz.com/video/29701.html

    回复
  7. 头像
    tdmurkhnib
    Windows 10 · Google Chrome

    你的文章内容非常卖力,让人点赞。 https://www.4006400989.com/qyvideo/95652.html

    回复
  8. 头像
    lgnrxcivhu
    Windows 10 · Google Chrome

    《我要结婚粤语》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/50656.html

    回复
  9. 头像
    ptvlhkvtiv
    Windows 10 · Google Chrome

    《大叔的哈雷日记》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/69036.html

    回复
  10. 头像
    dzaondoaeq
    Windows 10 · Google Chrome

    你的才华让人惊叹,你是我的榜样。 https://www.yonboz.com/video/43690.html

    回复
  11. 头像
    ahnneqigak
    Windows 10 · Google Chrome

    《帝国2024》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/127851.html

    回复
  12. 头像
    pkykwvynna
    Windows 10 · Google Chrome

    看到你的文章,我仿佛感受到了生活中的美好。 https://www.4006400989.com/qyvideo/79429.html

    回复
  13. 头像
    aiwxjjvwww
    Windows 10 · Google Chrome

    你的文章让我感受到了正能量,非常棒! http://www.55baobei.com/xl18hRPioz.html

    回复
  14. 头像
    merxvmeqvj
    Windows 10 · Google Chrome

    《我要结婚粤语》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/50656.html

    回复
  15. 头像
    qvjhmxlzyk
    Windows 10 · Google Chrome

    《大叔的哈雷日记》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/69036.html

    回复
  16. 头像
    cgtfdpazka
    Windows 10 · Google Chrome

    你的文章让我感受到了快乐,每天都要来看一看。 http://www.55baobei.com/6l458h2PAw.html

    回复
  17. 头像
    kccrlcumas
    Windows 10 · Google Chrome

    《帝国2024》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/127851.html

    回复
  18. 头像
    ltjhhgssjj
    Windows 10 · Google Chrome

    你的才华横溢,让人敬佩。 https://www.4006400989.com/qyvideo/88235.html

    回复
  19. 头像
    sgulxjhsfe
    Windows 10 · Google Chrome

    你的文章让我感受到了无尽的欢乐,谢谢分享。 http://www.55baobei.com/GutrO2PSYG.html

    回复
  20. 头像
    ggicipvwjy
    Windows 10 · Google Chrome

    《狂迷制造者:拉斯冯提尔》记录片高清在线免费观看:https://www.jgz518.com/xingkong/29268.html

    回复
  21. 头像
    satgedbzzl
    Windows 10 · Google Chrome

    你的文章让我感受到了无尽的欢乐,谢谢分享。 http://www.55baobei.com/GutrO2PSYG.html

    回复
  22. 头像
    akxxxqefjd
    Windows 10 · Google Chrome

    《抓住彩虹》泰国剧高清在线免费观看:https://www.jgz518.com/xingkong/154250.html

    回复
  23. 头像
    ztxsuczcns
    Windows 10 · Google Chrome

    《Santa Mi Amor》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/26995.html

    回复
  24. 头像
    smgpijjfag
    Windows 10 · Google Chrome

    逆境中的反思充满生命韧性。

    回复
  25. 头像
    jnflpzrzsh
    Windows 10 · Google Chrome

    ?学术类评语?

    回复
  26. 头像
    wpkfliirnb
    Windows 10 · Google Chrome

    文章结构紧凑,层次分明,逻辑严密,让人一读即懂。

    回复
  27. 头像
    pixyzbbnat
    Windows 10 · Google Chrome

    每一个段落都紧密相连,逻辑清晰,展现了作者高超的写作技巧。

    回复
  28. 头像
    esbceiqebm
    Windows 10 · Google Chrome

    案例丰富,数据详实,论证扎实可信。

    回复
  29. 头像
    bslphkssih
    Windows 10 · Google Chrome

    独特的构思和新颖的观点,让这篇文章在众多作品中脱颖而出。

    回复
  30. 头像
    vzexcrulul
    Windows 10 · Google Chrome

    案例丰富且贴合主题,论证逻辑环环相扣。

    回复
  31. 头像
    gdxhjuoddp
    Windows 10 · Google Chrome

    这篇文章不错!

    回复