程序员开发实例大全宝库

网站首页 > 编程文章 正文

想知道Boot应用MyBatis如何实现DAO吗?快收藏吧

zazugpt 2024-08-21 04:11:40 编程文章 16 ℃ 0 评论

一、SpringBoot环境的搭建

  1. 创建maven project

  2. 在pom.xml中添加boot框架jar包定义

  3. 在src/main/resources下创建application.propertie文件

  4. 在cn.xdl.springboot包下创建入口类

二、SpringBoot默认数据源的连接池

  1. 在pom.xml中添加spring-boot-starter-jdbc和ojdbc包定义

  2. 在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);

}

}

}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表