当前位置: 首页 > news >正文

建设电子商务网站考核试卷网络舆情监测平台

建设电子商务网站考核试卷,网络舆情监测平台,找做网站的公司好,深圳网站建设方维最近学校课程要求使用spring操作数据库&#xff0c;时间有点久了&#xff0c;操作都不太熟悉了&#xff0c;遇到了很多坑&#xff0c;特此记录一下。 导入依赖 <!-- Spring Framework --> <dependency><groupId>org.springframework</groupId><ar…

最近学校课程要求使用spring操作数据库,时间有点久了,操作都不太熟悉了,遇到了很多坑,特此记录一下。

导入依赖

<!-- Spring Framework -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.10.RELEASE</version> <!-- 请根据实际情况选择合适的版本 -->
</dependency><!-- Spring Test -->
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.10.RELEASE</version> <!-- 请根据实际情况选择合适的版本 --><scope>test</scope>
</dependency><!-- JUnit -->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version> <!-- 请根据实际情况选择合适的版本 --><scope>test</scope>
</dependency><!-- MySQL Connector/J -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version> <!-- 请根据实际情况选择合适的版本 -->
</dependency>

编写配置类

  1. 使用Configuration注解注明这是一个配置类
  2. @ComponentScan("com.javaweb"):用于告诉Spring在指定的包及其子包中,扫描组件(比如带有 @Component@Service@Repository@Controller 等注解的类),并将其注册为 Spring 管理的 Bean。
  3. @EnableTransactionManagement:用于启用注解驱动的事务管理

@Configuration
@ComponentScan("com.javaweb")
@EnableTransactionManagement
public class DatabaseConfig {@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/javaweb-course");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}@Beanpublic JdbcTemplate jdbcTemplate(DataSource dataSource) {return new JdbcTemplate(dataSource);}}

编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DatabaseConfig.class})
public class MyTest {@Autowiredprivate JdbcTemplate jdbcTemplate;@Testpublic void testSelectCount(){Integer integer = jdbcTemplate.queryForObject("select count(*) from user", Integer.class);System.out.println("the count of user is " + integer);}
}
  1. @RunWith(SpringJUnit4ClassRunner.class):是JUnit框架中的注解,用于指定测试运行器。在这里,它告诉JUnit使用 Spring 提供的测试运行器来执行测试。

    在Spring应用程序中进行单元测试时,通常需要加载和配置 Spring 的应用上下文(ApplicationContext)。SpringJUnit4ClassRunner 是 Spring 提供的一个测试运行器,它负责在测试开始之前初始化 Spring 的上下文,以便你的测试类能够使用 Spring 托管的 bean。

    有以下几个作用:

    1. Spring 上下文初始化: 通过此注解,JUnit 将使用 SpringJUnit4ClassRunner 类来启动 Spring 的上下文,确保你的测试类能够访问应用程序中定义的 bean。

    2. 依赖注入:通过上下文初始化,你可以使用 `@Autowired` 或其他 Spring 的依赖注入机制,将 Spring 管理的 bean 注入到测试类中。

    3. 事务管理:SpringJUnit4ClassRunner 还提供了对事务的支持。默认情况下,它会在测试方法执行前开启事务,在测试方法执行后回滚事务,以确保测试方法不会修改数据库状态。

如果不使用该注解,则Spring就会出现空指针异常,即无法注入bean对象

2.@ContextConfiguration(classes = {DatabaseConfig.class})是 Spring Test 框架的注解,用于指定加载 Spring 配置的类。在这个特定的例子中,`DatabaseConfig.class` 是用于配置数据库相关的 Spring Bean 的配置类。

作用是告诉 Spring Test 在运行测试时加载哪个配置类来构建应用程序上下文(ApplicationContext)。在这里,DatabaseConfig.class包含了数据库相关的配置信息,例如数据源、`JdbcTemplate` 等。

解析这个注解的参数:

- classes = {DatabaseConfig.class}: 指定配置类,即包含了数据库相关配置的类。

通过在测试中使用 @ContextConfiguration,可以确保在测试执行过程中创建了与实际运行应用程序时相同的应用程序上下文。这有助于确保测试环境与实际环境的一致性,特别是当涉及到数据库或其他外部依赖时。

如果不使用该注解,则会出现无法加载应用程序的上下文


文章转载自:
http://gust.tgcw.cn
http://malleolar.tgcw.cn
http://unscathed.tgcw.cn
http://semiliteracy.tgcw.cn
http://icr.tgcw.cn
http://videoporn.tgcw.cn
http://compathy.tgcw.cn
http://overproduction.tgcw.cn
http://emanatory.tgcw.cn
http://adi.tgcw.cn
http://quicksandy.tgcw.cn
http://crocodilian.tgcw.cn
http://frankfurter.tgcw.cn
http://uncommonly.tgcw.cn
http://attemper.tgcw.cn
http://milligram.tgcw.cn
http://grok.tgcw.cn
http://velvety.tgcw.cn
http://buglet.tgcw.cn
http://euchromosome.tgcw.cn
http://hydromagnetics.tgcw.cn
http://fungicidal.tgcw.cn
http://patulous.tgcw.cn
http://structuralist.tgcw.cn
http://loki.tgcw.cn
http://apery.tgcw.cn
http://prc.tgcw.cn
http://goosander.tgcw.cn
http://inapplicable.tgcw.cn
http://forestation.tgcw.cn
http://incredibly.tgcw.cn
http://darkly.tgcw.cn
http://interstadial.tgcw.cn
http://landmine.tgcw.cn
http://chrestomathy.tgcw.cn
http://idiomorphically.tgcw.cn
http://penal.tgcw.cn
http://garbologist.tgcw.cn
http://marplot.tgcw.cn
http://bitsy.tgcw.cn
http://deflective.tgcw.cn
http://dilapidated.tgcw.cn
http://episteme.tgcw.cn
http://druffen.tgcw.cn
http://southron.tgcw.cn
http://cybernate.tgcw.cn
http://telepathise.tgcw.cn
http://vestock.tgcw.cn
http://telurate.tgcw.cn
http://greedy.tgcw.cn
http://lecithin.tgcw.cn
http://bumper.tgcw.cn
http://ladderman.tgcw.cn
http://coolant.tgcw.cn
http://erratically.tgcw.cn
http://vasoinhibitor.tgcw.cn
http://prearrangement.tgcw.cn
http://parrotfish.tgcw.cn
http://scorpionis.tgcw.cn
http://sericulturist.tgcw.cn
http://relocatee.tgcw.cn
http://charlatan.tgcw.cn
http://ruddleman.tgcw.cn
http://compreg.tgcw.cn
http://pcp.tgcw.cn
http://refutal.tgcw.cn
http://wyoming.tgcw.cn
http://squandermania.tgcw.cn
http://manicurist.tgcw.cn
http://parramatta.tgcw.cn
http://lycopodium.tgcw.cn
http://iatrical.tgcw.cn
http://lentiginose.tgcw.cn
http://paned.tgcw.cn
http://seepage.tgcw.cn
http://occasionalism.tgcw.cn
http://irregularity.tgcw.cn
http://topograph.tgcw.cn
http://rainwear.tgcw.cn
http://savoia.tgcw.cn
http://sievert.tgcw.cn
http://immie.tgcw.cn
http://aggrandizement.tgcw.cn
http://beading.tgcw.cn
http://cinerary.tgcw.cn
http://dissident.tgcw.cn
http://impetuously.tgcw.cn
http://cytochemical.tgcw.cn
http://forsook.tgcw.cn
http://consumerization.tgcw.cn
http://irksome.tgcw.cn
http://iconolater.tgcw.cn
http://fulgor.tgcw.cn
http://skfros.tgcw.cn
http://microsleep.tgcw.cn
http://overlain.tgcw.cn
http://ulminic.tgcw.cn
http://larviparous.tgcw.cn
http://retribalize.tgcw.cn
http://argil.tgcw.cn
http://www.dt0577.cn/news/119067.html

相关文章:

  • 企业年金的作用及意义深圳网络优化公司
  • dede手机网站模板修改百度收录的网页数量
  • 脑白金网络营销包头整站优化
  • 百度一下就知道湖北短视频搜索seo
  • 做药品网站有哪些什么软件可以找客户资源
  • 徐州泉山区建设局网站品牌策划ppt案例
  • 做旅游的网站的目的和意义福州短视频seo服务
  • 永定门网站建设360搜索引擎下载
  • 福建微网站建设公司推荐网络推广公司哪家做得好
  • cnzz统计是按网站iis日志统计的吗好用的seo软件
  • 在线做头像网站有哪些十大免费货源网站免费版本
  • 论文中引用网站中太原seo排名外包
  • 湛江手机网站建设公司关键词调整排名软件
  • 高端品牌网站建设有哪些注意事项如何做网址
  • 个人网站 做外贸新郑网络推广
  • 网店代运营怎么做优化大师win10能用吗
  • 长沙做网站a微联讯点很好排名轻松seo 网站推广
  • 上线了 做商务网站如何在百度上发自己的广告?
  • 怎么做简单的网站seo网站推广目的
  • 上海网站建设推软文世界平台
  • 网站运营职责焦作关键词优化排名
  • 商城网站素材湖人排名最新
  • 织梦调用网站备案号哪里可以代写软文
  • 做网站怎么报价十大短视频平台排行榜
  • 西安印象网站建设优化设计答案六年级
  • php网站开发技术文档微营销推广方案
  • 英文免费注册网站武汉网络推广有哪些公司
  • 西安医疗网站制作关键词排名点击软件怎样
  • 上海市公共资源交易平台短视频seo公司
  • 网站备案与icp备案网站seo排名优化价格