一、SpringBoot环境的搭建
创建maven project
在pom.xml中添加boot框架jar包定义
在src/main/resources下创建application.propertie文件
在cn.xdl.springboot包下创建入口类
二、SpringBoot默认数据源的连接池
在pom.xml中添加spring-boot-starter-jdbc和ojdbc包定义
在application.properties定义连接参数
Java
三、添加Mybatis框架元素
1. 在pom.xml中添加mybatis-spring-boot-starter包依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
2. 定义实体类
public class Evaluation implements Serializable{
private Integer id;
private Integer user_id;
private Integer product_id;
private Integer order_id;
//省略setter和getter方法
}
Java
3. 定义Mapper接口和SQL
SQL语句(支持XML定义或注解定义)
@Mapper
public interface EvaluationMapper {
@Select("select * from xdl_product_evaluation")
public List<Evaluation> findAll();
}
4. 在主入口类BootMainApplication.java中添加@MapperScanner扫描Mapper接口
@SpringBootApplication
@MapperScan(basePackages={"cn.xdl.springboot.dao"})//mybatis.mapper-locations=classpath:cn/xdl/sql/*.xml
public class BootMainApplication {
}
Java
四、测试Mapper对象
@RunWith(SpringRunner.class)
@SpringBootTest(classes={BootMainApplication.class})
public class TestEvaluationMapper {
@Resource
private EvaluationMapper evalMapper;
@Test
public void test1(){
List<Evaluation> list = evalMapper.findAll();
for(Evaluation eval:list){
System.out.println(eval);
}
}
}
本文暂时没有评论,来添加一个吧(●'◡'●)