|
| 1 | +package com.codingapi.springboot.fast.generator; |
| 2 | + |
| 3 | +import com.codingapi.springboot.fast.jdbc.JdbcQuery; |
| 4 | +import com.codingapi.springboot.fast.jdbc.JdbcQueryContext; |
| 5 | +import com.codingapi.springboot.fast.jpa.JPAQuery; |
| 6 | +import com.codingapi.springboot.fast.jpa.JpaQueryContext; |
| 7 | +import com.codingapi.springboot.fast.metadata.TableEntityMetadata; |
| 8 | +import jakarta.persistence.EntityManagerFactory; |
| 9 | +import jakarta.persistence.GenerationType; |
| 10 | +import org.hibernate.cfg.AvailableSettings; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.core.env.Environment; |
| 15 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +@SpringBootTest |
| 24 | +public class DynamicTableGeneratorQueryTest { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private EntityManagerFactory entityManagerFactory; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private Environment environment; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private JdbcTemplate jdbcTemplate; |
| 34 | + |
| 35 | + |
| 36 | + @Test |
| 37 | + void generateThenUpdateQuery() throws ClassNotFoundException { |
| 38 | + String dialect = entityManagerFactory.getProperties().get(AvailableSettings.DIALECT).toString(); |
| 39 | + String jdbcUrl = environment.getProperty("spring.datasource.url"); |
| 40 | + String username = environment.getProperty("spring.datasource.username"); |
| 41 | + String password = environment.getProperty("spring.datasource.password"); |
| 42 | + |
| 43 | + DynamicTableGenerator dynamicTableGenerator = new DynamicTableGenerator(Class.forName(dialect), jdbcUrl, username, password); |
| 44 | + |
| 45 | + TableEntityMetadata tableEntityMetadata = new TableEntityMetadata("com.codingapi.springboot.fast.entity.Test"); |
| 46 | + tableEntityMetadata.setTable("test"); |
| 47 | + tableEntityMetadata.addPrimaryKeyColumn(Long.class, "id", GenerationType.IDENTITY, "主键"); |
| 48 | + tableEntityMetadata.addColumn(String.class, "name", "姓名"); |
| 49 | + |
| 50 | + Class<?> entityClass = tableEntityMetadata.buildClass(); |
| 51 | + |
| 52 | + // 创建表 |
| 53 | + dynamicTableGenerator.generateMigratorTableDDL(entityClass, true); |
| 54 | + |
| 55 | + // 通过DynamicTableGenerator 动态创建的entity无法通过jpa的 EntityManager进行数据查询处理, 因为Entity不在JPA扫描支持的范围内。 |
| 56 | + JPAQuery jpaQuery = JpaQueryContext.getInstance().getJPAQuery(); |
| 57 | + assertThrows(Exception.class,()->{ |
| 58 | + jpaQuery.listQuery(entityClass,"select t from com.codingapi.springboot.fast.entity.Test"); |
| 59 | + }); |
| 60 | + |
| 61 | + // 插入数据 |
| 62 | + jdbcTemplate.update("insert into test(name) values(?)", "小明"); |
| 63 | + |
| 64 | + // 查询数据 |
| 65 | + JdbcQuery jdbcQuery = JdbcQueryContext.getInstance().getJdbcQuery(); |
| 66 | + List<Map<String, Object>> list = jdbcQuery.queryForMapList("select * from test"); |
| 67 | + System.out.println(list); |
| 68 | + assertFalse(list.isEmpty()); |
| 69 | + |
| 70 | + } |
| 71 | +} |
0 commit comments