1:第一步选择file-new-project
2:选中Spring initializr,点击Next3:创建对应包名跟项目名称
4:选择想要的然后点击next
5:然后点击Finish
1:配置JDK
2:配置maven,点击file-setting
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- StringUtils -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring:
profiles:
active: local
datasource:
dricer-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/Test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: admin
password: admin123
server:
port: 8080
servlet:
context-path: /
#springboot整合mybatis
mybatis:
# 加载mapper映射文件
mapper-locations: classpath:/mapper/*.xml
logging:
path: ./logs
level:
#项目中dao层的位置
com:
example:
demo:
mapper: debug
file:
#日志文件的大小设置
max-size: 100MB
/**
* @author 白
*/
@RestController
@RequestMapping(value = "/user")
public class UserController {
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
/**
* 查询用户
* @param
* @return
*/
@RequestMapping(value = "/listUser",method = RequestMethod.POST)
public Result listUser() {
try {
List<User> result = userService.listUser();
LOGGER.info("调用查询用户接口-出参[{}]", result);
return new Result(result);
}catch (Exception e){
return new Result<>(-1,"错误");
}
}
/**
* 新增用户
* @param
* @return
*/
@RequestMapping(value = "/insertUser",method = RequestMethod.POST)
public Result insertUser(@RequestBody User user) {
try {
LOGGER.info("调用新增用户接口-传参[{}]", JSONObject.toJSONString(user);
userService.insertUser(user);
return new Result();
}catch (Exception e){
return new Result<>(-1,"错误");
}
}
/**
* 修改用户
* @param
* @return
*/
@RequestMapping(value = "/updateUser",method = RequestMethod.POST)
public Result updateUser(@RequestBody User user) {
try {
LOGGER.info("调用修改用户接口-传参[{}]", JSONObject.toJSONString(user);
userService.updateUser(user);
return new Result();
}catch (Exception e){
return new Result<>(-1,"错误");
}
}
/**
* 删除用户
* @param
* @return
*/
@RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
public Result deleteUser(@RequestParam @Valid Integer id) {
try {
LOGGER.info("调用删除用户接口-传参[{}]", id);
userService.deleteUser(id);
return new Result();
}catch (Exception e){
return new Result<>(-1,"错误");
}
}
}
/**
* @author 白
*/
public interface UserService {
/**
* 查询用户
* @return
*/
List<User> listUser();
/**
* 新增用户
* @param user
*/
void insertUser(User user);
/**
* 修改用户
* @param user
*/
void updateUser(User user);
/**
* 删除用户
* @param id
*/
void deleteUser(Integer id);
}
/**
* @author 白
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
/**
* 查询用户
* @return
*/
@Override
public List<User> listUser() {
List<User> list = userMapper.listUser();
return list;
}
/**
* 新增用户
* @param user
*/
@Override
public void insertUser(User user) {
userMapper.insertUser(user);
}
/**
* 修改用户
* @param user
*/
@Override
public void updateUser(User user) {
userMapper.updateUser(user);
}
/**
* 删除用户
* @param id
*/
@Override
public void deleteUser(Integer id) {
userMapper.deleteUser(id);
}
}
/**
* @author 白
*/
@Mapper
public interface UserMapper {
/**
* 查询用户
* @return
*/
List<User> listUser();
/**
* 新增用户
* @param user
*/
void insertUser(User user);
/**
* 修改用户
* @param user
*/
void updateUser(User user);
/**
* 删除用户
* @param id
*/
void deleteUser(Integer id);
}
<mapper namespace="com.example.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="isdel" property="isdel" jdbcType="INTEGER"/>
</resultMap>
<select id="listUser" resultMap="BaseResultMap">
SELECT id,name,isdel FROM user
</select>
<insert id="insertUser" parameterType="com.example.demo.entity.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name}
</if>
</trim>
</insert>
<update id="updateUser" parameterType="com.example.demo.entity.User">
update user
<set>
<if test="name != null">
name = #{name},
</if>
</set>
where id = #{id}
</update>
<update id="deleteUser" parameterType="java.lang.Integer">
UPDATE `user`
SET isdel = 1
WHERE
id = #{id}
</update>
</mapper>
/**
* 接口返回通用类
* @author 白
* @param <T>
*/
@Data
public class Result<T> implements Serializable {
public static final int SUCCESS_CODE = 0;
public static final String ERROR_MSG = "fail";
/**
* 状态码
*/
private int code;
/**
* 说明信息
*/
private String message;
/**
* result结果,快速判断
*/
private String result;
/**
* 结果数据
*/
private T data;
public Result() {
this(SUCCESS_CODE, "成功","success",null);
}
public Result(int code, String msg) {
this(code, msg, ERROR_MSG, null);
}
public Result(T data) {
this(SUCCESS_CODE, "成功", "success", data);
}
public Result(int code, String message, String result, T data) {
this.code = code;
this.message = message;
this.result = result;
this.data=data;
}
@Override
public String toString() {
return "Result [code=" + code + ", message=" + message + ", result=" + result + ", data=" + data + "]";
}
}
一个简单的spring boot+mybatis增删改查就写完了