栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring整合MyBatis和Junit(十一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring整合MyBatis和Junit(十一)

文章目录

文章目录
  • 文章目录
  • 一、整合MyBatis
    • 1.将SqlSessionFactory相关的配置转化为:
    • 2.映射配置转化为:
    • 3.案例
  • 二、整合Junit
    • 1.导入依赖
    • 2.使用Spring整合Junit专用类加载器


一、整合MyBatis

    
        
            
            
                
                
                
                
            
        
    
     
    
        
    

1.将SqlSessionFactory相关的配置转化为:
 @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setTypeAliasesPackage("com.jkj.pojo");
        factory.setDataSource(dataSource);
        return factory;
    }
2.映射配置转化为:
 @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.jkj.dao");
        return msc;
    }
3.案例

首先创建一个数据库Spring设计一个表,表名为j_account
1.j_account

2.导入依赖

 
        
            org.springframework
            spring-context
            5.2.10.RELEASE
        
        
            com.alibaba
            druid
            1.1.16
        
        
            org.mybatis
            mybatis
            3.5.6
        
        
            org.springframework
            spring-jdbc
            5.2.10.RELEASE
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            mysql
            mysql-connector-java
            5.1.47
        
    

3.Account

package com.jkj.pojo;

public class Account {
    private Integer id;
    private String name;
    private Double money;

    public Account() {
    }

    public Account(Integer id, String name, Double money) {
        this.id = id;
        this.name = name;
        this.money = 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 +
                '}';
    }
}

4.AccountDao

package com.jkj.dao;

import com.jkj.pojo.Account;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AccountDao {
    //查询全部
    @Select("select * from j_account")
    List findAll();
    //通过id查询
    @Select("select * from j_account where id=#{id}")
    Account FindById(Integer id);
    //增加数据
    @Insert("insert into j_account (id,name,money) values(id#{id},#{name},#{money})")
    void save(Account account);
    //删除数据
    @Delete("delete from j_account where id=#{id}")
    void delete(Integer id);
    //修改数据
    @Update("update j_account set name=#{name},money=#{money} where id=#{id}")
    Account update(Integer id);
}

5.AccountService

package com.jkj.service;
import com.jkj.pojo.Account;
import java.util.List;
public interface AccountService {
    //查询全部
    public List findAll();
    //通过id查询

    public Account FindById(Integer id);
    //增加数据

    public void save(Account account);
    //删除数据

    public void delete(Integer id);
    //修改数据

    public Account update(Integer id);

}

6.AccountServiceImpl

package com.jkj.service.impl;

import com.jkj.dao.AccountDao;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
   private AccountDao accountDao;

    public List findAll() {
        return accountDao.findAll();
    }

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

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

    }

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

    }

    public Account update(Integer id) {
        return accountDao.update(id);

    }
}

7.JdbcConfig

package com.jkj.config;

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

import javax.sql.DataSource;

public class JdbcConfig {
    @Value("com.mysql.jdbc.Driver")
    private String driver;
    @Value("jdbc:mysql://localhost:3306/spring")
    private String url;
    @Value("root")
    private String username;
    @Value("root")
    private String password;

    //2.添加@Bean,表示当前方法的返回值是一个bean
    @Bean
    //1.定义一个方法获得要管理的对象
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}

8.MyBatisConfig

package com.jkj.config;
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 sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setTypeAliasesPackage("com.jkj.pojo");
        factory.setDataSource(dataSource);
        return factory;
    }
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.jkj.dao");
        return msc;
    }
}

9.SpringConfig

package com.jkj.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("com.jkj")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
public class SpringConfig {

}

10.Test测试类

import com.jkj.config.SpringConfig;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService bean = context.getBean(AccountService.class,true);
        //查询全部
        List all = bean.findAll();
        for (Account account : all) {
            System.out.println(account);
            

        }

        //根据id查询
       


       //删除数据
       

    }
}

二、整合Junit 1.导入依赖
 
            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            5.2.10.RELEASE
        
2.使用Spring整合Junit专用类加载器
package com.jkj;

import com.jkj.config.SpringConfig;
import com.jkj.pojo.Account;
import com.jkj.service.AccountService;
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(classes = SpringConfig.class)
public class ServiceTest {
    @Autowired
    private AccountService as;
    @Test
    public void test(){
        Account byId = as.FindById(1);
        System.out.println(byId);  //Account{id=1, name='tom', money=2345.0}

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993279.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号