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





	
	
		
		
		
		
		
		
		
		
	
	

4、 mappers\NoticeMapper.xml




	
	
	
	
	
	


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 findAll();
	
	/** 查询总记录数 */
	Long count();
	
	/** 分页查询 */
	List findByPage(@Param("page")int page,@Param("rows")Integer rows);
	
}

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

	List findAll();
	
	/** 分页查询公告 */
	Map findByPage(Integer page, Integer rows);

}

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

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

}



二、整合Junit


添加依赖

pom.xml


	
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-devtools
		
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.0
		
		
		
			com.mchange
			c3p0
			0.9.5.2
		
		
		
			mysql
			mysql-connector-java
		
	

编写测试类

@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 findAll(){
		return noticeService.findAll();
	}
	
	/** 分页查询公告 */
	@PostMapping("/findByPage") 
	public Map findByPage(@RequestParam(value="page", defaultValue="1") Integer page, 
			@RequestParam(value="rows", defaultValue="15") Integer rows){
		// {"total": 30, "rows" : [{},{}]}
		return noticeService.findByPage(page, rows);
	}
	
}


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



你可能感兴趣的:(springBoot)