Springboot+Mybatis整合基本配置
准备工作:
使用mysql创建一个数据库,这里命名数据库为“oa”,以及各个字段的数据类型
Table of Contents
第二步:配置applcation.properties文件,这里我习惯用于applaction.yml文件配置项目
第三步:根据配置文件下在resource文件下分别创建mybatis-cofing.xml和mapper文件
第四步:在java文件下分别创建controller、mapper、entity、service文件夹
第一步:使用idea创建springboot项目
创建项目名为springboot-mybatis
选择模板web 、mysql、mybatis
点击下一步就创建好项目了,实现我已经创建好了以及完成了相关操作,一下是项目总体结构和prom文件
prom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yongjie</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:配置applcation.properties文件,这里我习惯用于applaction.yml文件配置项目
总共需要两步
1、配置数据库mysql
2、配置mybatis
第三步:根据配置文件下在resource文件下分别创建mybatis-cofing.xml和mapper文件
mybatis-cofing.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
mapper文件下创建user.xml,并写一个根据前端传送来的id查询数据
<?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.yongjie.springbootmybatis.mapper.UserMapper">
<select id="findById" parameterType="String" resultType="com.yongjie.springbootmybatis.entity.User">
select * from user where id = #{value }
</select>
</mapper>
第四步:在java文件下分别创建controller、mapper、entity、service文件夹
在mapper创建user.xml接口Usermapepr.java
注意:findById方法名一定要和User.xml文件查询select的id名一致
package com.yongjie.springbootmybatis.mapper;
import com.yongjie.springbootmybatis.entity.User;
public interface UserMapper {
public User findById(String id);
}
在service文件夹下创建Userservice.java 引入UserMapper接口
package com.yongjie.springbootmybatis.service;
import com.yongjie.springbootmybatis.entity.User;
import com.yongjie.springbootmybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServicee {
@Autowired
private UserMapper userMapper;
public User findByIn(String id){
return (User) userMapper.findById(id);
}
}
在controller文件创建User启动类UserController.java ,查询的结果使用json输出
package com.yongjie.springbootmybatis.controller;
import com.yongjie.springbootmybatis.entity.User;
import com.yongjie.springbootmybatis.service.UserServicee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@Autowired
private UserServicee userServicee;
@ResponseBody
@RequestMapping("/user/{id}")
private User findById(@PathVariable("id")String id){
return userServicee.findByIn(id);
}
}
最关键的一步:
在SpringbootMybatisApplication项目启动类里添加注解@MapperScan,扫描java代码mapper文件下的映射接口。MapperScan注解会在SpringBoot启动的时候扫描mapper包,并更具xml自动生成对应的实现类
上一篇: 第五章 数组
下一篇: 用strlen判断是否为空是不是比较好