SpringBoot的学习
搭建SpringBoot的运行demo网上都很多教程!第一次搭建的时候需要加载SpringBoot的插件需要一定的耐心!
以上是创建好SpringBoot的工程文件目录
第一:先来介绍SpringBoot的配置文件
注意配置文件里面不要写中文注释
这是application.yaml 的配置文件:可以看到这里是开发环境
spring:
profiles:
active: dev
application-dev.yml
## 启动服务的端口
server:
port: 8088
spring:
thymeleaf:
cache: false
## 配置mysql数据库连接
datasource:
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
## 注意这里的用户名和密码
username: root
## 注意这里的是password 不是data-password
password: 123456
## 配置mybatis的配置文件
mybatis:
## 配置别名
type-aliases-package: com.example.entity
## 配置*Mapper.xml的文件的路径
mapper-locations: mapper/*.xml
第二 再来介绍SpringBoot的pom.xml配置文件
pom.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 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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot1</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.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</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>
第三来介绍工程每个文件夹的内容
这里的mapper pojo 以及mappe.xml文件都是mybatis的逆向工程生成的
com.example.entity
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String realname;
private static final long serialVersionUID = 1L;
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 == null ? null : username.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname == null ? null : realname.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", username=").append(username);
sb.append(", password=").append(password);
sb.append(", realname=").append(realname);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
com.example.entity.UserExample
public class UserExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public UserExample() {
oredCriteria = new ArrayList<Criteria>();
}
....省略代码.....
接着我们可以从mapper的接口开始
com.example.mapper.UserMapper
@Repository //这里需要加上Repository 这个注释 在Service注入时候需要这个注解
public interface UserMapper {
long countByExample(UserExample example);
int deleteByExample(UserExample example);
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
List<User> selectByExample(UserExample example);
User selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
Service的文件内容
com.example.service.UserService
public interface UserService {
//查询所有的记录
List<User> findAll();
}
com.example.service.impl.UserServiceImpl
@Service(value = "service")
public class UserServiceImpl implements UserService {
@Autowired
//如果mapper的接口中不加注解会报错误(加上@Repository这个注解之后错误消失)
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectByExample(null);
}
}
Controller控制层的代码
com.example.controller.UserController
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
最后我们需要在SpringApplication上加上注解
@SpringBootApplication
@MapperScan("com.example.mapper")//扫描mapper接口里面的方法
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
}
}
启动之后再浏览器输入http://localhost:8088/findAll 就可以获取下面的数据
[{"id":1,"username":"apple","password":"apple","realname":"披萨猫"},{"id":2,"username":"bear","password":"bear","realname":"乐购鼠"}]