create database day045;
use day045;
create table user(
uid varchar(20) primary key,
username varchar(50)
);
create table `order`(
oid varchar(20) primary key,
price double,
user_id varchar(20)
);
insert into user(uid,username) values('u001','jack'),('u002','rose');
insert into `order`(oid,price,user_id) values('x001',55,'u001');
insert into `order`(oid,price,user_id) values('x002',34,'u001');
insert into `order`(oid,price,user_id) values('x003',754,'u001');
insert into `order`(oid,price,user_id) values('x004',3453,'u002');
insert into `order`(oid,price,user_id) values('x005',222,'u002');
项目名:day052_ssm
pom文件:lombok、mysql driver、MyBatis、web、test、swagger
yml文件:端口、数据源dataSource *、日志
启动类:
拷贝配置类:swagger相关
测试类:TestMapper、TestService (可选)
pom文件:lombok、mysql driver、MyBatis、web、test、swagger
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<mybatis.starter.version>1.3.2mybatis.starter.version>
<mysql.version>5.1.32mysql.version>
<swagger.version>2.7.0swagger.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.starter.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>${swagger.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>${swagger.version}version>
dependency>
dependencies>
yml文件:端口、数据源dataSource *、日志
#端口号
server:
port: 8080
#数据源(连接池)
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/day045?useUnicode=true&characterEncoding=utf8
username: root
password: '1234'
#javabean与表驼峰映射
mybatis:
configuration:
map-underscore-to-camel-case: true
#日志,给com.czxy.mapper包设置自己的日志级别
logging:
level:
com:
czxy:
mapper: debug
启动类:
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
拷贝配置类:swagger相关
测试类:TestMapper、TestService (可选)
*
package com.czxy.mapper;
import com.czxy.OrderApplication;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = OrderApplication.class)
public class TestOrderMapper {
}
package com.czxy.domain;
import lombok.Data;
@Data
public class Order {
private String oid;
private Integer price;
private String userId;
}
编写OrderMapper接口
编写OrderService接口
编写OrderServiceImpl实现类
编写OrderController
编写OrderMapper接口
package com.czxy.mapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OrderMapper {
}
编写OrderService接口
package com.czxy.service;
public interface OrderService {
}
编写OrderServiceImpl实现类
package com.czxy.service.impl;
import com.czxy.mapper.OrderMapper;
import com.czxy.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
@Resource
private OrderMapper orderMapper;
}
编写OrderController
package com.czxy.controller;
import com.czxy.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/order")
public class OrderController {
@Resource
private OrderService orderService;
}
编写OrderMapper接口中的selectAll方法
编写OrderService接口中的selectAll方法
编写OrderServiceImpl实现类中的selectAll方法
编写OrderController中的selectAll方法
package com.czxy.mapper;
import com.czxy.domain.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface OrderMapper {
/**
* 查询所有
* @return
*/
@Select("select * from `order`")
public List<Order> selectAll();
}
package com.czxy.service;
import com.czxy.domain.Order;
import java.util.List;
public interface OrderService {
/**
* 查询所有
* @return
*/
public List<Order> selectAll();
}
package com.czxy.service.impl;
import com.czxy.domain.Order;
import com.czxy.mapper.OrderMapper;
import com.czxy.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
@Resource
private OrderMapper orderMapper;
@Override
public List<Order> selectAll() {
//查询
List<Order> orderList = orderMapper.selectAll();
//返回
return orderList;
}
}
package com.czxy.controller;
import com.czxy.domain.Order;
import com.czxy.service.OrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Resource
private OrderService orderService;
@GetMapping
public ResponseEntity<List<Order>> selectAll() {
//查询
List<Order> orderList = orderService.selectAll();
//返回
return ResponseEntity.ok(orderList);
}
}
编写OrderMapper接口中的selectById方法
编写OrderService接口中的selectById方法
编写OrderServiceImpl实现类中的selectById方法
编写OrderController中的selectById方法
/**
* 查询详情
* @param oid
* @return
*/
@Select("select * from `order` where oid=#{oid}")
public Order selectById(@Param("oid") String oid);
/**
* 通过id查询详情
* @param oid
* @return
*/
public Order selectById(String oid);
@Override
public Order selectById(String oid) {
//查询
Order order = orderMapper.selectById(oid);
//返回
return order;
}
/**
* 查询详情
* @param oid
* @return
*/
@GetMapping("/{oid}")
public ResponseEntity<Order> selectById(@PathVariable("oid") String oid) {
//查询
Order order = orderService.selectById(oid);
//返回
return ResponseEntity.ok(order);
}