mybatis中的XMLLanguageDriver类出现的一大原因在于:使用注解配置sql语句没有使用xml的方式灵活和强大,为了弥补这种缺陷,XMLLanguageDriver出现了。下面只是简单的举个例子,贴点代码,不多做解释。因为对于使用过mybatis的人,一看就明白。
SimpleSelectInLangDriver
package com.core.mybatis.languageDriver;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;
public class SimpleSelectInLangDriver extends XMLLanguageDriver implements LanguageDriver {
private static final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
@Override
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
Matcher matcher = inPattern.matcher(script);
if (matcher.find()) {
script = matcher.replaceAll("<foreach collection=\"$1\" item=\"_item\" open=\"(\" "
+ "separator=\",\" close=\")\" >#{_item}</foreach>");
}
script = "<script>" + script + "</script>";
return super.createSqlSource(configuration, script, parameterType);
}
}
SimpleUpdateLangDriver
package com.core.mybatis.languageDriver;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;
public class SimpleUpdateLangDriver extends XMLLanguageDriver implements LanguageDriver{
private final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
@Override
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
Matcher matcher = inPattern.matcher(script);
if (matcher.find()) {
StringBuilder sb = new StringBuilder();
sb.append("<set>");
sb.append("<if test=\"name != null \"> name = #{name},</if>");
sb.append("<if test=\"price != null \"> price = #{price},</if>");
sb.deleteCharAt(sb.lastIndexOf(","));
sb.append("</set>");
script = matcher.replaceAll(sb.toString());
script = "<script>" + script + "</script>";
}
return super.createSqlSource(configuration, script, parameterType);
}
}
BookDao是dao层
package com.springmvc.family.dao;
import java.util.List;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.core.mybatis.languageDriver.SimpleSelectInLangDriver;
import com.core.mybatis.languageDriver.SimpleUpdateLangDriver;
import com.springmvc.family.model.Book;
/**
* @author lihong 2016年6月24日 下午2:12:19
* @version v1.0
*/
@Mapper
public interface BookDao
{
/**
* @param book
* @author lihong 2016年6月24日 下午2:12:39
* @since v1.0
*/
public void addBook(Book book);
/**
* 与addBook功能相同,此处是为了测试两种插入数据并获取主键的方法
* @param book void
* @author lihong 2016年6月24日 下午2:58:33
* @since v1.0
*/
public void insert(Book book);
/**
* @return List<Book>
* @author lihong 2016年6月24日 下午2:12:59
* @since v1.0
*/
public List<Book> getAll(Book book);
@Select("SELECT * FROM book WHERE id IN (#{idList})")
@Lang(SimpleSelectInLangDriver.class)
List<Book> getBookByIdInBatch(@Param("idList") List<Integer> idList);
@Update("UPDATE book (#{book}) WHERE id = #{id}")
@Lang(SimpleUpdateLangDriver.class)
void updateUsersById(Book book);
}
Book 类是实体类
@Alias("Book")
public class Book implements java.io.Serializable
{
/**
* @Fields serialVersionUID :
*/
private static final long serialVersionUID = -8019185784560242092L;
private int id;
private String name;
private float price;
/**
* @author lihong 2016年6月24日 下午2:11:59
* @since v1.0
*/
public Book()
{
}
/**
* @return the id
*/
public int getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(int id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the price
*/
public float getPrice()
{
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price)
{
this.price = price;
}
}
BookDaoTest 类是使用测试用例
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.springmvc.AnnotationServiceTest;
import com.springmvc.family.dao.BookDao;
import com.springmvc.family.model.Book;
/**
* @author lihong 2016年6月24日 下午2:37:49
* @version v1.0
*/
public class BookDaoTest extends AnnotationServiceTest
{
@Autowired
private BookDao bookDao;
private Book getBook()
{
Book book = new Book();
book.setName("测试生成的书名");
book.setPrice(2.32f);
return book;
}
/**
* 测试插入数据后返回自增的主键id值
* @author lihong 2016年6月24日 下午2:45:56
* @since v1.0
*/
@Test
public void testAdd()
{
for(int i = 0; i < 10; i++)
{
Book book = getBook();
bookDao.addBook(book);
System.out.println("数据库自增主键策略返回的id是: " + book.getId() + " ");
}
}
/**
* 采用另外一种配置方式测试插入数据后返回自增的主键id值
* @author lihong 2016年6月24日 下午3:02:09
* @since v1.0
*/
@Test
public void testInsert()
{
for(int i = 0; i < 10; i++)
{
Book book = getBook();
bookDao.insert(book);
System.out.println("数据库自增主键策略返回的id是: " + book.getId() + " ");
}
}
/**
* @author lihong 2016年6月24日 下午2:49:51
* @since v1.0
*/
@Test
public void testGetAll()
{
for(int i = 0; i < 4; i++) {
Book book = new Book();
book.setName("测试生成的书名");
book.setPrice(2.32f);
List<Book> all = bookDao.getAll(book);
for(Book bk : all)
{
System.out.print("id: " + bk.getId() + " ");
System.out.print("name: " + bk.getName() + " ");
System.out.print("price: " + bk.getPrice() + " ");
System.out.println("-----------------------------------------------------");
}
}
}
@Test
public void testGetInBatchById() {
List<Integer> idList = new ArrayList<Integer>();
idList.add(24);
idList.add(25);
List<Book> books = bookDao.getBookByIdInBatch(idList);
for(Book book : books)
{
System.out.print("id: " + book.getId() + " ");
System.out.print("name: " + book.getName() + " ");
System.out.print("price: " + book.getPrice() + " ");
System.out.println("-----------------------------------------------------");
}
}
@Test
public void testUpdateBookById() {
Book book = new Book();
book.setId(23);
book.setName("***************");
book.setPrice(2.32f);
bookDao.updateUsersById(book);
}
}