自己的一个 springboot + mybatis 小项目

好久没写博客了,最近想更深入的学习一下spring boot,所以自己新建了一个小项目,使用spring boot + mybatis + mysql的案例实现一个简单的查询操作,主要是保留一下各种配置文件,免得以后找文件恼火。

一、目录结构

在这里插入图片描述

二、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.0.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.liuyang</groupId>
	<artifactId>miaosha</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>miaosha</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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>1.1.10</version>
		</dependency>
        
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

需要注意的是,这里用的是阿里巴巴的druid连接池。

三、application.yml文件

spring: 
  datasource:
    url: jdbc:mysql://localhost:3306/miaosha
    username: root
    password: liuyang
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    
mybatis: 
    configuration:    
            map-underscore-to-camel-case: true

而mybatis的这段配置是开启自动驼峰模式。

四、Controller层

package com.liuyang.miaosha.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.liuyang.miaosha.controller.viewobject.UserVO;
import com.liuyang.miaosha.entity.UserDO;
import com.liuyang.miaosha.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired(required=true)
	private UserService userService;
	
	@RequestMapping("/{id}")
	public UserVO test(@PathVariable Integer id) {
		return convertFromDO(userService.selectUserById(id));
	}
	
	private UserVO convertFromDO(UserDO userDO) {
		if(userDO == null) {
			return null;
		}
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(userDO, userVO);
		return userVO;
	}

}

细心的同学发现了,这里有个DO转VO的方法,在我的理解来说,DO就是数据库字段所一一对应的类,但是我们有些字段我们不想返回给前端,所以设置一个VO类专门写返回前端的字段。

五、UserDO类

package com.liuyang.miaosha.entity;

public class UserDO {
	
	private Integer id;
	private String name;
	private Byte gender;
	private Integer age;
	private String telephone;
	private String registerMode;
	private String thirdPartyId;
	
	public UserDO() {}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Byte getGender() {
		return gender;
	}
	public void setGender(Byte gender) {
		this.gender = gender;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	
	public String getRegisterMode() {
		return registerMode;
	}

	public void setRegisterMode(String registerMode) {
		this.registerMode = registerMode;
	}

	public String getThirdPartyId() {
		return thirdPartyId;
	}

	public void setThirdPartyId(String thirdPartyId) {
		this.thirdPartyId = thirdPartyId;
	}

	@Override
	public String toString() {
		return "UserDO [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", telephone="
				+ telephone + ", registerMode=" + registerMode + ", thirdPartyId=" + thirdPartyId + "]";
	}
	
}

六、UserVO类

package com.liuyang.miaosha.controller.viewobject;

public class UserVO {
	
	private Integer id;
	private String name;
	private Byte gender;
	private Integer age;
	private String telephone;
	
	public UserVO() {}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Byte getGender() {
		return gender;
	}

	public void setGender(Byte gender) {
		this.gender = gender;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	@Override
	public String toString() {
		return "UserVO [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", telephone="
				+ telephone + "]";
	}
}

由此可见,返回前端的就只有 id、 name、gender、age、telephone这五个字段。

七、Service层

1、Service接口

package com.liuyang.miaosha.service;

import com.liuyang.miaosha.entity.UserDO;

public interface UserService {
	UserDO selectUserById(Integer id);
}

2、Service实现类

package com.liuyang.miaosha.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuyang.miaosha.entity.UserDO;
import com.liuyang.miaosha.mapper.UserMapper;
import com.liuyang.miaosha.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public UserDO selectUserById(Integer id) {
		return userMapper.selectUserById(id);
	}
}

八、UserMapper层

1、UserMapper接口

package com.liuyang.miaosha.mapper;

import com.liuyang.miaosha.entity.UserDO;

public interface UserMapper {
	UserDO selectUserById(Integer id);
}

2、UserMapper.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.liuyang.miaosha.mapper.UserMapper">
	<select id="selectUserById" parameterType="int" resultType="com.liuyang.miaosha.entity.UserDO">
		SELECT * FROM user_info WHERE id = #{id}
	</select>
</mapper>

九、数据库字段

在这里插入图片描述

一共七个字段,但返回前端的就只有前面五个字段。

十、项目启动

项目搭建好后直接启动MiaoshaApplication,但是在启动之前需要注意加上MapperScan,上代码:

package com.liuyang.miaosha;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.liuyang.miaosha.mapper")
public class MiaoshaApplication {

	public static void main(String[] args) {
		SpringApplication.run(MiaoshaApplication.class, args);
	}
}

加上后就可以直接启动了,浏览器访问http://localhost:8080/user/1,便能得到数据库的数据。
在这里插入图片描述

没有更多推荐了,返回首页