作为比较火的微服务,刚好做了springcloud项目 趁机把springboot整理下。大家可以借鉴以及给我好的建议
标题创建maven项目
![首先使用spring初始化向导]
(https://img-blog.csdnimg.cn/20190128191529639.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzI5MTE3OA==,size_16,color_FFFFFF,t_70)
![配置项目信息]
(https://img-blog.csdnimg.cn/20190128192206826.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzI5MTE3OA==,size_16,color_FFFFFF,t_70)
创建完成在pom.xml加入版本控制
<dependencyManagement>
<!--加入版本控制-->
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Brussels-SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置好application.yml
#数据库的配置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/dreamidlefish?useUniCode=yes&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
#端口号配置
server:
port: 8080
# mybatis配置
mybatis:
type-aliases-package: com.dc.dreamidlefish.pojo
mapper-locations: classpath:mapper/*.xml
进行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="com.dc.dreamidlefish.mapper.UserMapper">
<select id="queryAll" resultType="com.dc.dreamidlefish.pojo.User" >
select * from t_user
</select>
</mapper>
写好UserMapper接口
package com.dc.dreamidlefish.mapper;
import com.dc.dreamidlefish.pojo.User;
import java.util.List;
public interface UserMapper {
//查询所有的用户
public List<User> queryAll();
}
配置之后对启动类加上注解使mapper生效
package com.dc.dreamidlefish;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.dc.dreamidlefish.mapper")
@SpringBootApplication
public class DreamidlefishApplication {
public static void main(String[] args) {
SpringApplication.run(DreamidlefishApplication.class, args);
}
}
编写user域模型
package com.dc.dreamidlefish.pojo;
import lombok.Data;
import java.util.Date;
@Data//在这里使用了lombok
public class User {
//用户id
private Integer id;
//用户姓名
private String username;
//用户登录名
private String loginname;
//用户密码
private String password;
//用户电话
private String mobile;
//用户邮箱
private String email;
//用户家庭地址
private String address;
//用户创建时间
private Date createtime;
//用户修改时间
private Date updatetimme;
//用户状态
private String state;
}
编写service以及实现类
package com.dc.dreamidlefish.service;
import com.dc.dreamidlefish.pojo.User;
import java.util.List;
public interface UserService {
//查询所有用户
public List<User> queryAll();
}
package com.dc.dreamidlefish.service.impl;
import com.dc.dreamidlefish.mapper.UserMapper;
import com.dc.dreamidlefish.pojo.User;
import com.dc.dreamidlefish.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/*
*实现类
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> queryAll() {
List<User> list = userMapper.queryAll();
return list;
}
}
加入controller因为用到前后端分离使用restful风格
package com.dc.dreamidlefish.controller;
import com.dc.dreamidlefish.pojo.User;
import com.dc.dreamidlefish.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//这个注解相当于之前Controller和 @ResponseBody两者相结合
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
//测试get请求
@GetMapping("test")
public List<User> test(){
List<User> list = userService.queryAll();
return list;
}
@GetMapping("te")
public String te(){
String m = "111";
return m;
}
}
最后使用api测试工具展示效果
在这里推荐大家掌握一款api因为后续如果加入权限的话需要使用api测试工具