这里放置的是一些杂物,生人勿入。
Token的一般parse 过程。
@Test
public void shouldDemonstrateGenericTokenReplacement() {
GenericTokenParser parser = new GenericTokenParser("${", "}", new VariableTokenHandler(new HashMap<String, String>() {
{
put("first_name", "James");
put("initial", "T");
put("last_name", "Kirk");
put("var{with}brace", "Hiya");
put("", "");
}
}));
assertEquals("James T Kirk reporting.", parser.parse("${first_name} ${initial} ${last_name} reporting."));
assertEquals("Hello captain James T Kirk", parser.parse("Hello captain ${first_name} ${initial} ${last_name}"));
assertEquals("James T Kirk", parser.parse("${first_name} ${initial} ${last_name}"));
assertEquals("JamesTKirk", parser.parse("${first_name}${initial}${last_name}"));
assertEquals("{}JamesTKirk", parser.parse("{}${first_name}${initial}${last_name}"));
assertEquals("}JamesTKirk", parser.parse("}${first_name}${initial}${last_name}"));
assertEquals("}James{{T}}Kirk", parser.parse("}${first_name}{{${initial}}}${last_name}"));
assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
assertEquals("}James}T{Kirk", parser.parse("}${first_name}}${initial}{${last_name}"));
assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}"));
assertEquals("}James}T{Kirk{{}}", parser.parse("}${first_name}}${initial}{${last_name}{{}}${}"));
assertEquals("{$$something}JamesTKirk", parser.parse("{$$something}${first_name}${initial}${last_name}"));
assertEquals("${", parser.parse("${"));
assertEquals("${\\}", parser.parse("${\\}"));
assertEquals("Hiya", parser.parse("${var{with\\}brace}"));
assertEquals("", parser.parse("${}"));
assertEquals("}", parser.parse("}"));
assertEquals("Hello ${ this is a test.", parser.parse("Hello ${ this is a test."));
assertEquals("Hello } this is a test.", parser.parse("Hello } this is a test."));
assertEquals("Hello } ${ this is a test.", parser.parse("Hello } ${ this is a test."));
}
XPath 的Parse
@Test
public void shouldTestXPathParserMethods() throws Exception {
String resource = "resources/nodelet_test.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
XPathParser parser = new XPathParser(inputStream, false, null, null);
assertEquals((Long)1970l, parser.evalLong("/employee/birth_date/year"));
assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
assertEquals("${id_var}", parser.evalString("/employee/@id"));
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
assertEquals(7, parser.evalNodes("/employee/*").size());
XNode node = parser.evalNode("/employee/height");
assertEquals("employee/height", node.getPath());
assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
}
Temp File 的一些操作
@Before
public void setUp() throws Exception {
tempFile = File.createTempFile("migration", "properties");
tempFile.canWrite();
sourceFile = File.createTempFile("test1", "sql");
destFile = File.createTempFile("test2", "sql");
}
使用嵌入型数据库derby
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.12.1.1</version>
<scope>test</scope>
</dependency>
MyBatis中的依赖关系
@BeforeClass
public static void setup() throws Exception {
DataSource dataSource = BaseDataTest.createBlogDataSource();
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.setLazyLoadingEnabled(true);
configuration.getTypeAliasRegistry().registerAlias(Blog.class);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundBlogMapper.class);
configuration.addMapper(BoundAuthorMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
sqlSessionFactory ---> Configuration ---> Environment ---> DataSource
Environment ---> TransactionFactory
单个简单参数的查询
<select id="selectBlogWithPostsUsingSubSelect" parameterType="int" resultMap="blogWithPosts">
select * from Blog where id = #{id}
</select>
参数为列表
List<Post> posts = mapper.findPostsInList(new ArrayList<Integer>() {{
add(1);
add(3);
add(5);
}});
<select id="findPostsInList" parameterType="list" resultType="org.apache.ibatis.domain.blog.Post">
select * from post
where id in (#{list[0]},#{list[1]},#{list[2]})
</select>
参数为map
List<Post> findThreeSpecificPosts(@Param("one") int one,
RowBounds rowBounds,
@Param("two") int two,
int three);
<select id="findThreeSpecificPosts" parameterType="map" resultType="org.apache.ibatis.domain.blog.Post">
select * from post
where id in (#{one},#{two},#{2})
</select>
参数为对象外加枚举
<insert id="insertAuthor" parameterType="org.apache.ibatis.domain.blog.Author">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author (id,username,password,email,bio,favourite_section)
values(
#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection:VARCHAR}
)
</insert>
like 的用法
@Test
public void shouldSelectListOfPostsLike() {
SqlSession session = sqlSessionFactory.openSession();
try {
BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
List<Post> posts = mapper.selectPostsLike(new RowBounds(1,1),"%a%");
assertEquals(1, posts.size());
} finally {
session.close();
}
}
eager 加载以及延迟加载
@Select({
"SELECT *",
"FROM blog"
})
@Results({
@Result(property = "author", column = "author_id", one = @One(select = "org.apache.ibatis.binding.BoundAuthorMapper.selectAuthor", fetchType=FetchType.EAGER)),
@Result(property = "posts", column = "id", many = @Many(select = "selectPostsById", fetchType=FetchType.EAGER))
})
List<Blog> selectBlogsWithAutorAndPostsEagerly();