SpringBoot 集成 Mybatis 框架
一、1、SpringBoot 集成 Mybatis 的基本步骤
第一步:添加依赖;
第二步:配置数据源;
第三步:扫描接口包。
二、详细的集成步骤如下:
1.第一步:添加依赖;
添加依赖;除了常规依赖外,需要加入 Mybatis
代码如下(示例):
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
<modelVersion> 4.0.0 </modelVersion>
<groupId> com.ysd.springboot </groupId>
<artifactId> spring-boot-mybatis </artifactId>
<packaging> jar </packaging>
<version> 0.0.1-SNAPSHOT </version>
<name> spring-boot-mybatis </name>
<url> http://maven.apache.org </url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.5.1.RELEASE </version>
</parent>
<properties>
<!-- 项目设置:编码格式 UTF-8 及 springboot 相关版本 -->
<project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
<project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding>
<java.version> 1.8 </java.version>
<mybatis-spring-boot> 1.2.0 </mybatis-spring-boot>
<mysql-connector> 5.1.39 </mysql-connector>
<druid> 1.0.18 </druid>
</properties>
<dependencies>
<!-- Spring Boot SpringMVC 依赖 -->
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId> org.mybatis.spring.boot </groupId>
<artifactId> mybatis-spring-boot-starter </artifactId>
<version> ${mybatis-spring-boot} </version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId> mysql </groupId>
<artifactId> mysql-connector-java </artifactId>
<version> ${mysql-connector} </version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- SpringBoot 插件 -->
<plugin>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
<!-- SpringBoot 项目打包名称 -->
<finalName> springmybatis </finalName>
</build>
</project>
2.第二步:配置数据源;
在 application.properties 配置文件中,配置数据源、Mybatis 的配置及映射文件。
代码如下(示例):
## 数据源配置
spring . datasource . url = jdbc : mysql : //localhost:3306/库名
spring . datasource . username = root
spring . datasource . password = root
spring . datasource . driver - class - name = com . mysql . jdbc . Driver
## Mybatis 配置
# 实体所在包,起别名
mybatis . typeAliasesPackage = org . spring . springboot . domain
# 映射文件所在路径
mybatis . mapperLocations = classpath : mapper /*.xml
3.第三步:扫描接口包。
在主模块上注解扫描接口包,使用@MapperScan([包名])。
代码如下(示例):
@SpringBootApplication // Spring Boot 应用的标识
@MapperScan ( "org.spring.springboot.dao" ) // mapper 接口类扫描包配置
//如果要显示 Sql 细节还需要在 logback 配置<logger name="接口类所在包" level="debug" />
public class Application {
public static void main ( String [] args ) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication . run ( Application . class , args );
}
}
实验:
参考的数据库:
/*创建数据库 springbootdb*/
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `springbootdb` /*!40100 DEFAULT CHARACTER SET utf8
*/ ;
USE `springbootdb` ;
/*创建表 city*/
DROP TABLE IF EXISTS `city` ;
CREATE TABLE `city` (
`id` int ( 10 ) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号' , `provinceId` int ( 10 ) unsigned DEFAULT NULL COMMENT '省份编号' , `cityName` varchar ( 20 ) DEFAULT NULL COMMENT '城市名称' , `description` text COMMENT '城市描述' , PRIMARY KEY ( `id` )
) ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8 ;
/*插入测试数据 */
insert into `city` ( `id` , `provinceId` , `cityName` , `description` ) values ( 1 , 1 , '北京' , '这是北京市的描述信
息,北京这家伙是中国首都,百年帝都,政治经济文化中心,也将是世界的中心.' ),( 2 , 2 , '郑州' , '这是郑
州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀,河南政治经济文化中心,也是中国的
中心城市.' ),( 3 , 3 , 'ZhengZhou' , '这是郑州市的描述信息,郑州这家伙是河南省会,城市中的后起之秀, 河南政治经济文化中心,也是中国的中心城市.' );
运行结果:
打开工具sts,新建一个项目、具体如下。
点击下一步
点击finish结束。
回看第一步.
1、添加依赖
查看pom.xml中是否有
第二步,配置数据源
在 application.properties 配置文件中,配置数据源、Mybatis 的配置及映射文件。
在src/main/resources目录下新建包Mapper,在官网搜mybatis入门复制如下代码。
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "org.mybatis.example.BlogMapper" >
<select id = "selectBlog" resultType = "Blog" >
select * from Blog where id = #{id}
</select>
</mapper>
上述项目中建entity、dao、controller.代码如下。
entity
package com . zha . entity ;
public class City {
private Integer id ; //城市编号
private Integer provinceId ; //省份编号
private String cityName ; //城市名称
private String description ; //城市描述
public Integer getId () {
return id ;
}
public void setId ( Integer id ) {
this . id = id ;
}
public Integer getProvinceId () {
return provinceId ;
}
public void setProvinceId ( Integer provinceId ) {
this . provinceId = provinceId ;
}
public String getCityName () {
return cityName ;
}
public void setCityName ( String cityName ) {
this . cityName = cityName ;
}
public String getDescription () {
return description ;
}
public void setDescription ( String description ) {
this . description = description ;
}
@Override
public String toString () {
return "City [id=" + id + ", provinceId=" + provinceId + ", cityName=" + cityName + ", description="
+ description + "]" ;
}
public City ( Integer id , Integer provinceId , String cityName , String description ) {
super ();
this . id = id ;
this . provinceId = provinceId ;
this . cityName = cityName ;
this . description = description ;
}
public City () {
super ();
}
}
dao
package com . zha . dao ;
import java . util . List ;
import com . zha . entity . City ;
public interface CityDao {
public List < City > getAll ();
}
controller
package com . zha . controller ;
import org . springframework . beans . factory . annotation . Autowired ;
import org . springframework . web . bind . annotation . RequestMapping ;
import org . springframework . web . bind . annotation . RestController ;
import com . zha . dao . CityDao ;
@RestController
public class helloController {
@Autowired
CityDao cityDao ;
@RequestMapping ( "/hello" )
public String hello () {
return "" + cityDao . getAll ();
}
}
第三步
在主模块上注解扫描接口包,使用@MapperScan([包名])。
然后在src/main/resources目录下新建包Mapper,在官网搜mybatis入门复制如下代码,换成自己的。
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "com.zha.dao.CityDao" >
<select id = "getAll" resultType = "com.zha.entity.City" >
SELECT
city.id,
city.provinceId,
city.cityName,
city.description
FROM
city
</select>
</mapper>
运行项目,查看。
看到这里就说明已经成功了,在网页输入http://localhost:8080/hello,按回车键。
到此这篇关于使用sts工具、SpringBoot整合mybatis的详细步骤的文章就介绍到这了,更多相关sts SpringBoot整合mybatis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/dfyhd/article/details/115366841
查看更多关于使用sts工具、SpringBoot整合mybatis的详细步骤的详细内容...