快速搭建SpringCloud+mybatis项目
开发工具使用IntelliJ IDEA
JDK版本1.8
maven版本apache-maven-3.6.3
数据库使用MariaDB 10.5(用mysql也一样,注意下版本)
mybatis-spring-boot-starter 2.1.0
pom.xml添加相关依赖
工程采用pom打包方式
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.dbgroupId>
<artifactId>springboot-mybatisartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.5.RELEASEversion>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<mysql-connector-java.version>5.1.41mysql-connector-java.version>
<mybatis-spring-boot-starter.version>2.1.0mybatis-spring-boot-starter.version>
<mapper-spring-boot-starter.version>2.1.5mapper-spring-boot-starter.version>
<pagehelper-spring-boot-starter.version>1.2.12pagehelper-spring-boot-starter.version>
<fastjson-version>1.2.66fastjson-version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR3version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql-connector-java.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-spring-boot-starter.version}version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>${mapper-spring-boot-starter.version}version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>${pagehelper-spring-boot-starter.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson-version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<finalName>${project.artifactId}finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
plugins>
build>
project>
pom.xml引入springboot、mysql、mybatis等依赖
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-mybatisartifactId>
<groupId>com.dbgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>mybatis-integrateartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
dependency>
dependencies>
project>
#配置tomcat服务器端口等信息
server:
port: 8090
tomcat:
uri-encoding: utf-8
#配置应用项目信息
spring:
application:
name: mybatis-test
datasource: # 数据源的相关配置
type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCP
driver-class-name: com.mysql.jdbc.Driver # mysql驱动
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: admin
hikari:
connection-timeout: 30000 # 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 默认:30秒
minimum-idle: 5 # 最小连接数
maximum-pool-size: 20 # 最大连接数
auto-commit: true # 自动提交
idle-timeout: 600000 # 连接超时的最大时长(毫秒),超时则被释放(retired),默认:10分钟
pool-name: DateSourceHikariCP # 连接池名字
max-lifetime: 1800000 # 连接的生命时长(毫秒),超时而且没被使用则被释放(retired),默认:30分钟 1800000ms
connection-test-query: SELECT 1
mybatis需要的pojo实体类、Mapper接口、mapper.xml sql映射文件可通过tk-mybatis-generator逆向工具生成后复制过来,具体操作可见
tk-mybatis-generator逆向生成工具使用
package com.db.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
package com.db.pojo;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
@Table(name = "app_user")
public class AppUser {
@Id
private Integer id;
private String userid;
private String username;
@Column(name = "created_time")
private Date createdTime;
/**
* @return id
*/
public Integer getId() {
return id;
}
/**
* @param id
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return userid
*/
public String getUserid() {
return userid;
}
/**
* @param userid
*/
public void setUserid(String userid) {
this.userid = userid;
}
/**
* @return username
*/
public String getUsername() {
return username;
}
/**
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return created_time
*/
public Date getCreatedTime() {
return createdTime;
}
/**
* @param createdTime
*/
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
}
package com.db.user.mapper;
import com.db.mapper.MyMapper;
import com.db.pojo.AppUser;
import org.springframework.stereotype.Repository;
@Repository
public interface AppUserMapper extends MyMapper<AppUser> {
}
新建springboot项目启动类Application.java
package com.db;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.db.user.mapper")
//@ComponentScan(basePackages = {"com.db"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MapperScan配置的扫包路径不要弄错。
ComponentScan根据自己启动类所有的目录确定是否需要加。由于我的启动类就是在com.db下,这里不需要添加,如果加上,idea也会提示这样加是多余的,需要删除。
如果启动类在com.db.service,项目启动又需要扫描到整个项目注解内容,即注解能被spring容器扫描到,则在启动类加上@ComponentScan(basePackages = {“com.db”})
下面开始编写业务层代码,包括前端请求需要的controller和业务处理service
package com.db.service;
import com.db.pojo.AppUser;
import java.util.List;
public interface UserService {
/**
* 根据用户id查询用户信息
*/
public AppUser getUser(String userId);
/**
* 查询所有用户信息,可实现分页
*/
public List<AppUser> getAllUser(Integer page,
Integer pageSize);
}
实现类添加@Service注解,这样能被spring容器扫描到,注入该bean
package com.db.service.impl;
import com.db.pojo.AppUser;
import com.db.service.UserService;
import com.db.user.mapper.AppUserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private AppUserMapper appUserMapper;
@Override
public AppUser getUser(String userId) {
AppUser user = new AppUser();
user.setUserid(userId);
List<AppUser> userList = appUserMapper.select(user);
return userList.get(0);
}
@Override
public List<AppUser> getAllUser(Integer page, Integer pageSize) {
Example example = new Example(AppUser.class);
example.orderBy("createdTime").desc();
Example.Criteria criteria = example.createCriteria();
// criteria.andEqualTo("userid","");
PageHelper.startPage(page, pageSize);
List<AppUser> userList = appUserMapper.selectByExample(example);
return userList;
}
}
package com.db.controller.api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping("user")
public interface UserControllerApi {
@GetMapping("/getUserInfoByUserId")
public Object getUserInfoByUserId(@RequestParam String userId);
@GetMapping("/getAllUserInfo")
public Object getAllUserInfo(@RequestParam Integer page,
@RequestParam Integer pageSize);
}
package com.db.controller.api.impl;
import com.alibaba.fastjson.JSON;
import com.db.controller.api.UserControllerApi;
import com.db.pojo.AppUser;
import com.db.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserControllerImpl implements UserControllerApi {
@Autowired
private UserService userService;
@Override
public Object getUserInfoByUserId(String userId) {
AppUser userInfo = userService.getUser(userId);
System.out.println(JSON.toJSON(userInfo));
return userInfo;
}
@Override
public Object getAllUserInfo(Integer page,Integer pageSize) {
List<AppUser> userList = userService.getAllUser(page,pageSize);
System.out.println(JSON.toJSON(userList));
return userList;
}
}
浏览器输入地址
http://localhost:8090/user/getUserInfoByUserId?userId=1002
后台控制台打印
http://localhost:8090/user/getAllUserInfo?page=1&pageSize=3
后台控制台打印
完成!!!!