在SpringBoot项目中如何实现使用Mybatis中的@Annotation注解-成都快上网建站

在SpringBoot项目中如何实现使用Mybatis中的@Annotation注解

在Spring Boot项目中如何实现使用 Mybatis中的@ Annotation注解?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联公司是一家集网站建设,扶沟企业网站建设,扶沟品牌网站建设,网站定制,扶沟网站建设报价,网络营销,网络优化,扶沟网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

一、运行 springboot-mybatis-annotation 工程

然后Application 应用启动类的 main 函数,然后在浏览器访问:

http://localhost:8080/api/city?cityName=温岭市

可以看到返回的 JSON 结果:

{
"id": 1,
"provinceId": 1,
"cityName": "温岭市",
"description": "我的家在温岭。"
}

三、springboot-mybatis-annotation 工程配置详解

1.pom 添加 Mybatis 依赖

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0
 springboot
 springboot-mybatis-annotation
 0.0.1-SNAPSHOT
 jar
 springboot-mybatis-annotation
 Springboot-mybatis :: 整合Mybatis Annotation Demo
 
 
 org.springframework.boot
 spring-boot-starter-parent
 1.5.1.RELEASE
 
 
 1.2.0
 5.1.39
 
 
 
 
 org.springframework.boot
 spring-boot-starter-web
 
 
 
 org.springframework.boot
 spring-boot-starter-test
 test
 
 
 
 org.mybatis.spring.boot
 mybatis-spring-boot-starter
 ${mybatis-spring-boot}
 
 
 
 mysql
 mysql-connector-java
 ${mysql-connector}
 
 
 
 junit
 junit
 4.12
 
 

2.在 CityDao 城市数据操作层接口类添加注解 @Mapper、@Select 和 @Results

/**
* 城市 DAO 接口类
*
* Created by xchunzhao on 02/05/2017.
*/
@Mapper // 标志为 Mybatis 的 Mapper
public interface CityDao {
/**
* 根据城市名称,查询城市信息
*
* @param cityName 城市名
*/
@Select("SELECT * FROM city")
// 返回 Map 结果集
@Results({
@Result(property = "id", column = "id"),
@Result(property = "provinceId", column = "province_id"),
@Result(property = "cityName", column = "city_name"),
@Result(property = "description", column = "description"),
})
City findByName(@Param("cityName") String cityName);
}

@Mapper 标志接口为 MyBatis Mapper 接口

@Select 是 Select 操作语句

@Results 标志结果集,以及与库表字段的映射关系

其他的注解可以看 org.apache.ibatis.annotations 包提供的,如图:

在Spring Boot项目中如何实现使用 Mybatis中的@ Annotation注解

看完上述内容,你们掌握在Spring Boot项目中如何实现使用 Mybatis中的@ Annotation注解的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前名称:在SpringBoot项目中如何实现使用Mybatis中的@Annotation注解
链接URL:http://kswjz.com/article/igicsd.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流