日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring整合Mybatis和JUnit

發布時間:2025/3/15 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring整合Mybatis和JUnit 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring整合Mybatis:

注解整合MyBatis分析

  • 業務類使用注解形式聲明bean,屬性采用注解注入
  • 建立獨立的配置管理類,分類管理外部資源,根據功能進行分類,并提供對應的方法獲取bean
  • 使用注解形式啟動bean掃描,加載所有注解配置的資源(bean)
  • 使用AnnotationConfigApplicationContext對象加載所有的啟動配置類,內部使用導入方式進行關聯

注解整合MyBatis步驟:

1.修改mybatis外部配置文件格式為注解格式
2.業務類使用@Component聲明bean,使用@Autowired注入對象
3.建立配置文件JDBCConfig與MyBatisConfig類,并將其導入到核心配置類SpringConfig
4.開啟注解掃描
5.使用AnnotationConfigApplicationContext對象加載配置項

pom文件

dao.AccountDao

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

domain.Account

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 +'}';} }

service.AccountService

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

service.impl.AccountServiceImpl

@Service("accountService") public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Overridepublic void save(Account account) {accountDao.save(account);}@Overridepublic void update(Account account) {accountDao.update(account);}@Overridepublic void delete(Integer id) {accountDao.delete(id);}@Overridepublic Account findById(Integer id) {return accountDao.findById(id);}@Overridepublic List<Account> findAll() {return accountDao.findAll();} }

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/db2 jdbc.username=root jdbc.password=itzhuzhu

config.JDBCConfig

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;@Bean("dataSource")public DataSource getDataSource(){System.out.println(driver);DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;} }

config.MyBatisConfig

public class MyBatisConfig {@Beanpublic SqlSessionFactoryBean getSqlSessionFactoryBean(@Autowired DataSource dataSource) {SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();ssfb.setTypeAliasesPackage("com.itzhuzhu.domain");ssfb.setDataSource(dataSource);return ssfb;}@Beanpublic MapperScannerConfigurer getMapperScannerConfigurer() {MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.itzhuzhu.dao");return msc;} }

config.SpringConfig

@Configuration @ComponentScan("com.itzhuzhu") @PropertySource("classpath:jdbc.properties") @Import({JDBCConfig.class, MyBatisConfig.class}) public class SpringConfig { }

測試:

@Testpublic void Test2() {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService as = (AccountService) ctx.getBean("accountService");System.out.println(as.findById(1));}

Spring整合JUnit:

  • Spring接管Junit的運行權,使用Spring專用的Junit類加載
  • 為Junit測試用例設定對應的spring容器
    • 從Spring5.0以后,要求Junit的版本必須是4.12及以上
    • Junit僅用于單元測試,不能將Junit的測試類配置成spring的bean,否則該配置將會被打包進入工程中

    導入Spring整合Junit坐標

    <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.9.RELEASE</version> </dependency>

    Spring整合Junit測試用例注解格式

    // 配置Spring專用的類加載器 @RunWith(SpringJUnit4ClassRunner.class) // 加載Spring配置類 @ContextConfiguration(classes = SpringConfig.class) public class ServiceTest {@Autowiredprivate AccountService accountService;@Testpublic void testFindById() {Account ac = accountService.findById(2); // System.out.println(ac);Assert.assertEquals("Jock1", ac.getName());}@Testpublic void testFindAll() {List<Account> list = accountService.findAll();Assert.assertEquals(3, list.size());} }

    總結

    以上是生活随笔為你收集整理的Spring整合Mybatis和JUnit的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。