事务持久化调整

yun-zuoyi
alwaysfrin 6 years ago
parent c33076e0a1
commit a6301d2d8c

@ -0,0 +1,19 @@
package cn.estsh.i3plus.core.api.iservice.busi;
import io.swagger.annotations.ApiOperation;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:16
* @Modify:
**/
public interface ITestTransService {
@ApiOperation(value = "两类dao混用测试entityManager")
void testListTran(int type,String name);
@ApiOperation(value = "自定义dao混用")
void testListTranSec(int type,String name);
}

@ -0,0 +1,52 @@
package cn.estsh.i3plus.core.apiservice.controller;
import cn.estsh.i3plus.core.api.iservice.busi.ITestTransService;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.fastdfs.FastDFSClient;
import cn.estsh.impp.framework.boot.fastdfs.FastDFSFile;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
/**
* @Description : entityManager
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:20
* @Modify:
**/
@RestController
@Api(description="对事务以及entityManager的操作演示")
@RequestMapping(PlatformConstWords.BASE_URL + "/demo-trans")
public class DemoTransactionController extends CoreBaseController{
private static final Logger LOGGER = LoggerFactory.getLogger(DemoTransactionController.class);
@Autowired
private ITestTransService transService;
@PostMapping("/test1")
@ApiOperation(value = "dao混合调用")
public ResultBean transTest1(int type,String name) {
transService.testListTran(type,name);
return ResultBean.success("测试成功");
}
@PostMapping("/test2")
@ApiOperation(value = "dao混合调用2")
public ResultBean transTest2(int type,String name) {
transService.testListTranSec(type,name);
return ResultBean.success("测试成功");
}
}

@ -0,0 +1,25 @@
package cn.estsh.i3plus.core.apiservice.dao;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.i3plus.pojo.platform.bean.SysUserInfo;
import java.util.List;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:19
* @Modify:
**/
public interface ITestTransDao {
List listUser(String name);
SysUserInfo getUser(String name);
SysTool getSysTool(String name);
List listSysTool(String name);
}

@ -0,0 +1,26 @@
package cn.estsh.i3plus.core.apiservice.dao;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.i3plus.pojo.platform.bean.SysUserInfo;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:20
* @Modify:
**/
@Repository
public interface ITransDaoSec extends CrudRepository<SysTool,Long> {
List<SysTool> findByName(String name);
//按位查询
@Query("select u from SysTool u where name = :uname")
List<SysTool> findSysToolByName(String uname);
}

@ -0,0 +1,114 @@
package cn.estsh.i3plus.core.apiservice.daoimpl;
import cn.estsh.i3plus.core.apiservice.dao.ITestTransDao;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.i3plus.pojo.platform.bean.SysUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Metamodel;
import java.util.List;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:31
* @Modify:
**/
@Service
public class TestTransDaoImpl implements ITestTransDao {
@Autowired
private EntityManager entityManager;
@Override
public List listUser(String name) {
System.out.println("======listUser=====");
Metamodel mm = entityManager.getMetamodel();
for(EntityType<?> s : mm.getEntities()){
System.out.println("entityType : " + s.getName());
}
for(ManagedType<?> s : mm.getEntities()){
System.out.println("ManagedType : " + s.getPersistenceType().name());
}
System.out.println(entityManager.getDelegate() + "====" + entityManager);
System.out.println("======listUser end=====");
String hql = "select m from SysUserInfo m where m.name = :name";
Query query = entityManager.createQuery(hql,SysUserInfo.class);
query.setParameter("name", name);
return query.getResultList();
}
@Override
public SysUserInfo getUser(String name) {
System.out.println("======getUser=====");
Metamodel mm = entityManager.getMetamodel();
for(EntityType<?> s : mm.getEntities()){
System.out.println("entityType : " + s.getName());
}
for(ManagedType<?> s : mm.getEntities()){
System.out.println("ManagedType : " + s.getPersistenceType().name());
}
System.out.println(entityManager.getDelegate() + "====" + entityManager);
System.out.println("======getUser end=====");
String hql = "select m from SysUserInfo m where m.name = :name";
Query query = entityManager.createQuery(hql,SysUserInfo.class);
query.setParameter("name", name);
return (SysUserInfo) query.getSingleResult();
}
@Override
public List listSysTool(String name) {
System.out.println("======listSysTool=====");
Metamodel mm = entityManager.getMetamodel();
for(EntityType<?> s : mm.getEntities()){
System.out.println("entityType : " + s.getName());
}
for(ManagedType<?> s : mm.getEntities()){
System.out.println("ManagedType : " + s.getPersistenceType().name());
}
System.out.println(entityManager.getDelegate() + "====" + entityManager);
System.out.println("======listSysTool end=====");
String hql = "select m from SysTool m where m.name = :name";
Query query = entityManager.createQuery(hql,SysTool.class);
query.setParameter("name", name);
return query.getResultList();
}
@Override
public SysTool getSysTool(String name) {
System.out.println("======getSysTool=====");
Metamodel mm = entityManager.getMetamodel();
for(EntityType<?> s : mm.getEntities()){
System.out.println("entityType : " + s.getName());
}
for(ManagedType<?> s : mm.getEntities()){
System.out.println("ManagedType : " + s.getPersistenceType().name());
}
System.out.println(entityManager.getDelegate() + "====" + entityManager);
System.out.println("======getSysTool end=====");
String hql = "select m from SysTool m where m.name = :name";
Query query = entityManager.createQuery(hql,SysTool.class);
query.setParameter("name", name);
return (SysTool) query.getSingleResult();
}
}

@ -0,0 +1,137 @@
package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
import cn.estsh.i3plus.core.api.iservice.busi.ITestTransService;
import cn.estsh.i3plus.core.apiservice.dao.ITestTransDao;
import cn.estsh.i3plus.core.apiservice.dao.ITransDaoSec;
import cn.estsh.i3plus.pojo.platform.bean.SysDepartment;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.i3plus.pojo.platform.bean.SysUserInfo;
import cn.estsh.i3plus.pojo.platform.repository.SysDepartmentRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysUserInfoRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysUserRepository;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Description :
* @Reference :
* @Author : alwaysfrin
* @CreateDate : 2019-04-29 17:18
* @Modify:
**/
@Service
public class TestTransService implements ITestTransService {
@Autowired
private SysDepartmentRepository sysDepartmentRDao;
@Autowired
private SysUserInfoRepository sysUserInfoRDao;
@Autowired
private SysUserRepository sysUerRDao;
@Autowired
private ITestTransDao testTransDao;
@Autowired
private ITransDaoSec testTransSecDao;
@Override
public void testListTran(int type,String name) {
if(type == 1) {
List<SysUserInfo> userList = testTransDao.listUser(name);
System.out.println("===userList===" + userList.size());
System.out.println("方式一RDAO + dao");
List<SysDepartment> depList = sysDepartmentRDao.findByProperty("name", name);
System.out.println("===depList===" + depList.size());
}else if(type == 2) {
System.out.println("方式一2RDAO + dao");
List<SysDepartment> depList = sysDepartmentRDao.findByProperty("name", name);
System.out.println("===depList===" + depList.size());
List<SysTool> toolList = testTransSecDao.findByName(name);
System.out.println("===toolList===" + toolList.size());
}else if(type == 3){
System.out.println("方式三RDAO + idao-query");
List<SysDepartment> depList = sysDepartmentRDao.findByProperty("name",name);
System.out.println("===depList===" + depList.size());
List<SysTool> toolList = testTransSecDao.findByName(name);
System.out.println("===toolList===" + toolList.size());
}else if(type == 4){
System.out.println("方式四dao + idao");
List<SysDepartment> depList = sysDepartmentRDao.findByProperty("name",name);
System.out.println("===depList===" + depList.size());
List<SysTool> toolList = testTransSecDao.findByName(name);
System.out.println("===toolList===" + toolList.size());
}else if(type == 5){
System.out.println("方式五dao + idao-query");
List<SysDepartment> depList = sysDepartmentRDao.findByProperty("name",name);
System.out.println("===depList===" + depList.size());
List<SysTool> toolList = testTransSecDao.findByName(name);
System.out.println("===toolList===" + toolList.size());
}else if(type == 6) {
System.out.println("方式六:主键重复");
SysDepartment dep = sysDepartmentRDao.getByProperty("name", name);
System.out.println(dep.getId() + "===dep===" + dep);
SysUserInfo userinfo = testTransDao.getUser(name);
System.out.println(userinfo.getId() + "===userList===" + userinfo);
}else if(type == 7) {
SysUserInfo userinfo = testTransDao.getUser(name);
System.out.println(userinfo.getId() + "===userList===" + userinfo);
SysTool sysTool = testTransDao.getSysTool(name);
System.out.println(sysTool.getId() + "===sysTool===" + sysTool);
}else if(type == 8) {
SysUserInfo userinfo = testTransDao.getUser(name);
System.out.println(userinfo.getId() + "===userinfo===" + userinfo);
List userinfoList = testTransDao.listUser(name);
System.out.println("===userList===" + userinfoList);
SysTool sysTool = testTransDao.getSysTool(name);
System.out.println(sysTool.getId() + "===sysTool===" + sysTool);
List sysToolList = testTransDao.listSysTool(name);
System.out.println("===sysToolList===" + sysToolList);
}else if(type == 9) {
SysUserInfo userinfo = sysUserInfoRDao.getByProperty("name",name);
System.out.println(userinfo.getId() + "===userinfo===" + userinfo);
SysTool sysTool = testTransDao.getSysTool(name);
System.out.println(sysTool.getId() + "===sysTool===" + sysTool);
SysDepartment sysDepartment = sysDepartmentRDao.getByProperty("name",name);
System.out.println(sysDepartment.getId() + "===sysDepartment===" + sysDepartment);
SysUser user = sysUerRDao.getByProperty("userName",name);
System.out.println(user.getId() + "===userName===" + user);
List userinfoList = testTransDao.listUser(name);
System.out.println("===userList===" + userinfoList);
List sysToolList = testTransDao.listSysTool(name);
System.out.println("===sysToolList===" + sysToolList);
} else if(type == 10) {
SysUserInfo userinfo = sysUserInfoRDao.getByProperty("name",name);
System.out.println(userinfo.getId() + "===userinfo===" + userinfo);
SysDepartment sysDepartment = sysDepartmentRDao.getByProperty("name",name);
System.out.println(sysDepartment.getId() + "===sysDepartment===" + sysDepartment);
SysUser user = sysUerRDao.getByProperty("userName",name);
System.out.println(user.getId() + "===userName===" + user);
}
}
@Override
public void testListTranSec(int type,String name) {
}
}

@ -56,6 +56,74 @@ public class TestUser extends TestBase{
String TEXT = "飓风黑兹尔是1954年大西洋飓风季期间造成死亡人数最多、经济损失最惨重的飓风。这场风暴夺走了海地多达1000人的生命然后又以四级飓风强度从南、北卡罗莱纳州边境附近袭击美国造成95人丧生。之后黑兹尔转变成温带风暴但仍然致使81人遇难其中大部分来自多伦多。由于风暴造成了重大人员伤亡和财产损失其名称“黑兹尔”因此从大西洋风暴命名名单上退役。黑兹尔摧毁了海地40%的咖啡树和50%的可可作物对当地经济的影响持续了数年之久。飓风在南、北卡罗莱纳州登陆冲击点附近的大部分海滨住宅被毁。气旋从北卡罗莱纳州沿大西洋海岸北上先后影响了弗吉尼亚州、西弗吉尼亚州、马里兰州、特拉华州、新泽西州、宾夕法尼亚州和纽约州产生的阵风时速有近160公里造成的经济损失高达3.08亿美元。多伦多市内及周边有许多溪流溢出导致严重的洪涝灾害。加拿大遭受的损失达到1.35亿加拿大元。由于缺少应对热带气旋的经验,加上风暴强度的保持时间异常之长,多伦多所受影响的严重程度是前所未有的";
@Autowired
private ITestTransService testTransService;
@Test
public void testTrans(){
try {
testTransService.testListTran(1, "aaa");
}catch(Exception e){
System.out.println("111============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(2, "aaa");
}catch(Exception e){
System.out.println("222============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(3, "aaa");
}catch(Exception e){
System.out.println("333============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(4, "aaa");
}catch(Exception e){
System.out.println("444============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(5, "aaa");
}catch(Exception e){
System.out.println("555============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(6, "aaa");
}catch(Exception e){
System.out.println("666============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(7, "aaa");
}catch(Exception e){
System.out.println("777============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(8, "aaa");
}catch(Exception e){
System.out.println("888============事务出错:" + e.getMessage());
e.printStackTrace();
}
try {
testTransService.testListTran(9, "aaa");
}catch(Exception e){
System.out.println("999============事务出错:" + e.getMessage());
e.printStackTrace();
}
}
@Test
public void testSysPosition() throws Exception{
List<SysPosition> dataBase = new ArrayList<>();

Loading…
Cancel
Save