如何用Spring整合MyBatis和Junit
创始人
2024-05-19 19:21:00
0

Spring整合MyBatis和Junit

  • 一. 整合MyBatis
    • 1. 目录:
    • 2. pom.xml:
    • 3. domain层:
    • 4. dao层:
    • 5. service层:
      • AccountService接口类:
      • AccountServiceImpl实现类:
    • 6. jdbc.properties配置文件:
    • 7. 配置类:
      • JdbcConfig配置类:
      • SpringConfig配置类:
      • MyBastisonfig配置类 ★★★:
    • 8. main运行:
  • 二. 整合Junit
    • 1. pom.xml:
    • 2. AccountServiceTest测试类:

一. 整合MyBatis

Spring整合MyBatis,本质是将MyBastis的核心配置文件换成Spring来整合;

  1. 转化了sqlSessionFactory的配置‘
  2. 原来的typeAliases换成set方法;
  3. 将dataSources变成set方法,将dataSources的bean注入;这里的dataSource用的Druid德鲁伊;
  4. 原来读取映射文件的mapper变成了MapperScannerConfigurer的bean对象,并set扫描包的名称;

总结:总共需要配置两个bean:SqlSessionFactoryBeanMapperScannerConfigurer,(外加dataSource

1. 目录:

在这里插入图片描述

2. pom.xml:

在这里插入图片描述

3. domain层:

即pojo实体类,对应数据库结构;

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;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}

4. dao层:

AccountDao:
即mapper代理接口类,对应mapper映射文件,此处使用注解的方式写sql;

public interface AccountDao {@Insert("insert into tbl_account(name,money)values(#{name},#{money})")void save(Account account);@Delete("delete from tbl_account where id = #{id} ")void delete(Integer id);@Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")void update(Account account);@Select("select * from tbl_account")List findAll();@Select("select * from tbl_account where id = #{id} ")Account findById(Integer id);
}

5. service层:

AccountService接口类:

public interface AccountService {void save(Account account);void delete(Integer id);void update(Account account);List findAll();Account findById(Integer id);}

AccountServiceImpl实现类:

使用@Service注解来定义bean

@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate 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 findAll() {return accountDao.findAll();}
}

6. jdbc.properties配置文件:

用来填充 dataSource 数据源信息
在这里插入图片描述

7. 配置类:

JdbcConfig配置类:

即定义dataSource 这个 bean
只要和数据库有关都需要!
dataSource的值从 jdbc.properties配置文件中获取;

public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}

SpringConfig配置类:

@import 两个外部配置类:JDBC(datasource)和MyBatis;
在这里插入图片描述

MyBastisonfig配置类 ★★★:

在这里插入图片描述

  1. SqlSessionFactoryBean
    第一个bean用来获取SqlSessionFactory;
    事务处理由JDBC提供了, 这里使用默认的就可以;
    setTypeAliasesPackage是用来扫描类型别名的包;
    需要注入dataSource的bean,也就是JdbcConfig定义的bean,这里从方法参数注入

  2. MapperScannerConfigurer
    是用来扫描mapper映射文件,所以这里一个单独的bean,不是在SqlSessionFactoryBean中;

原mapper
在这里插入图片描述

原dataSource
在这里插入图片描述

8. main运行:

使用配置类创造IOC容器;
只需获取service的bean再操作即可,dao的bean已经注入到service了(通过定义bean的方法参数注入);
在这里插入图片描述

结果:打印出查询信息

在这里插入图片描述

二. 整合Junit

在上面基础上进行整合;

1. pom.xml:

增加Junit包和Spring-test整合包:
在这里插入图片描述

2. AccountServiceTest测试类:

一般都是测试业务层,测数据层的少;

  • 新建测试业务层接口的测试类:
    在这里插入图片描述

  • @RunWith 设定类运行器:
    SpringJunitClassRunner.class是专用类运行器;

  • 需要让类知道Spring的配置环境:
    @ContextConfiguration(classes=Spring配置文件.class)

  • 将要被测试的(被注入的bean)建立成员变量
    设置@Autowired 自动装配,让IOC容器中的bean自动注入;

  • 测试方法用@Test注释;
    在这里插入图片描述
    结果:打印查询信息
    在这里插入图片描述

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...