SpringBoot整合Mybatis案例

 2019/7/15以实习生身份入职公司前端做Angular ,但是感觉前途迷茫,于是乎学习一下Java的框架——SpringBooot。


IDE:idea、DB:mysql


 新建一个SpringBoot项目

创建一个新项目。

SpringBoot整合Mybatis案例_xml

创建项目的文件结构以及jdk的版本 

SpringBoot整合Mybatis案例_xml_02

选择所需要的依赖

 SpringBoot整合Mybatis案例_xml_03

修改项目名,完成项目创建

 SpringBoot整合Mybatis案例_java_04

建好后的pom.mxl文件

1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.1.7.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.example</groupId>
12 <artifactId>demo</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>demo</name>
15 <description>Demo project for Spring Boot</description>
16
17 <properties>
18 <java.version>1.8</java.version>
19 </properties>
20
21 <dependencies>
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter-jdbc</artifactId>
25 </dependency>
26 <dependency>
27 <groupId>org.springframework.boot</groupId>
28 <artifactId>spring-boot-starter-web</artifactId>
29 </dependency>
30 <dependency>
31 <groupId>org.mybatis.spring.boot</groupId>
32 <artifactId>mybatis-spring-boot-starter</artifactId>
33 <version>2.1.0</version>
34 </dependency>
35
36 <dependency>
37 <groupId>mysql</groupId>
38 <artifactId>mysql-connector-java</artifactId>
39 <scope>runtime</scope>
40 </dependency>
41 <dependency>
42 <groupId>org.springframework.boot</groupId>
43 <artifactId>spring-boot-starter-test</artifactId>
44 <scope>test</scope>
45 </dependency>
46 </dependencies>
47
48 <build>
49 <plugins>
50 <plugin>
51 <groupId>org.springframework.boot</groupId>
52 <artifactId>spring-boot-maven-plugin</artifactId>
53 </plugin>
54 </plugins>
55 </build>
56
57 </project>

 修改配置文件

 本文不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容。

application.yml

spring:
profiles:
active: dev

application-dev.yml

server:
port: 8080

spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver


mybatis:
mapper-locations: classpath:mapper/*Mapping.xml
type-aliases-package: com.example.entity

logging:
level:
com:
example:
mapper:debug

两个文件的意思是:


在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。


创建包

创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。

   SpringBoot整合Mybatis案例_java_05

数据库表结构

CREATE TABLE `user` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`userName` varchar(32) NOT NULL,
`passWord` varchar(50) NOT NULL,
`realName` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

  SpringBoot整合Mybatis案例_java_06

编写代码

编写实体类 User.java

package entity;

/**
* @Auther wjw
* @Date 2019-08-20
*/


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 + '\'' +
'}';
}
}

 编写控制类 UserController.java

package com.example.demo.controller;


import com.example.demo.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;

/**
* @ Author wjw
* @ Date 2019-08-20
*/

@RestController
@RequestMapping("/testBoot")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("getUser/{id}")
public String GetUser(@PathVariable int id) {
return userService.Sel(id).toString();
}

}

 编写服务类 UserService.java

package com.example.demo.service;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @ Author wjw
* @ Date 2019-08-20
*/

@Service
public class UserService {

@Autowired
UserMapper userMapper;

public User Sel(int id) {
return userMapper.Sel(id);
}

}

 编写 UserMapper.java.java

package com.example.demo.mapper;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Select;

/**
* @ Author wjw
* @ Date 2019-08-20
*/

@Repository
public interface UserMapper {
User Sel(int id);
}

 编写 UserMapping.xml 文件

<?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.demo.mapper.UserMapper">

<resultMap id="BaseResultMap" type="com.example.demo.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.demo.entity.User">
select * from user where id = #{id}
</select>

</mapper>

最终框架结构

 SpringBoot整合Mybatis案例_spring_07

完成以上,下面在启动类里加上注解用于给出需要扫描的mapper文件路径@MapperScan("com.example.demo.mapper") 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;


@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

运行 

运行,最后启动,浏览器输入地址看看吧:​​http://localhost:8080/testBoot/getUser/1​

 SpringBoot整合Mybatis案例_xml_08

SpringBoot整合Mybatis案例_spring_09

 

【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!

【重要说明】本文为本人的学习记录,论点和观点仅代表个人而不代表当时技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!

【Gitee地址】秦浩铖:​​https://gitee.com/wjw1014​