SpringBoot整合Mybatis案例
2019/7/15以实习生身份入职公司前端做Angular ,但是感觉前途迷茫,于是乎学习一下Java的框架——SpringBooot。
参照大神博客:https://blog.csdn.net/iku5200/article/details/82856621#commentBox
新建一个SpringBoot项目
创建一个新项目。
创建项目的文件结构以及jdk的版本
选择所需要的依赖
修改项目名,完成项目创建
建好后的pom.mxl文件
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-jdbc</artifactId>
</dependency>
<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>
<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>
修改配置文件
本文不使用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
两个文件的意思是:
创建包
创建包controller、entity、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。
数据库表结构
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;
编写代码
编写实体类 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>
最终框架结构
完成以上,下面在启动类里加上注解用于给出需要扫描的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