@Data
public class Article {
private int id;
private String title;
private String content;
}
编写dao层逻辑,创建接口
public interface ArticleMapper {
Article getArticleById(int id);
}
编写映射文件,写条件查询SQL
select * from t_article where id=#{id}
编写service层逻辑
public interface ArticleService {
Article getArticleById(int id);
}
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleMapper articleMapper;
@Override
public Article getArticleById(int id) {
return articleMapper.getArticleById(id);
}
}
在测试方法中,进行查询测试
@SpringBootTest
public class ExamdemoApplicationTests {
@Autowired
private ArticleService articleService;
@Test
public void getMessage(){
Article result = articleService.getArticleById(1);
System.out.println(result);
}
}
得到结果
增加AOP切面逻辑,引入启动器
org.springframework.bootspring-boot-starter-aop
定义切面,并编写前置通知方法,指向接入点。
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(* com.hcxy.service.*.*(..))")
public void test(){
}
@Before("test()")
public void beforeAdvice(){
System.out.println("开始查询数据库");
}
}