springBoot(五)整合之C3P0_Junit

整合一下C3P0

spring-boot配置内本身就兼容C3P0


一、整合C3P0




模仿spring-Boot写法

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties

1.建立一个普通的bean,利用@Configuration与@ConfigurationProperties自定义一个配置类

@Configuration // 定义配置信息类
public class DataSourceConfiguration {
	/** 定义创建数据源方法 */
	@Bean(name="dataSource") // 定义Bean
	@Primary // 主要的候选者
	@ConfigurationProperties(prefix="spring.datasource.c3p0") // 配置属性
	public DataSource getDataSource(){
		return DataSourceBuilder.create() // 创建数据源构建对象
				.type(ComboPooledDataSource.class) // 设置数据源类型
				.build(); // 构建数据源对象
	}
}

2、 在application.properties配置c3p0,可以设置多个数据源

# 配置c3p0数据源
spring.datasource.c3p0.driverClass=com.mysql.jdbc.Driver
spring.datasource.c3p0.jdbcUrl=jdbc:mysql://localhost:3306/springboot_db
spring.datasource.c3p0.user=root
spring.datasource.c3p0.password=root
spring.datasource.c3p0.maxPoolSize=20
spring.datasource.c3p0.minPoolSize=5
spring.datasource.c3p0.initialPoolSize=5


# 配置mybatis需要属性
# 配置MyBatis的核心配置文件
mybatis.configLocation=classpath:mybatis-config.xml
# 配置类型别名包扫描
mybatis.typeAliasesPackage=cn.itcast.springboot.domain
# 配置SQL语句映射文件
mybatis.mapperLocations=classpath:mappers/**/*Mapper.xml




3、\src\main\resources\mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 全局的设置-->
	<settings>
		<!-- 开启缓存 -->
		<setting name="cacheEnabled" value="true"/>
		<!-- 启用延迟加载功能 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 按需要延迟加载-->
		<setting name="aggressiveLazyLoading" value="false"/>
		<!-- 开启驼峰映射 (方便自动映射) -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	
</configuration>

4、 mappers\NoticeMapper.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.itcast.springboot.mapper.NoticeMapper">
	
	<!-- 统计查询 -->
	<select id="count" resultType="long">
		select count(*) from notice
	</select>
	
	<!-- 分页查询 -->
	<select id="findByPage" resultType="Notice">
		select * from notice limit #{page},#{rows}
	</select>
</mapper>


5、\src\main\java\cn\itcast\springboot\domain\Notice---Pojo类

public class Notice implements Serializable {
	private Long id;
	private String title;
	private String content;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}

6、\src\main\java\cn\itcast\springboot\mapper---映射类
@Mapper // 加@Mapper注解,就会创建它的代理对象
public interface NoticeMapper {
	@Select("select * from notice")
	List<Notice> findAll();
	
	/** 查询总记录数 */
	Long count();
	
	/** 分页查询 */
	List<Notice> findByPage(@Param("page")int page,@Param("rows")Integer rows);
	
}

7、服务类和实现类
\src\main\java\cn\itcast\springboot\service
public interface NoticeService {

	List<Notice> findAll();
	
	/** 分页查询公告 */
	Map<String, Object> findByPage(Integer page, Integer rows);

}

实现类
@Service
@Transactional
public class NoticeServiceImpl implements NoticeService {

	/** 注入数据访问接口 */
	@Autowired
	private NoticeMapper noticeMapper;
	
	@Override
	public List<Notice> findAll() {
		return noticeMapper.findAll();
	}
	
	/** 分页查询公告 */
	public Map<String, Object> findByPage(Integer page, Integer rows){
		// {"total": 30, "rows" : [{},{}]}
		/** 查询总记录数 */
		Long total = noticeMapper.count();
		/** 分页查询 */
		List<Notice> notices = noticeMapper.findByPage((page - 1) * rows, rows);
		
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("rows", notices);
		return data;
		
	}

}



二、整合Junit


添加依赖

pom.xml
<!-- 配置测试启动器 -->
<!-- 配置依赖 -->
	<dependencies>
		<!-- 配置SpringBoot的Web启动器(内嵌了tomcat、集成了SpringMVC) -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 配置devtools实现热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		
		<!-- 配置MyBatis的启动器 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<!-- c3p0 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.2</version>
		</dependency>
		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
	</dependencies>

编写测试类

@RunWith(SpringJUnit4ClassRunner.class) // 指定运行的主类
@SpringBootTest(classes=Application.class) // 指定SpringBoot启动类,
public class NoticeTest {
   @Autowired
   private NoticeService noticeService;
   @Test
   public void findAll(){
      System.out.println(noticeService.findAll());
   }
}
@RunWith 注解运行的主类
@SpringBootTest 注解的classes 属性要指定启动类的class



Controller层

@RestController
public class NoticeController {
	
	/** 注入业务层 */
	@Autowired
	private NoticeService noticeService;
	
	/** 查询所有的公告 */
	@GetMapping("/findAll")
	public List<Notice> findAll(){
		return noticeService.findAll();
	}
	
	/** 分页查询公告 */
	@PostMapping("/findByPage") 
	public Map<String, Object> findByPage(@RequestParam(value="page", defaultValue="1") Integer page, 
			@RequestParam(value="rows", defaultValue="15") Integer rows){
		// {"total": 30, "rows" : [{},{}]}
		return noticeService.findByPage(page, rows);
	}
	
}


如果需要看测试类的有关详情,我后面会专门写一个帖子吧,



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值