diff --git a/mogdb-springmvc-mybatis-demo/pom.xml b/mogdb-springmvc-mybatis-demo/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..a0d60c188ce033d88e28d6277bc383c3755b642b --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/pom.xml @@ -0,0 +1,398 @@ + + + + + io.mogdb + mogdb-demo + 1.0 + + 4.0.0 + + com.enmotech + mogdb-springmvc-mybatis-demo + war + mogdb-springmvc-mybatis Maven Webapp + + + UTF-8 + 1.8 + 1.8 + 5.2.4.RELEASE + 4.0.0-b07 + 1.2 + 1.1.18 + 3.2.7 + 1.2.2 + 5.0.1 + 6.0.0 + 4.13 + 1.7.25 + 2.8.2 + + + + + + org.springframework + spring-web + ${spring.version} + + + + org.springframework + spring-core + ${spring.version} + + + + commons-logging + commons-logging + + + + + + org.springframework + spring-beans + ${spring.version} + + + + org.springframework + spring-context + ${spring.version} + + + commons-logging + commons-logging + + + + + + org.springframework + spring-context-support + ${spring.version} + + + + org.springframework + spring-aop + ${spring.version} + + + + org.springframework + spring-aspects + ${spring.version} + + + + org.springframework + spring-expression + ${spring.version} + + + + org.springframework + spring-tx + ${spring.version} + + + + org.aspectj + aspectjweaver + 1.9.2 + + + + org.springframework + spring-test + ${spring.version} + + + + + org.springframework + spring-webmvc + ${spring.version} + + + + + javax.servlet + javax.servlet-api + ${javax.servlet.version} + + + + jstl + jstl + ${jstl.version} + + + + + org.mybatis + mybatis + ${mybatis.version} + + + + + org.opengauss + opengauss-jdbc + ${opengauss.jdbc.version} + + + + + + + + + + com.github.pagehelper + pagehelper + ${pagehelper.version} + + + + + org.springframework + spring-jdbc + ${spring.version} + + + + + com.mchange + c3p0 + 0.9.5.2 + + + + org.mybatis + mybatis-spring + ${mybatis.spring.version} + + + + + com.alibaba + fastjson + 1.2.69 + + + + org.apache.commons + commons-collections4 + 4.4 + + + + org.apache.commons + commons-lang3 + 3.8.1 + + + + junit + junit + ${junit.version} + + + + + org.oracle.com + ojdbc + 7.1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + com.fasterxml.jackson.core + jackson-core + 2.9.3 + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.3 + + + + + org.slf4j + slf4j-api + ${slf4j-log4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j-log4j.version} + runtime + + + + + + org.apache.logging.log4j + log4j-core + ${log4j2.version} + runtime + + + org.apache.logging.log4j + log4j-web + ${log4j2.version} + runtime + + + org.apache.logging.log4j + log4j-api + ${log4j2.version} + runtime + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j2.version} + runtime + + + + + + mogdb-spring-mybatis + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-war-plugin + 3.2.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + org.apache.tomcat.maven + tomcat8-maven-plugin + 3.0-r1655215 + + src/main/webapp + 8099 + UTF-8 + + + + + + + + + nexus + Team Nexus Repository + http://192.168.64.138:8081/nexus/content/groups/public + + true + + + true + + + + + + + nexus + Team Nexus Plugin Repository + http://192.168.64.138:8081/nexus/content/groups/public + + true + + + false + + + + + alfresco-public + https://artifacts.alfresco.com/nexus/content/groups/public + + + alfresco-public-snapshots + https://artifacts.alfresco.com/nexus/content/groups/public-snapshots + + true + daily + + + + + + + releases + http://192.168.64.138:8081/nexus/content/repositories/releases + + + snapshots + http://192.168.64.138:8081/nexus/content/repositories/snapshots + + + diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/IndexController.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/IndexController.java new file mode 100644 index 0000000000000000000000000000000000000000..bd44e93b366d14ea34bbfb6cdf5f6a00de5f967d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/IndexController.java @@ -0,0 +1,74 @@ +package io.mogdb.springmvc.mybatis.controller; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import io.mogdb.springmvc.mybatis.service.MyStudentService; +import io.mogdb.springmvc.mybatis.service.OmOfferingInstService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.sql.SQLException; +import java.util.Map; + +@RequestMapping("/") +@Controller +public class IndexController { + @Autowired + private MyStudentService myStudentService; + + @Autowired + private OmOfferingInstService omOfferingInstService; + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String index() { + return "index"; + } + + @RequestMapping("/index/v1") + @ResponseBody + public String indexString() { + return "index"; + } + + @RequestMapping(value = "/index/test", method = RequestMethod.GET) + public String printHelloWorld(ModelMap modelMap) { + // add attribute to load modelMap + modelMap.addAttribute("message", + "test mogdb spring mybatis in Spring MVC!"); + // return the name of the file to be loaded "test.jsp" + return "test"; + } + + @RequestMapping("/index/testTs") + @ResponseBody + public String testTrans(StudentEntity studentEntity) { + return myStudentService.testTs(studentEntity); + } + + @RequestMapping("/index/testTsV2") + @ResponseBody + public String testTsV2(StudentEntity studentEntity) { + return myStudentService.updateV2(studentEntity); + } + + @RequestMapping("/index/testBatch") + @ResponseBody + public String testBatch(Map data) { + // 分区表,inset into select + // omOfferingInstService.batchInsertInst(data); + // 非分区表,insert into select + omOfferingInstService.batchInsertInstV2(data); + // 非分区表,insert into values (),() + // omOfferingInstService.batchInsertInstV3(data); + return "success"; + } + + @RequestMapping("/index/testCopy") + @ResponseBody + public String testCopyTable() throws SQLException, ClassNotFoundException { + return omOfferingInstService.testCopyTable(); + } +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/StudentController.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/StudentController.java new file mode 100644 index 0000000000000000000000000000000000000000..525c5970bc5f9684aa86a99c9a404b9c26990673 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/controller/StudentController.java @@ -0,0 +1,147 @@ +package io.mogdb.springmvc.mybatis.controller; + +import com.alibaba.fastjson.JSONObject; +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import io.mogdb.springmvc.mybatis.service.StudentService; +import com.github.pagehelper.PageInfo; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import java.util.List; + + +@Controller +@RequestMapping("/student") +public class StudentController { + private static final Logger logger = LoggerFactory.getLogger(StudentController.class); + + @Autowired + private StudentService studentService; + + @RequestMapping("/testBatch") + @ResponseBody + public String testAddTestBatchRecord() { + int rows = studentService.addTestBatchRecord(); + if (rows > 0) { + return "插入 test_batch 成功"; + } else { + return "插入 test_batch 失败"; + } + } + + @RequestMapping("/findAll") + public ModelAndView findAll() { + List entities = studentService.findAllStudent(); + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("student"); + modelAndView.addObject("list", entities); + logger.info("size={}", entities.size()); + return modelAndView; + } + + @RequestMapping("/findPage") + @ResponseBody + public PageInfo findPage() { + PageInfo pageInfo = studentService.findPageStudent(); + logger.info("size={}", pageInfo.getSize()); + return pageInfo; + } + + @ResponseBody + @RequestMapping("/getList") + public String getStudentList() { + List entities = studentService.findAllStudent(); + String studentList = JSONObject.toJSONString(entities); + return studentList; + } + + public static void main(String[] args) { + System.err.println((int) (Math.random() * 100)); + } + + @ResponseBody + @RequestMapping("/addRecord") + public String addRecord(StudentEntity studentEntity) { + if (studentEntity == null || studentEntity.getClassId() < 1) { + int classId = (int) (Math.random() * 100); + studentEntity = new StudentEntity(); + studentEntity.setName("zp" + classId); + studentEntity.setClassId(classId); + studentEntity.setStatus(0); + } else if (StringUtils.isBlank(studentEntity.getName())) { + int classId = studentEntity.getClassId(); + studentEntity.setName("zp" + classId); + } + studentService.insertStudent(studentEntity); + String result = "insertStudent result={" + studentEntity.getId() + "}"; + return result; + } + + @ResponseBody + @RequestMapping("/deleteRecord") + public String deleteRecord(StudentEntity studentEntity) { + if (studentEntity == null || studentEntity.getId() < 1) { + studentEntity.setId(-1); + } + int count = studentService.deleteStudent(studentEntity.getId()); + if (count > 0) { + return "deleteStudent result={" + studentEntity.getId() + "}"; + } else { + return "数据不存在,删除失败。"; + } + } + + @ResponseBody + @RequestMapping("/updateRecord") + public String updateRecord(StudentEntity studentEntity) { + if (studentEntity == null || studentEntity.getId() < 1) { + studentEntity.setId(-1); + } + int count = studentService.updateStudent(studentEntity); + if (count > 0) { + return "updateStudent result={" + studentEntity.getId() + "}"; + } else { + return "数据不存在,更新失败。"; + } + } + + @ResponseBody + @RequestMapping("/updateRecordV2") + public String updateRecordV2(StudentEntity studentEntity) { + StudentEntity stu1 = studentService.findAllStudent().stream().findFirst().get(); + int count = studentService.updateStudent(stu1); + studentService.deleteStudent(stu1.getId()); + if (count > 0) { + return "updateStudent result={" + studentEntity.getId() + "}"; + } else { + return "数据不存在,更新失败。"; + } + } + + @ResponseBody + @RequestMapping("/updateRecordV3") + public String updateRecordV3(StudentEntity studentEntity) { + studentService.testV3(studentEntity); + return "success"; + } + + @ResponseBody + @RequestMapping("/updateRecordV4") + public String updateRecordV4(StudentEntity studentEntity) { + studentService.insertStudentV2(studentEntity); + return "success"; + } + + @ResponseBody + @RequestMapping("/testV5") + public String testV5(StudentEntity studentEntity) { + studentService.testV5(studentEntity); + return "success"; + } +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/OmOfferingInstDAO.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/OmOfferingInstDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..8913c3c8e72d9de310ac47e4279bfc0885ec484d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/OmOfferingInstDAO.java @@ -0,0 +1,12 @@ +package io.mogdb.springmvc.mybatis.dao; + +import org.springframework.stereotype.Repository; + +import java.util.Map; + +@Repository +public interface OmOfferingInstDAO { + int batchInsert(Map params); + + int executeCMD(); +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/StudentDAO.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/StudentDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..8c0aeacd43c98bf8adb0e6d4c3b26b50c7ba6bd2 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/dao/StudentDAO.java @@ -0,0 +1,32 @@ +package io.mogdb.springmvc.mybatis.dao; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +@Repository +public interface StudentDAO { + + Integer insertStudent(@Param("studentEntity") StudentEntity studentEntity); + + Integer updateStudent(@Param("studentEntity") StudentEntity studentEntity); + + Integer deleteStudent(@Param("id") int id); + + List findAllValidStudent(); + + StudentEntity findStudentById(@Param("id") int id); + + int batchInsert(Map data); + + int batchInsertV2(Map data); + + int executeCMD(); + + int addTestRBatchRecord(@Param("col1") int col1, @Param("col2") String col2); + + int addTestRBatchRecordV2(Map data); +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/entity/StudentEntity.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/entity/StudentEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..f8b137febad58c8340d60e5a0f0905acb11f2f2d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/entity/StudentEntity.java @@ -0,0 +1,67 @@ +package io.mogdb.springmvc.mybatis.entity; + +import java.io.Serializable; +import java.util.Date; + +public class StudentEntity implements Serializable { + private static final long serialVersionUID = 7475710140661551551L; + + private int id; //学号 + private String name; //姓名 + private int classId; //班级 + private int status; //是否有效(1:有效,-1:无效) + private Date addTime; //添加时间 + private Date updateTime; //更新时间 + + public static long getSerialVersionUID() { + return serialVersionUID; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getClassId() { + return classId; + } + + public void setClassId(int classId) { + this.classId = classId; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public Date getAddTime() { + return addTime; + } + + public void setAddTime(Date addTime) { + this.addTime = addTime; + } + + public Date getUpdateTime() { + return updateTime; + } + + public void setUpdateTime(Date updateTime) { + this.updateTime = updateTime; + } +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/MyStudentService.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/MyStudentService.java new file mode 100644 index 0000000000000000000000000000000000000000..dcb03321ac6b7ac2f953ea5dd9665c4073a179b7 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/MyStudentService.java @@ -0,0 +1,11 @@ +package io.mogdb.springmvc.mybatis.service; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import org.springframework.stereotype.Component; + +@Component +public interface MyStudentService { + String testTs(StudentEntity studentEntity); + + String updateV2(StudentEntity studentEntity); +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/OmOfferingInstService.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/OmOfferingInstService.java new file mode 100644 index 0000000000000000000000000000000000000000..834937d5898d09c37937d679a606ead0d4cafc8d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/OmOfferingInstService.java @@ -0,0 +1,17 @@ +package io.mogdb.springmvc.mybatis.service; + +import org.springframework.stereotype.Component; + +import java.sql.SQLException; +import java.util.Map; + +@Component +public interface OmOfferingInstService { + int batchInsertInst(Map data); + + int batchInsertInstV2(Map data); + + int batchInsertInstV3(Map data); + + String testCopyTable() throws ClassNotFoundException, SQLException; +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/StudentService.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/StudentService.java new file mode 100644 index 0000000000000000000000000000000000000000..03c9af9df858ae4a3cdd48672000dabeb9d6dbf2 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/StudentService.java @@ -0,0 +1,30 @@ +package io.mogdb.springmvc.mybatis.service; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public interface StudentService { + Integer insertStudent(StudentEntity studentEntity); + + Integer updateStudent(StudentEntity studentEntity); + + Integer deleteStudent(int id); + + List findAllStudent(); + + PageInfo findPageStudent(); + + StudentEntity findStudentById(int id); + + String testV3(StudentEntity studentEntity); + + String testV5(StudentEntity studentEntity); + + String insertStudentV2(StudentEntity studentEntity); + + int addTestBatchRecord(); +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/MyStudentServiceImpl.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/MyStudentServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..18b9917f07234a14603659d9eece6ef9906b7ae9 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/MyStudentServiceImpl.java @@ -0,0 +1,29 @@ +package io.mogdb.springmvc.mybatis.service.impl; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import io.mogdb.springmvc.mybatis.service.MyStudentService; +import io.mogdb.springmvc.mybatis.service.StudentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class MyStudentServiceImpl implements MyStudentService { + @Autowired + private StudentService studentService; + + @Override + public String testTs(StudentEntity studentEntity) { + studentEntity = studentService.findAllStudent().stream().findFirst().get(); + studentService.updateStudent(studentEntity); + studentService.deleteStudent(studentEntity.getId()); + return "success"; + } + + @Override + public String updateV2(StudentEntity studentEntity) { + studentEntity = studentService.findAllStudent().stream().findFirst().get(); + studentService.updateStudent(studentEntity); + studentService.deleteStudent(studentEntity.getId()); + return "success"; + } +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/OmOfferingInstServiceImpl.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/OmOfferingInstServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..f33bcc1906aa4d369e4df181ce45243288a327c5 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/OmOfferingInstServiceImpl.java @@ -0,0 +1,182 @@ +package io.mogdb.springmvc.mybatis.service.impl; + +import io.mogdb.springmvc.mybatis.dao.OmOfferingInstDAO; +import io.mogdb.springmvc.mybatis.dao.StudentDAO; +import io.mogdb.springmvc.mybatis.service.OmOfferingInstService; +import org.postgresql.copy.CopyManager; +import org.postgresql.core.BaseConnection; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.*; +import java.util.stream.IntStream; + +@Service +public class OmOfferingInstServiceImpl implements OmOfferingInstService { + @Autowired + private OmOfferingInstDAO omOfferingInstDAO; + + @Autowired + private StudentDAO studentDAO; + + @Override + public int batchInsertInst(Map data) { + List> recordList = new ArrayList<>(); + int n = 32767; + // n = 65535; + n = n - 1; + n = 32498; + IntStream.range(0, n).forEach(i -> recordList.add(new HashMap<>())); + data = new HashMap<>(); + data.put("recordList", recordList); + omOfferingInstDAO.executeCMD(); + omOfferingInstDAO.batchInsert(data); + return 0; + } + + @Override + public int batchInsertInstV2(Map data) { + List> recordList = new ArrayList<>(); + int n = 32767; + // n = 65535; + // n = 32498; + n = 65535 / 3; + IntStream.range(0, n).forEach(i -> { + Map record = new HashMap<>(); + record.put("name", "zp01"); + record.put("classId", 1); + record.put("status", 1); + recordList.add(record); + }); + data = new HashMap<>(); + data.put("recordList", recordList); + studentDAO.executeCMD(); + studentDAO.batchInsert(data); + return 0; + } + + @Override + public int batchInsertInstV3(Map data) { + List> recordList = new ArrayList<>(); + int n = 32767; + // n = 65535; + // n = 32498; + n = 32499; + n = 32767 / 3 + 1; + IntStream.range(0, n).forEach(i -> { + Map record = new HashMap<>(); + record.put("name", "zp01"); + record.put("classId", 1); + record.put("status", 1); + recordList.add(record); + }); + data = new HashMap<>(); + data.put("recordList", recordList); + studentDAO.executeCMD(); + studentDAO.batchInsertV2(data); + return 0; + } + + @Override + public String testCopyTable() throws ClassNotFoundException, SQLException { + Class.forName("org.postgresql.Driver"); + String url = "jdbc:postgresql://120.26.81.56:34800/db2?currentSchema=public&batchMode=off&logger=Slf4JLogger&loggerLevel=TRACE"; + String username = "ycy"; + String password = "ycy.12345678"; + Connection _conn = DriverManager.getConnection(url, username, password); + return testCopy(_conn); + } + + public String testCopy(Connection _conn) { + StringBuilder result = new StringBuilder(); + String dir = "/data/ycy/devel/"; + // 将SELECT * FROM t_perf 查询结果导出到本地文件d:/data.txt + try { + long beginTms1 = Calendar.getInstance().getTimeInMillis(); + copyToFile(_conn, dir + "data.txt", "(SELECT * FROM t_perf)"); + long endTms1 = Calendar.getInstance().getTimeInMillis(); + StringBuilder sb = new StringBuilder(); + sb.append("### Step 1 : 将SELECT * FROM t_perf 查询结果导出到本地文件d:/data.txt ###,耗时(ms):").append(endTms1 - beginTms1); + result.append(sb.toString()); + result.append("\n"); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + //将d:/data.txt中的数据导入到 t_perf_1 中。 + String tablename1 = "t_perf_1"; + try { + long beginTms1 = Calendar.getInstance().getTimeInMillis(); + copyFromFile(_conn, dir + "data.txt", tablename1); + long endTms1 = Calendar.getInstance().getTimeInMillis(); + StringBuilder sb = new StringBuilder(); + sb.append("### Step 2 : 将d:/data.txt中的数据导入到 t_perf_1 中 ###,耗时(ms):").append(endTms1 - beginTms1); + result.append(sb.toString()); + result.append("\n"); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 将 t_perf_1 中的数据导出到本地文件 d:/data1.txt + try { + long beginTms1 = Calendar.getInstance().getTimeInMillis(); + copyToFile(_conn, dir + "data1.txt", tablename1); + long endTms1 = Calendar.getInstance().getTimeInMillis(); + StringBuilder sb = new StringBuilder(); + sb.append("### Step 1 : 将 t_perf_1 中的数据导出到本地文件 d:/data1.txt ###,耗时(ms):").append(endTms1 - beginTms1); + result.append("\n"); + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return result.toString(); + } + + // 使用copyIn把数据从文件中导入数据库, + public static void copyFromFile(Connection connection, String filePath, String tableName) + throws SQLException, IOException { + FileInputStream fileInputStream = null; + try { + CopyManager copyManager = new CopyManager((BaseConnection) connection); + fileInputStream = new FileInputStream(filePath); + copyManager.copyIn("COPY " + tableName + " FROM STDIN", fileInputStream); + } finally { + if (fileInputStream != null) { + try { + fileInputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + // 使用copyOut把数据从数据库中导出到文件中 + public static void copyToFile(Connection connection, String filePath, String tableOrQuery) + throws SQLException, IOException { + FileOutputStream fileOutputStream = null; + try { + CopyManager copyManager = new CopyManager((BaseConnection) connection); + fileOutputStream = new FileOutputStream(filePath); + copyManager.copyOut("COPY " + tableOrQuery + " TO STDOUT", fileOutputStream); + } finally { + if (fileOutputStream != null) { + try { + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/StudentServiceImpl.java b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/StudentServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..0c4661d0a08d77f98c5a904628fec64f44ddc37d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/java/io/mogdb/springmvc/mybatis/service/impl/StudentServiceImpl.java @@ -0,0 +1,136 @@ +package io.mogdb.springmvc.mybatis.service.impl; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import io.mogdb.springmvc.mybatis.dao.StudentDAO; +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import io.mogdb.springmvc.mybatis.service.StudentService; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@Service +public class StudentServiceImpl implements StudentService { + private final static Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class); + + @Autowired + private StudentDAO studentDao; + + @Autowired + private SqlSessionFactory sqlSessionFactory; + + @Override + public int addTestBatchRecord() { + int col1 = 100; + String col2 = "100col2"; + Map data = new HashMap<>(); + data.put("col1", col1); + data.put("col2", col2); +// return studentDao.addTestRBatchRecord(col1, col2); +// int res = studentDao.addTestRBatchRecordV2(data); +// return res; + return addTestBatch(); + } + + public int addTestBatch() { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); //批处理模式 + StudentDAO mapper = sqlSession.getMapper(StudentDAO.class); + int num = 2; + for (int i = 0; i < num; i++) { + int col1 = 100; + String col2 = "100col2"; + Map data = new HashMap<>(); + data.put("col1", col1); + data.put("col2", col2); + mapper.addTestRBatchRecordV2(data); + } + /** + * Batch模式下 flushStatements方法 实际就是执行 BatchExecutor.doFlushStatements方法 + * 该方法会刷新statement + */ + sqlSession.flushStatements(); + return 1; + } + + @Override + public String testV3(StudentEntity studentEntity) { + StudentEntity stu1 = findAllStudent().stream().findFirst().get(); + updateStudent(stu1); + deleteStudent(stu1.getId()); + return ""; + } + + @Override + @Transactional + public String testV5(StudentEntity studentEntity) { + StudentEntity stu1 = studentDao.findAllValidStudent().stream().findFirst().get(); + studentDao.updateStudent(stu1); + studentDao.deleteStudent(stu1.getId()); + return ""; + } + + @Override + public String insertStudentV2(StudentEntity studentEntity) { + int classId = (int) (Math.random() * 100); + studentEntity = new StudentEntity(); + studentEntity.setName("zp" + classId); + studentEntity.setClassId(classId); + studentEntity.setStatus(0); + Integer result = studentDao.insertStudent(studentEntity); + throw new RuntimeException(); + } + + @Override + public Integer insertStudent(StudentEntity studentEntity) { + Integer result = studentDao.insertStudent(studentEntity); + logger.info("insertStudent result={}", result); + return result; + } + + @Override + public Integer updateStudent(StudentEntity studentEntity) { + Integer result = studentDao.updateStudent(studentEntity); + logger.info("updateStudent result={}", result); + return result; + } + + @Override + public Integer deleteStudent(int id) { + Integer result = studentDao.deleteStudent(id); + logger.info("deleteStudent result={}", result); + return result; + } + + @Override + public List findAllStudent() { + List entities = studentDao.findAllValidStudent(); + logger.info("findAllStudent result={}", (entities != null ? entities.size() : 0)); + return entities; + } + + @Override + public PageInfo findPageStudent() { + PageHelper.startPage(0, 0); + List entities = studentDao.findAllValidStudent(); + logger.info("findAllStudent result={}", (entities != null ? entities.size() : 0)); + PageInfo info = new PageInfo<>(entities); + return info; + } + + @Override + public StudentEntity findStudentById(int id) { + StudentEntity studentEntity = studentDao.findStudentById(id); + logger.info("findStudentById result={}", studentEntity); + return studentEntity; + } +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/jdbc.properties b/mogdb-springmvc-mybatis-demo/src/main/resources/jdbc.properties new file mode 100644 index 0000000000000000000000000000000000000000..d6b65128531e9a9b1ebdc62c64ef0d844142c4a0 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/jdbc.properties @@ -0,0 +1,7 @@ +#dbc.driver=org.opengauss.Driver +#jdbc.url=jdbc:opengauss://121.36.15.2:55448/postgres?currentSchema=sqm3&batchMode=off&logger=Slf4JLogger&loggerLevel=TRACE + +jdbc.driver=org.postgresql.Driver +jdbc.url=jdbc:postgresql://121.36.15.2:55448/t1?currentSchema=zp_test&batchMode=on&logger=Slf4JLogger&loggerLevel=TRACE +jdbc.username=xxxx +jdbc.password=xxxx diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/log4j2.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/log4j2.xml new file mode 100644 index 0000000000000000000000000000000000000000..05099aa1c5f0e9fef0f81c0c5ed33767021f86f5 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/log4j2.xml @@ -0,0 +1,111 @@ + + + + ${sys:catalina.home}/logs/mout/ + stat + global + ERROR + ERROR + + + + + + + + + + + + + + + [MOGDB] %d{yyyy-MM-dd HH:mm:ss SSS} [%-5p] %t(%c:%L) - %m%n + + + + + + + + + + + + + + + + + + + + [mout] %d{yyyy-MM-dd HH:mm:ss SSS} [%-5p] %t(%c:%L) - %m%n + + + + + + + + + + + + + + + + + + + + [MOGDB] %d{yyyy-MM-dd HH:mm:ss SSS} [%-5p] %t(%c:%L) - %m%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/StudentMapper.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/StudentMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..381b92f90512ab9085b5482b2d0304b69ce40de0 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/StudentMapper.xml @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + select Id, Name, ClassId, Status, AddTime, UpdateTime from RUN_Student + + + + insert into RUN_Student + ( + Name, + ClassId, + Status + ) + values + ( + #{studentEntity.name}, + #{studentEntity.classId}, + 1 + ) + + + + insert into RUN_Student + ( + Name, + ClassId, + Status + ) + + select #{item.name}, #{item.classId}, #{item.status} + + + + + + insert into test_batch (col1, col2) values (#{col1},#{col2}) + + + + insert into test_batch (col1, col2) values (#{col1},#{col2}) + + + + insert into RUN_Student + ( + Name, + ClassId, + Status + ) values + + (#{item.name}, #{item.classId}, #{item.status}) + + + + + SET max_stack_depth TO 204800 + + + + update RUN_Student set + Name = #{studentEntity.name}, + ClassId = #{studentEntity.classId}, + Status = 1 + where id = #{studentEntity.id} + + + + delete from RUN_Student where id = #{id} + + + + + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/omOfferingInstMapper.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/omOfferingInstMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..bed7ef0cde50c8a5029c7b5d5021a2dded455e6f --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/mappers/omOfferingInstMapper.xml @@ -0,0 +1,29 @@ + + + + + + select * from om_offering_inst; + + + + insert into om_offering_inst ( business_seq, prod_id, be_id, offering_inst_id, offering_id, offering_name, + offering_code, cust_type, cust_id, brand, primary_flag, rel_pri_offering_inst_id, bundle_flag, + p_offering_inst_id, offering_type, offering_sub_type, apply_obj_type, apply_obj_id, status, status_date, + eff_date, exp_date, entity_type, entity_id, entity_name, entity_code, create_date, modify_date, apply_obj_be_id, + custid_index, oper_code, cust_code, subs_id, group_id, owner_party_role_type, owner_party_role_id, ex_field1, + ex_field2, ex_field3, ex_field4, ex_field5, msisdn, cmiot_modify_time ) + + select '500024201070', null, '351', '350005382359858', 11000001, '物联卡个人', 'IO111000000001', 'C', + 311000007486002, '7', 'Y', null, 'N', null, null, null, 'S', '350000487203389', '2', to_date('2023-4-13 + 21:03:42','yyyy-mm-dd hh24:mi:ss'), to_date('2023-4-13 21:03:00','yyyy-mm-dd hh24:mi:ss'), + to_date('2099-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss'), '01', 350000487203389, null, null, sysdate, sysdate, + '', 2, '01', null, '350000487203389', null, 'C', '311000007486002', null, null, null, null, null, null, + sysdate + + + + + SET max_stack_depth TO 204800 + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/mybatis.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/mybatis.xml new file mode 100644 index 0000000000000000000000000000000000000000..6f82e3f525f97e53a9666ccca560e43cb72f7f1d --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/mybatis.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/spring.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/spring.xml new file mode 100644 index 0000000000000000000000000000000000000000..50eb7d018d26b376c6d7a5b9f557d34d4cef6552 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/spring.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/springmvc.xml b/mogdb-springmvc-mybatis-demo/src/main/resources/springmvc.xml new file mode 100644 index 0000000000000000000000000000000000000000..35d24d0c516eda2475125d695551e59eea128fd2 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/springmvc.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + application/json;charset=UTF-8 + text/html;charset=UTF-8 + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/resources/sql/student.sql b/mogdb-springmvc-mybatis-demo/src/main/resources/sql/student.sql new file mode 100644 index 0000000000000000000000000000000000000000..550212aa1c948c06187c6755a95da32c97ea5fb3 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/resources/sql/student.sql @@ -0,0 +1,41 @@ +drop table if exists RUN_Student; + +CREATE TABLE RUN_Student ( + id bigserial NOT NULL, + addTime timestamp default current_timestamp, + updateTime timestamp default current_timestamp, + Name varchar(192) NULL, + ClassId int4 NULL, + Status int4 null, + CONSTRAINT RUN_Student_pk PRIMARY KEY (id) +); + +insert into RUN_Student(Name, ClassId, Status) +values('zp001', 1, 1); + + +insert into RUN_Student(Name, ClassId, Status) +values('zp002', 2, 1); + + +insert into RUN_Student(Name, ClassId, Status) +values('zp003', 3, 1); + + +insert into RUN_Student(Name, ClassId, Status) +values('zp004', 4, 1); + + +insert into RUN_Student(Name, ClassId, Status) +values('zp005', 5, 0); + + +insert into RUN_Student(Name, ClassId, Status) +values('zp006', 6, 0); + +commit; + + +tomcat8:run -Dlog4j.skipJansi=true + + diff --git a/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/index.jsp b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..7dd92f5b973a049136af84195b8b416b1a5b6274 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/index.jsp @@ -0,0 +1,5 @@ + + +

mogdb springmvn mybatis demo

+ + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/index.jsp b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 0000000000000000000000000000000000000000..7dd92f5b973a049136af84195b8b416b1a5b6274 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,5 @@ + + +

mogdb springmvn mybatis demo

+ + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/student.jsp b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/student.jsp new file mode 100644 index 0000000000000000000000000000000000000000..e1ed4f41c8edbb8bb3bd210bb525bcc44e8edae7 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/student.jsp @@ -0,0 +1,19 @@ +<%-- + Created by IntelliJ IDEA. + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page isELIgnored="false" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + Title + + + + + ${student.id}--${student.name}--${student.classId}--${student.status}
+
+ + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/test.jsp b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/test.jsp new file mode 100644 index 0000000000000000000000000000000000000000..aea49febabd745d6524e68ea8c08936aea16ef2f --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/views/test.jsp @@ -0,0 +1,8 @@ + + + mogdb springmvn mybatis demo + + +

${message}

+ + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/web.xml b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..e8ece14cccf880b644fbc68743a8f82d9e25dd9e --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,106 @@ + + + + Archetype Created Web Application + + + + contextConfigLocation + classpath:spring.xml + + + + log4jConfigLocation + classpath:log4j2.xml + + + + log4jConfiguration + classpath:log4j2.xml + + + log4jRefreshInterval + 60000 + + + org.apache.logging.log4j.web.Log4jServletContextListener + + + + + org.springframework.web.util.IntrospectorCleanupListener + + + + + + org.springframework.web.context.ContextLoaderListener + + + + + MVC-DispatcherServlet + org.springframework.web.servlet.DispatcherServlet + + + contextConfigLocation + classpath:springmvc.xml + + + 1 + + + MVC-DispatcherServlet + + / + + + + + encodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + encodingFilter + /* + + + + index.jsp + + + + + default + *.js + + + + default + *.css + + + + default + *.jpg + + + + default + *.png + + \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/BaseTest.java b/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/BaseTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0790650c3d792e7ef0fcd768051a14f935d158b5 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/BaseTest.java @@ -0,0 +1,11 @@ +package io.mogdb.springmvc.mybatis.test; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:spring.xml"}) +public abstract class BaseTest { +} \ No newline at end of file diff --git a/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/StudentControllerTest.java b/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/StudentControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7756a7812c37a6026cc03cdf40eda93442039c40 --- /dev/null +++ b/mogdb-springmvc-mybatis-demo/src/test/java/io/mogdb/springmvc/mybatis/test/StudentControllerTest.java @@ -0,0 +1,60 @@ +package io.mogdb.springmvc.mybatis.test; + +import io.mogdb.springmvc.mybatis.entity.StudentEntity; +import io.mogdb.springmvc.mybatis.service.StudentService; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +public class StudentControllerTest extends BaseTest { + @Autowired + private StudentService studentService; + + @Test + public void findAllStudentTest() { + List entities = studentService.findAllStudent(); + System.out.println(entities); + } + + @Test + public void findStudentByIdTest() { + int id = 1; + StudentEntity entity = studentService.findStudentById(id); + System.out.println(entity); + } + + @Test + public void insertStudentTest() { + int count = 0; + for (int i = 1; i < 3; i++) { + StudentEntity entity = new StudentEntity(); + entity.setName("周星驰" + i); + entity.setClassId(i); + count = count + studentService.insertStudent(entity); + } + System.out.println(count); + } + + @Test + public void deleteStudentTest() { + int count = 0; + for (int i = 7; i < 13; i++) { + count = count + studentService.deleteStudent(i); + } + System.out.println(count); + } + + @Test + public void updateStudentTest() { + int count = 0; + for (int i = 7; i < 13; i++) { + StudentEntity entity = new StudentEntity(); + entity.setId(i); + entity.setClassId(i - 6); + entity.setName("周星驰" + "-" + "周润发" + (i - 8)); + count = count + studentService.updateStudent(entity); + } + System.out.println(count); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8e092bfc7e33f0ed6987f320358592d5c850375a..3666032dfc291f0b75b7761a6ebee0ee78bab1e9 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,7 @@ mogdb-springboot-jpa-demo mogdb-springboot-demo mogdb-quartz-demo + mogdb-springmvc-mybatis-demo io.mogdb