SpringBoot整合MyBatis(04)

这部分内容要求读者有一定的MyBatis基础知识。

1)准备运行环境:

第一步:准备数据库(MySQL数据库)。

#客户表
CREATE TABLE `cst_customer` (
  `cust_id` int(11) NOT NULL AUTO_INCREMENT,
  `cust_address` varchar(255) DEFAULT NULL,
  `cust_name` varchar(255) DEFAULT NULL,
  `cust_phone` varchar(255) DEFAULT NULL
  PRIMARY KEY (`cust_id`)
);

INSERT INTO `cst_customer` VALUES ('1', '南宁市', 'AA', '0771-66668888');
INSERT INTO `cst_customer` VALUES ('2', '南宁市', 'BB', '0771-66668888');
INSERT INTO `cst_customer` VALUES ('3', '广州市', 'CC', '020-65085588');

第二步:创建一个Maven项目,然后加入相关依赖。

<!-- 配置MyBatis启动器 -->
<dependency>
  	<groupId>org.mybatis.spring.boot</groupId>
  	<artifactId>mybatis-spring-boot-starter</artifactId>
  	<version>1.3.0</version>
</dependency>
<!-- 配置mysql驱动 -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 配置c3p0连接池 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

第三步:配置数据源。

在application.properties文件中配置数据源信息。

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/entor
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.mchange.v2.c3p0.ComboPooledDataSource

第四步:创建实体类,该实体类映射到cst_customer表。

public class Customer implements Serializable {
	private Integer custId;
	private String name;
	private String phone;
	private String address;
	
	//省略setters和getters方法...
}

2)实现查询功能

第一步:编写映射接口,并提供查询方法,该方法使用注解方式绑定查询的SQL命令。

public interface CustomerMapper {

	/** 查询所有客户 */
	@Select("select * from cst_customer")
	@Results({  
       @Result(property="custId",column="cust_id"),
       @Result(property="name",column="cust_name"),
       @Result(property="phone",column="cust_phone"),
       @Result(property="address",column="cust_address"),
    }) 
	public List<Customer> findAll();
}

第二步:创建业务接口和实现类。

//接口
public interface ICustomerService {
	
	public List<Customer> findCustomers();
	
}

//实现类
@Service
public class CustomerServiceImpl implements ICustomerService {
	@Autowired
	private CustomerMapper custMapper;

	@Override
	public List<Customer> findCustomers() {
		return custMapper.findAll();
	}

}

第三步:创建SpringBoot启动类。

@SpringBootApplication(scanBasePackages={"com.xxx"})
@MapperScan(basePackages={"com.xxx.mapper"})
public class Application {

	public static void main(String[] args) {
		SpringApplication springApplication 
			= new SpringApplication(Application.class);
		springApplication.run(args);
	}
	
}

@MapperScan注解用于扫描com.xxx.mapper下所有的映射接口文件。


使用Junit做单元测试:

第一步:引入JUnit依赖。

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version> 4.12</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
</dependency>

第二步:创建测试类。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class CustomerServiceTest {
	@Autowired
	private ICustomerService custService;

	@Test
	public void testFindCustomers() {
		List<Customer> customers = custService.findCustomers();
		for (Customer customer : customers) {
			System.out.println(customer);
		}
	}
	
}

@RunWith 代表运行的主类。

@SpringBootTest 注解的classes 属性要指定启动类的class。

第三步:鼠标右键,选择Run as -> Junit Test即可。

猜你喜欢

转载自blog.csdn.net/zhongliwen1981/article/details/82263167