Spring Boot 整合 Mybatis (一) mybatis-spring-boot-starter
spring-boot
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
项目地址:https://gitcode.com/gh_mirrors/sp/spring-boot
免费下载资源
·
mybatis-spring-boot-autoconfigure :http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
mybatis:http://www.mybatis.org/mybatis-3/zh/index.html
1.maven依赖
<?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 http://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.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>priv.gitonlie.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis</name>
<description>mybatis project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<mybatis.springboot.version>1.3.2</mybatis.springboot.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>${mybatis.springboot.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</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>
<plugin><!-- 跳过单元测试 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.application.yml配置
spring:
application:
name: mybatis-server
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/flowable_demo?useUnicode=true&characterEncoding=utf-8
username: root
password: admin123456
server:
port: 8081
mybatis:
type-aliases-package: priv.gitonlie.mybatis.entity
mapper-locations:
- classpath:mapper/*.xml
3.实体类
public class User {
private String id;
private String name;
private String password;
private String token;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
4.Mapper接口
public interface UserMapper {
List<User> queryAllUser();
}
5.mapper.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="priv.gitonlie.mybatis.mapper.UserMapper">
<select id="queryAllUser" resultType="User">
select * from user
</select>
</mapper>
6.service服务层
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> queryAll(){
return userMapper.queryAllUser();
}
}
7.controller控制层
@RestController
public class UserController {
@Autowired
private UserService service;
@GetMapping("demo")
public List<User> queryAll(){
List<User> list = service.queryAll();
return list;
}
}
8.启动访问
GitHub 加速计划 / sp / spring-boot
12
5
下载
spring-projects/spring-boot: 是一个用于简化Spring应用开发的框架。适合用于需要快速开发企业级Java应用的项目。特点是可以提供自动配置、独立运行和内置的Tomcat服务器,简化Spring应用的构建和部署。
最近提交(Master分支:3 个月前 )
60e0de79 - 2 天前
c9e8174f - 2 天前
更多推荐
已为社区贡献3条内容
所有评论(0)