Commit 77adfaef authored by chenpengtao's avatar chenpengtao

殷小威作业

parent 9c5aaea3
<?xml version="1.0" encoding="UTF-8"?>
<?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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yxw</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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>2.1.1</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--@Slf4j自动化日志对象-log-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yxw;
package com.yxw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.yxw.config;
package com.yxw.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
\ No newline at end of file
package com.yxw.zuoye.controller;
package com.yxw.zuoye.controller;
import com.yxw.zuoye.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.yxw.zuoye.entity.Order;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@RequestMapping(value = "/order")
@Api("订单管理功能")
public class OrderController {
@Autowired
OrderService orderService;
@ApiOperation("添加订单")
@PostMapping(value = "/addOrder")
public boolean addOrder(Order order){
int n = orderService.insertOrder(order);
if(n>0)
return true;
else
return false;
}
@ApiOperation("查询订单")
@GetMapping(value = "/getOrders")
public List<Order> getOrders(){
log.info("查询订单");
return orderService.getOrders();
}
}
package com.yxw.zuoye.dao;
package com.yxw.zuoye.dao;
import com.yxw.zuoye.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface OrderMapper {
int insertOrder(Order order);
int insertOrderBatch(List<Order> orders);
List<Order> getOrders();
}
package com.yxw.zuoye.entity;
package com.yxw.zuoye.entity;
import com.alibaba.fastjson.JSONObject;
public class Order {
private String orderId;
private User userInfo;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public User getUserInfo() {
return userInfo;
}
public void setUserInfo(User userInfo) {
this.userInfo = userInfo;
}
}
package com.yxw.zuoye.entity;
package com.yxw.zuoye.entity;
public class User {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.yxw.zuoye.service;
package com.yxw.zuoye.service;
import com.yxw.zuoye.dao.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxw.zuoye.entity.Order;
import java.util.List;
@Service
public class OrderService {
@Autowired
OrderMapper orderMapper;
public int insertOrder(Order order){
return orderMapper.insertOrder(order);
}
public List<Order> getOrders(){
return orderMapper.getOrders();
}
}
package com.yxw.zuoye.typehandler;
package com.yxw.zuoye.typehandler;
import com.alibaba.fastjson.JSONObject;
import com.yxw.zuoye.entity.User;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.alibaba.fastjson.JSON;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserTypeHandler extends BaseTypeHandler<User> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, User user, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i,JSON.toJSONString(user));
}
@Override
public User getNullableResult(ResultSet resultSet, String s) throws SQLException {
User user = JSON.parseObject(resultSet.getString(s),User.class);
//JSONObject user = JSON.parseObject(resultSet.getString(s));
return user;
}
@Override
public User getNullableResult(ResultSet resultSet, int i) throws SQLException {
User user = JSON.parseObject(resultSet.getString(i),User.class);
//JSONObject user = JSON.parseObject(resultSet.getString(i));
return user;
}
@Override
public User getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
User user = JSON.parseObject(callableStatement.getString(i),User.class);
//JSONObject user = JSON.parseObject(callableStatement.getString(i));
return user;
}
}
server:
server:
port: 8088
servlet:
context-path: /demo
spring:
datasource:
username: root
password: yin55555
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.yxw.zuoye.entity
type-handlers-package: com.yxw.zuoye.typehandler
spring:
spring:
profiles:
active: dev
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 此xml在spring-boot-1.5.3.RELEASE.jar里 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<!-- 开启后可以通过jmx动态控制日志级别(springboot Admin的功能) -->
<!--<jmxConfigurator/>-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>C:/log/zuoye.log</File>
<encoder>
<pattern>%date [%level] [%thread] %logger{60} [%file : %line] %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 添加.gz 历史日志会启用压缩 大大缩小日志文件所占空间 -->
<!--<fileNamePattern>/home/hfw-client/hfw_log/stdout.log.%d{yyyy-MM-dd}.log</fileNamePattern>-->
<fileNamePattern>D:/log/hfw-client/hfw_log/stdout.log.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory><!-- 保留30天日志 -->
</rollingPolicy>
</appender>
<logger name="com.yxw.zuoye.dao" level="DEBUG" />
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?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.yxw.zuoye.dao.OrderMapper">
<resultMap id="BaseResultMap" type="com.yxw.zuoye.entity.Order">
<result column="orderId" jdbcType="VARCHAR" property="orderId" />
<result column="userInfo" jdbcType="VARCHAR" typeHandler="com.yxw.zuoye.typehandler.UserTypeHandler"/>
</resultMap>
<select id="getOrders" resultType="com.yxw.zuoye.entity.Order">
select * from ordertable
</select>
<insert id="insertOrder" parameterType="com.yxw.zuoye.entity.Order">
insert into ordertable(orderId,userInfo) values (#{orderId},#{userInfo})
</insert>
<insert id="insertOrderBatch">
INSERT INTO ordertable
(orderId, userInfo)
VALUES
<foreach collection ="list" item="order" separator =",">
(#{order.orderId},#{order.userInfo})
</foreach >
</insert>
</mapper>
\ No newline at end of file
package com.yxw.demo;
package com.yxw.demo;
import com.yxw.zuoye.dao.OrderMapper;
import com.yxw.zuoye.entity.Order;
import com.yxw.zuoye.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@SpringBootTest
@Slf4j
class DemoApplicationTests {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Test
void contextLoads() {
}
@Test
public void mybatisBatchOne() {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
try {
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
Order order = new Order();
User user = new User();
user.setUserId("userId");
user.setUserName("userName");
order.setOrderId("orderId");
order.setUserInfo(user);
orderMapper.insertOrder(order);
}
long end = System.currentTimeMillis();
log.info("耗时:" + (end - start));
} catch (Exception e) {
log.error(e.toString());
} finally {
session.commit();
session.close();
}
}
@Test
public void mybatisBatchTwo() {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
try {
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
Order order = new Order();
User user = new User();
user.setUserId("userId");
user.setUserName("userName");
order.setOrderId("orderId");
order.setUserInfo(user);
orderMapper.insertOrder(order);
}
long end = System.currentTimeMillis();
log.info("耗时:" + (end - start));
} catch (Exception e) {
log.error(e.toString());
} finally {
session.commit();
session.close();
}
}
@Test
public void saveOrderBatchOne() {
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
try {
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
long start = System.currentTimeMillis();
List<Order> orderList = new ArrayList<Order>();
for (int i = 0; i < 10000; i++) {
Order order = new Order();
User user = new User();
user.setUserId("userId");
user.setUserName("userName");
order.setOrderId("orderId");
order.setUserInfo(user);
orderList.add(order);
}
orderMapper.insertOrderBatch(orderList);
orderList.clear();
long end = System.currentTimeMillis();
log.info("耗时:" + (end - start));
} catch (Exception e) {
log.error(e.toString());
} finally {
session.commit();
session.close();
}
}
@Test
public void saveOrderBatchTwo() {
//设置ExecutorType.BATCH原理:把SQL语句发个数据库,数据库预编译好,数据库等待需要运行的参数,接收到参数后一次运行,ExecutorType.BATCH只打印一次SQL语句,多次设置参数步骤,
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH);
try {
OrderMapper orderMapper = session.getMapper(OrderMapper.class);
long start = System.currentTimeMillis();
List<Order> orderList = new ArrayList<Order>();
for (int i = 0; i < 10000; i++) {
Order order = new Order();
User user = new User();
user.setUserId("userId");
user.setUserName("userName");
order.setOrderId("orderId");
order.setUserInfo(user);
orderList.add(order);
}
orderMapper.insertOrderBatch(orderList);
orderList.clear();
long end = System.currentTimeMillis();
log.info("耗时:" + (end - start));
} catch (Exception e) {
log.error(e.toString());
} finally {
session.commit();
session.close();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment