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

秦皇岛 免费建网站河北省建设银行网站

秦皇岛 免费建网站,河北省建设银行网站,网站租用价格,注册一个公司一年费用一、整合JUnit 1、Spring 整合 JUnit 核心注解有两个#xff1a; RunWith(SpringJUnit4ClassRunner.class) 是设置Spring专用于测试的类运行器#xff08;Spring程序执行程序有自己的一套独立的运行程序的方式#xff0c;不能使用JUnit提供的类运行方式#xff09;Conte…一、整合JUnit 1、Spring 整合 JUnit 核心注解有两个 RunWith(SpringJUnit4ClassRunner.class) 是设置Spring专用于测试的类运行器Spring程序执行程序有自己的一套独立的运行程序的方式不能使用JUnit提供的类运行方式ContextConfiguration(classes SpringConfig.class) 是用来设置Spring核心配置文件或配置类的就是加载Spring的环境所需具体的环境配置 //加载spring整合junit专用的类运行器 RunWith(SpringJUnit4ClassRunner.class) //指定对应的配置信息 ContextConfiguration(classes SpringConfig.class) public class DemoServiceTestCase {//注入你要测试的对象Autowiredprivate DemoService demoService;Testpublic void testGetById(){//执行要测试的对象对应的方法System.out.println(accountService.findById(2));} }2、SpringBoot 整合 JUnit SpringBoot直接简化了 RunWith(SpringJUnit4ClassRunner.class) 和 ContextConfiguration(classes SpringConfig.class) 这两个几乎固定的注解。 package com.ty;import com.ty.service.DemoService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;SpringBootTest class SpringbootDemoApplicationTests {Autowiredprivate DemoService demoService;Testpublic void getByIdTest(){demoService.getById();} } 注意 当然如果测试类 SpringbootDemoApplicationTests 所在的包目录与 SpringBoot启动类 SpringbootDemoApplication 不相同则启动时JUnit会找不到SpringBoot的启动类。报错 java.lang.IllegalStateException: Unable to find a SpringBootConfiguration, you need to use ContextConfiguration or SpringBootTest(classes...) with your test。 解决方法 将测试类 SpringbootDemoApplicationTests 所在的包目录与 SpringBoot启动类 SpringbootDemoApplication 调整一致或通过 SpringBootTest(classes SpringbootDemoApplication.class) 指定SpringBoot启动类。 二、整合MyBatis 1、Spring 整合 MyBatis 首选引入MyBatis的一系列 Jar dependenciesdependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.16/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion3.5.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.47/version/dependency!--1.导入mybatis与spring整合的jar包--dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion1.3.0/version/dependency!--导入spring操作数据库必选的包--dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion5.2.10.RELEASE/version/dependency /dependencies数据库连接信息配置 jdbc.drivercom.mysql.jdbc.Driver jdbc.urljdbc:mysql://localhost:3306/spring_db?useSSLfalse jdbc.usernameroot jdbc.passwordroot定义mybatis专用的配置类 //定义mybatis专用的配置类 Configuration public class MyBatisConfig { // 定义创建SqlSessionFactory对应的beanBeanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){//SqlSessionFactoryBean是由mybatis-spring包提供的专用于整合用的对象SqlSessionFactoryBean sfb new SqlSessionFactoryBean();//设置数据源替代原始配置中的environments的配置sfb.setDataSource(dataSource);//设置类型别名替代原始配置中的typeAliases的配置sfb.setTypeAliasesPackage(com.itheima.domain);return sfb;} // 定义加载所有的映射配置Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc new MapperScannerConfigurer();msc.setBasePackage(com.itheima.dao);return msc;}}Spring核心配置 Configuration ComponentScan(com.itheima) PropertySource(jdbc.properties) public class SpringConfig { }配置Bean Configuration 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 dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;} }2、SpringBoot 整合 MyBatis 对比以上SpringBoot简单很多。 首先导入MyBatis对应的starter mybatis-spring-boot-starter 和 数据库驱动 mysql-connector-java dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.27/versionscoperuntime/scope/dependency配置数据源相关信息 spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/tyusername: rootpassword: 123驱动类过时提醒更换为com.mysql.cj.jdbc.Driver 配置Entity和Dao数据库SQL映射需要添加Mapper被容器识别到 package com.ty.entity;import lombok.Data;Data public class TyUser {private Integer id;private String name;private Integer age;} package com.ty.dao;import com.ty.entity.TyUser;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;Mapperpublic interface DemoDao {Select(select * from ty_user where id #{id})public TyUser getById(Integer id);}通过测试类注入 DemoService 即可调用。 package com.ty;import com.ty.dao.DemoDao; import com.ty.entity.TyUser; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;SpringBootTest class SpringbootDemoApplicationTests {Autowiredprivate DemoDao demoDao;Testpublic void getByIdTestDao(){TyUser byId demoDao.getById(1);System.out.println(byId);}}三、整合MyBatis-Plus MyBaitsPlus简称MP国人开发的技术符合中国人开发习惯 导入 mybatis_plus starter mybatis-plus-boot-starter dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.3/version /dependency配置数据源相关信息 spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/tyusername: rootpassword: 123Dao 映射接口与实体类 package com.example.springboot_mybatisplus_demo.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.springboot_mybatisplus_demo.entity.User; import org.apache.ibatis.annotations.Mapper;Mapper public interface DemoDao extends BaseMapperUser { } 实体类名称与表名一致可自动映射。当表名有前缀时可在application.yml中配置表的通用前缀。 package com.example.springboot_mybatisplus_demo.entity;import lombok.Data;Data public class User {private Integer id;private String name;private Integer age; } mybatis-plus:global-config:db-config:table-prefix: ty_ #设置所有表的通用前缀名称为tbl_编写测试类注入DemoDao 即可调用 mybatis_plus 提供的一系列方法。继承的BaseMapper的接口中帮助开发者预定了若干个常用的API接口简化了通用API接口的开发工作。 package com.example.springboot_mybatisplus_demo;import com.example.springboot_mybatisplus_demo.dao.DemoDao; import com.example.springboot_mybatisplus_demo.entity.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;SpringBootTest class SpringbootMybatisplusDemoApplicationTests {Autowiredprivate DemoDao demoDao;Testpublic void getByIdTestDao(){User byId demoDao.selectById(1);System.out.println(byId);}}
http://www.yutouwan.com/news/433518/

相关文章:

  • vs2017html5网站开发WordPress图片关闭永久链接
  • 有没有什么做统计的网站全国建设厅网站
  • 宁夏网站建设一条龙郑州做网站华久科技
  • 查互做蛋白的网站wordpress好用
  • 开发小型门户网站的方法 步骤国内网站
  • wordpress安装ssl后网站404网站顶部导航
  • 网站平台建设实训总结郑州专业网站制作服务费用
  • 免费建网站无广告婚恋网站上认识人 带你做原油交易
  • 义乌做网站哪家好wordpress中国区官方论坛
  • 网站网页翻页设计微信网站主题
  • 网站开发可以学吗怎样建设VR网站
  • wordpress搭建多个购物网站国外网站建设现状
  • 怎么将网站做成小程序石家庄新华区网站建设
  • 公司 网站建设 会计科目个人网页制作模板三张
  • 连锁销售网站制作支付网站建设要求
  • 网站建设与管理清考作业南京制作网页学校
  • 租房网站建设多少钱软件商店下载官网
  • diango做的网站怎么用房地产开发公司网站建设方案模板
  • 常见的网站名称有哪些个人网站的搭建
  • 深圳注明企业网站设计深圳市公司网站建设公司
  • 网站开发 简单重庆哪家网站
  • 做网站没有公网做购彩网站是怎么盈利的
  • 购物网站html模板免费制作网页
  • 品牌网站建设流程一个网站两个域名
  • 网站开发培训机构哪个好北京电商公司有哪些
  • 中国建设银行网站网上业务服务范围天华建筑设计公司官网
  • 网站信息化建设总结用家用电脑建设网站
  • 外贸网站建设哪家实惠鞍山便民信息平台
  • 汽车网站建设可行性分析开发外包公司
  • 浙江建设集团网站首页网络推广方案文案