构建stock_backend主业务工程
在stock_parent工程下构建子工程stock_backend,整个过程与stock_common工程一致;
1.引入依赖
tock_backend工程被stock_parent父工程聚合,pom配置如下:
<?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">
<parent>
<artifactId>stock_parent</artifactId>
<groupId>com.itheima.stock</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>股票后端核心工程</description>
<artifactId>stock_backend</artifactId>
<!--不指定打包方式默认就是jar-->
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--引入公共依赖-->
<dependency>
<groupId>com.itheima.stock</groupId>
<artifactId>stock_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- 基本依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--打包名称-->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置yml
# web定义
server:
port: 8091
spring:
# 配置mysql数据源
datasource:
druid:
username: root
password: root
url: jdbc:mysql://192.168.200.131:3306/stock_db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
# 初始化时建立物理连接的个数。初始化发生在显示调用 init 方法,或者第一次 getConnection 时
initialSize: 6
# 最小连接池数量
minIdle: 2
# 最大连接池数量
maxActive: 20
# 获取连接时最大等待时间,单位毫秒。配置了 maxWait 之后,缺省启用公平锁,
# 并发效率会有所下降,如果需要可以通过配置 useUnfairLock 属性为 true 使用非公平锁。
maxWait: 60000
# 配置mybatis
mybatis:
type-aliases-package: com.itheima.stock.pojo
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true # 开启驼峰映射
# pagehelper配置
pagehelper:
helper-dialect: mysql #指定分页数据库类型(方言)
reasonable: true #合理查询超过最大页,则查询最后一页
3.定义main启动类
package com.itheima.stock;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author by itheima
* @Date 2021/12/29
* @Description 定义main启动类
*/
@SpringBootApplication
@MapperScan("com.itheima.stock.mapper")
public class StockApp {
public static void main(String[] args) {
SpringApplication.run(StockApp.class,args);
}
}
4.测试
定义mapper接口方法
StockUserMapper接口和xml定义查询所有股票业务信息的接口方法:
/**
* 根据用户名称查询用户信息
* @param userName 用户名称
* @return
*/
SysUser findByUserName(@Param("name") String userName);
<!--根据用户名称查询用户信息-->
<select id="findByUserName" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_user
where username= #{name}
</select>
定义服务接口及实现
package com.itheima.stock.service;
import com.itheima.stock.pojo.entity.SysUser;
/**
* @author by itheima
* @Date 2022/6/29
* @Description 定义用户服务接口
*/
public interface UserService {
/**
* 根据用户查询用户信息
* @param userName 用户名称
* @return
*/
SysUser getUserByUserName(String userName);
}
定义UserService服务接口实现:
package com.itheima.stock.service.impl;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author by itheima
* @Date 2022/6/29
* @Description 定义user服务实现
*/
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private SysUserMapper sysUserMapper;
/**
* 根据用户名称查询用户信息
* @param userName 用户名称
* @return
*/
@Override
public SysUser getUserByUserName(String userName) {
SysUser user=sysUserMapper.findByUserName(userName);
return user;
}
}
定义web访问接口
package com.itheima.stock.controller;
import com.itheima.stock.pojo.entity.SysUser;
import com.itheima.stock.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author by itheima
* @Date 2022/6/29
* @Description 定义用户处理器接口
*/
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
/**
* 根据用户名查询用户信息
* @param userName
* @return
*/
@GetMapping("/user/{userName}")
public SysUser getUserByUserName(@PathVariable("userName") String userName){
return userService.getUserByUserName(userName);
}
}
启动项目测试