IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> spring整合mybatis思路分析(2) -> 正文阅读

[Java知识库]spring整合mybatis思路分析(2)

1.创建配置文件

配置文件:jdbc.proerties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/nevpb
jdbc.we=root
jdbc.password=1111111

2.创建spring容器管理:

config类(管理配置文件,连接数据库对象spring类)

@Value("${jdbc.driver}")
public String driver;
相当于 在一个bean中增加了一个属性:
<property name="jdbc.driver" value="${jdbc.driver}"></property>
然后把值给到下面定义的变量driver

@Bean  //创建了bean对象:对象名称是dataSource,之后可以导入管理spring中然后使用

    public DataSource dataSource(){

    DruidDataSource ds=new DruidDataSource();
    ds.setDriverClassName(driver);
    ds.setUrl(url);
    ds.setUsername(we);
    ds.setPassword(password);
    return ds;
}

package config;

import com.alibaba.druid.pool.DruidDataSource;
import impl.CER;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class jdbcConfig {

@Value("${jdbc.driver}")
public String driver;

@Value("${jdbc.url}")
public String url;
@Value("${jdbc.we}")
public String we;
@Value("${jdbc.password}")
public String password;

@Bean

    public DataSource dataSource(){

    DruidDataSource ds=new DruidDataSource();
    ds.setDriverClassName(driver);
    ds.setUrl(url);
    ds.setUsername(we);
    ds.setPassword(password);
    return ds;
}
  

 
}

MybatisConfig类(管理manper代理,mybaits)spring类

sqlSessionFactoryBean对象:传入是连接池对象:封装好的
创建这个才能使用mybatis的里面的编辑sql语句。

  @Select("select * from account where id=2")
    Account findBtId(Integer id);

mapperScannerConfigurer对象:创建mapper代理
用来管理

Account ac= accoutService.findByld(2);

package config;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;


public class MybatisConfig {



@Bean

    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){


SqlSessionFactoryBean ssfb=new SqlSessionFactoryBean();

ssfb.setDataSource(dataSource);
return ssfb;

}


@Bean
        public MapperScannerConfigurer mapperScannerConfigurer(){

    MapperScannerConfigurer msc=new MapperScannerConfigurer();
    msc.setBasePackage("dao");

    return msc;


}





}

springconfig类(spring容器类)把所有bean放到这里

package config;


import impl.AccoutService;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Configuration
@ComponentScan("dao")
@ComponentScan("impl")
@PropertySource({"classpath:jdbc.properties"})//这里有问题
@Import({jdbcConfig.class,MybatisConfig.class})


public class springconfig {




}

余下的代码

3.数据接口(sql语句)

package dao;

import impl.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

public interface AccountDao {


    
    @Insert("insert into accout(name,money)values(#{name},#{money})")
    void save(Account account);


    @Select("select * from account where id=2")
    Account findBtId(Integer id);


}

Account类:

package impl;

public class Account {


   public   Integer id;
    public String name;
    public 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 +
                '}';
    }
}

AccountServiceImpl类

package impl;

import dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("AccountService")
public class AccountServiceImpl implements AccoutService {


    @Autowired
    public AccountDao accountDao;

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

    public Account findByld(Integer id){


        return accountDao.findBtId(id);
    }


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

public void we(){
    System.out.println("AccountServiceimpl这个生效");
}





}

AccoutService接口

package impl;

public interface AccoutService {



    public void save(Account account);

    public Account findByld(Integer id);


}

执行代码(测试)

import dao.AccountDao;
import impl.Account;
import impl.AccoutService;
import impl.CER;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import config.springconfig;

import javax.sql.DataSource;


public class app2 {




    public static void main(String[] args) {



        ApplicationContext ctx=new AnnotationConfigApplicationContext(springconfig.class);
        //1.注解扫描



 AccoutService accoutService= (AccoutService) ctx.getBean("AccountService");

//Account ac=accoutService.findByld(2);
//        System.out.println(ac);


   //     CER cer= (CER) ctx.getBean("wer");
      //  System.out.println(cer.drive);

      Account ac= accoutService.findByld(2);
       System.out.println(ac);

    }

    

}

结果:

结果:成功查询到
在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-10-22 21:01:38  更:2022-10-22 21:05:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/10 18:26:44-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码