Search in sources :

Example 1 with Course

use of com.sharding.jdbc.entity.Course in project note by kebukeYi.

the class ShardingJdbcApplicationTests method addCourse.

@Test
public void addCourse() {
    // 01 配置文件
    for (int i = 0; i < 10; i++) {
        Course c = new Course();
        // c.setCid(Long.valueOf(i));
        c.setCname("shardingsphere");
        c.setUserId(Long.valueOf("" + (1000 + i)));
        c.setCstatus("1");
        courseMapper.insert(c);
    // 未有主键时
    // Logic SQL: INSERT INTO course  ( cname, user_id,cstatus )  VALUES  ( ?,?,? )
    // Actual SQL: m1 ::: INSERT INTO course_1  ( cname,user_id, cstatus , cid)  VALUES  (?, ?, ?, ?)
    // 手动设置主键时
    // Logic SQL: INSERT INTO course  ( cname, user_id,cstatus )  VALUES  ( ?,?,?,?)
    // Actual SQL: m1 ::: INSERT INTO course_1  ( cname,user_id, cstatus , cid)  VALUES  (?, ?, ?, ?) ::: [0, shardingsphere, 1000, 1]
    }
}
Also used : Course(com.sharding.jdbc.entity.Course) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Course

use of com.sharding.jdbc.entity.Course in project note by kebukeYi.

the class ShardingJdbcApplicationTests method queryCourseByHint.

@Test
public void queryCourseByHint() {
    // 02 配置文件  强制路由策略打开
    HintManager hintManager = HintManager.getInstance();
    // 直接 定死 分表路由
    hintManager.addTableShardingValue("course", 2);
    // 直接 定死 分库路由
    // hintManager.addDatabaseShardingValue("coursedb",2);
    // Logic SQL: SELECT  cid,cname,user_id,cstatus  FROM course
    // Actual SQL: m1 ::: SELECT  cid,cname,user_id,cstatus  FROM course_2
    // Actual SQL: m2 ::: SELECT  cid,cname,user_id,cstatus  FROM course_2
    List<Course> courses = courseMapper.selectList(null);
    courses.forEach(course -> System.out.println(course));
    hintManager.close();
}
Also used : HintManager(org.apache.shardingsphere.api.hint.HintManager) Course(com.sharding.jdbc.entity.Course) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Course (com.sharding.jdbc.entity.Course)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 HintManager (org.apache.shardingsphere.api.hint.HintManager)1