SpringBoot整合MyBatis学习
一、SpringBoot概述与项目创建
1.1SpringBoot简介
借用百度百科的话,Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置,所有的配置都在application.yml一个配置文件中完成。SpringBoot可完美结合Spring、MyBatis、SpringMVC等框架,学习使用SpringBoot前最好有一定的Spring基础和maven基础的,也就是需要使用了解它们怎么配置,有什么功能。
使用IDEA创建项目(个人喜好习惯,本文采用IDEA讲述),在https://start.spring.io网站中或者IDEA中选择Spring Initializr中选择创建。
1.2使用IDEA创建SpringBoot项目
1.2.1在网站中创建
在网站中创建,参考林静生寒写的这篇博客:https://www.cnblogs.com/ljsh/p/10928106.html
1.2.2 在IDEA中创建
a. 新建项目,File>> open >> New Project,选择Spring Initializr

b. 选择自己本地安装的JDK版本等信息(一般以前有开发项目的这里除项目组名、项目名外都不用更改),填入项目组名、项目名。
c. 选择如下图所示的项目所需的包(Web >> Spring Web;SQL >> JDBC API,MyBatis Framework,MySQL Driver)。如果未整合MyBatis,只是单纯的SpringBoot项目,选择了MyBatis Framework包后,需要在pom.xml文件中对该包注释掉,否则会有警告。

创建项目,等待配置加载完成,即显示类似Maven项目目录结构。
二、问题与解决
2.1 未整合MyBatis却导入了MyBatis的Jar包,出现No MyBatis mapper was found in[]警告
开始没有整合MyBatis,但pom文件中配置了mybatis的jar包,此时需要先注释掉,否则会出现警告。
![]()
2.2 整合了MyBatis后,启动项目时出现No MyBatis mapper was found in[]警告或错误。
整合了MyBatis后,需要在程序入口文件application中添加@MapperScan("com.example.Dao")语句,否则也会出现上述警告,或者可在每个Mapper文件中添加@Mapper达到类似效果。

2.3数据库配置中,出现错误
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
解决方法:
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的,
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 版本6以后的
根据配置版本更改成对应的名称即可。
三、 SrpingBoot文件结构
3.1总体结构
3.1.1未整合MyBatis前

3.1.2整合了MyBatis后

3.2 Controller
创建完项目后,没有controller文件夹和文件,需要自行创建。
@RestController: a convenience annotation that does nothing more than adding the@Controller and@ResponseBody annotations。
@RestController是@Controller和@ResponseBody的结合体,两个标注合并起来的作用。
如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RestController
public class TestController {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/sb_test")
public List<Map<String, Object>> getDbType(){
String sql = "select * from springboot_test";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
Set<Map.Entry<String, Object>> entries = map.entrySet( );
for (Map.Entry<String, Object> entry : entries) {
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + ":" + value);
}
}
return list;
}
}
3.3 数据库

四、SpringBoot配置文件
4.1 Application.yml
创建项目时,会自动创建Application.properties文件,只需要将其重命名Application.yml即可。然后在该文件中进行MyBatis等框架和数据库连接的配置。SpringBoot会自动将application-xxx.yml文件自动解析成Application.properties。
application-dev.yml文件
server:
port: 8080
spring:
datasource:
username: root
password: 942653 //<换成自己的数据库名、用户名、密码>
url: jdbc:mysql://localhost:3306/springboottest?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
如果需要将配置文件更清晰的分开,配置多套环境,可根据需求创建application.yml和application-xxx.yml该类型文件。其中xxx则根据环境类型填写,配置方法如下:
在Spring Boot中多环境配置文件名需要满足application-{profilename}.yml的格式,其中{profilename}对应你的环境标识,比如:
application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于加载哪个环境的配置文件,则需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profilename}值。
application.yml文件
spring:
profiles:
active: dev
以上所有则是一个简单的SpringBoot项目的基础配置,创建数据库后,运行完成的项目,在浏览器中输入配置的url,显示出数据库内容,那么已经完成了一个简单的SpringBoot项目。


五、整合MyBatis
5.1 Entity
与MVC模式开发项目相同,放入实体类java文件,持久化数据。
package com.example.entity;
public class User {
private Integer id;
private String userName;
private String passWord;
private String realName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", realName='" + realName + '\'' +
'}';
}
}
5.2 Controller
package com.example.controller;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/testBoot")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/idname")
public String GetUser(){
return userService.Sel(1).toString();
}
}
5.3 Dao
所在包与下方第六点中@MapperScan所对应
package com.example.Dao;
import com.example.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao {
User Sel(int id);
}
5.4 Service
package com.example.service;
import com.example.entity.User;
import com.example.Dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDao userDao;
public User Sel(int id){
return userDao.Sel(id);
}
}
5.5 SpringBootApplication
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.Dao")
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
5.6 Mapper
<?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.example.Dao.UserDao">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="passWord" jdbcType="VARCHAR" property="passWord" />
<result column="realName" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="Sel" resultType="com.example.entity.User">
select * from user where id = #{id}
</select>
</mapper>
运行结果

本文详细介绍了如何从零开始创建SpringBoot项目,并整合MyBatis框架。通过解决整合过程中可能出现的问题,如导入MyBatis警告、Mapper扫描等,到配置数据库和文件结构,最后实现成功运行。适合有一定Spring基础的开发者学习。

427

被折叠的 条评论
为什么被折叠?



