MyBatis 学习 - 注解
首先,POJO
/** * @Title: Question.java * @Package com.test.model * @Description: TODO(POJO Question) * Copyright: Copyright (c) 2012 Company China Foreign Exchange Trade System and National Interbank Funding Center. * Building 24,1387 ZhangDong Road, Shanghai, China. * All Rights Reserved. * Company: Insigma HT * @author Ares * @date 2013-06-24 下午3:48:31 * * Revision History * * Date Programmer Notes * --------- --------------------- -------------------------------------------- * 2013-06-24 Ares initial */ package com.test.model; import java.util.Date; /** * * @author Ares * @Describe TODO(POJO Question) * */ public class Question { private String id; private int qorder; private String qname; private String qcontent; private Date updateTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public int getQorder() { return qorder; } public void setQorder(int qorder) { this.qorder = qorder; } public String getQname() { return qname; } public void setQname(String qname) { this.qname = qname; } public String getQcontent() { return qcontent; } public void setQcontent(String qcontent) { this.qcontent = qcontent; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }
继续,Mapper
/** * @Title: QuestionDAO.java * @Package com.test.dao * @Description: TODO(Question Mapper) * Copyright: Copyright (c) 2012 Company China Foreign Exchange Trade System and National Interbank Funding Center. * Building 24,1387 ZhangDong Road, Shanghai, China. * All Rights Reserved. * Company: Insigma HT * @author Ares * @date 2013-06-24 下午3:48:31 * * Revision History * * Date Programmer Notes * --------- --------------------- -------------------------------------------- * 2013-06-24 Ares initial */ package com.test.dao; import java.util.List; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import com.test.model.Question; /** * * @author Ares * @Describe TODO(Question Mapper) * */ public interface QuestionDAO { /** * AddBy Ares * TODO(find all questions) */ @Select("SELECT * FROM Question") public List<Question> findAll(); /** * AddBy Ares * TODO(find question by ID) */ @Select("SELECT * FROM Question WHERE ID = #{id}") public Question findByID(@Param("id")String id); /** * AddBy Ares * TODO(just save one question) */ @Insert("INSERT INTO Question VALUES(#{id},#{qorder},#{qname},#{qcontent},#{updateTime})") public void save(Question question); /** * AddBy Ares * TODO(save all questions in list) */ @InsertProvider(type=SqlProvider.class, method="saveAll") public void saveAll(List<Question> list); }
上面的Mapper中,saveAll方法使用了SqlProvider,这个是用作批量添加使用的。
/** * @Title: SqlProvider.java * @Package com.test.dao * @Description: TODO(SqlProvider) * Copyright: Copyright (c) 2012 Company China Foreign Exchange Trade System and National Interbank Funding Center. * Building 24,1387 ZhangDong Road, Shanghai, China. * All Rights Reserved. * Company: Insigma HT * @author Ares * @date 2013-6-24 下午3:48:31 * * Revision History * * Date Programmer Notes * --------- --------------------- -------------------------------------------- * 2013-6-24 Ares initial */ package com.test.dao; import java.text.MessageFormat; import java.util.List; import java.util.Map; import com.test.model.Question; /** * @author Ares * TODO(SqlProvider) */ public class SqlProvider { private static final String TABLE = "question"; public String saveAll(Map<String, List<Question>> params){ List<Question> list = (List<Question>)params.get("list"); if(list!=null){ StringBuffer sb = new StringBuffer("insert into ").append(TABLE).append(" values "); MessageFormat mf = new MessageFormat("(#'{'list[{0}].id}, #'{'list[{0}].qorder}, #'{'list[{0}].qname}, #'{'list[{0}].qcontent}, #'{'list[{0}].updateTime})"); for (int i=0; i<list.size(); i++) { sb.append(mf.format(new Object[]{i})); if (i < list.size() - 1) { sb.append(","); } } System.out.println(sb.toString()); return sb.toString(); } return null; } }
接下来,把配置文件拿过来,
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.test.model"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="jdbc" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true"/> <property name="username" value="root"/> <property name="password" value="test"/> </dataSource> </environment> </environments> <mappers> <package name="com.test.dao"/> </mappers> <!-- <mappers> <mapper resource="com/test/model/Question.xml"/> </mappers> --> </configuration>
最后,开始测试
/** * @Title: MyBatisTest.java * @Package com.test.test * @Description: TODO(MyBatisTest) * Copyright: Copyright (c) 2012 Company China Foreign Exchange Trade System and National Interbank Funding Center. * Building 24,1387 ZhangDong Road, Shanghai, China. * All Rights Reserved. * Company: Insigma HT * @author Ares * @date 2013-06-24 下午3:48:31 * * Revision History * * Date Programmer Notes * --------- --------------------- -------------------------------------------- * 2013-06-24 Ares initial */ package com.test.test; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import com.test.dao.QuestionDAO; import com.test.model.Question; /** * @author Ares * TODO(MyBatisTest) */ public class MyBatisTest { @Test public void testMyBatis(){ SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(this.getClass().getResourceAsStream("/com/test/cfg/sqlMapClient.xml")); SqlSession sqlSession = sqlSessionFactory.openSession(); QuestionDAO questionDao = sqlSession.getMapper(QuestionDAO.class); List<Question> list = new ArrayList<Question>(); Question q = new Question(); q.setId(UUID.randomUUID().toString()); q.setQname("Are you ok?"); q.setQcontent("I'm ok."); q.setQorder(6); q.setUpdateTime(new Date()); list.add(q); Question q2 = new Question(); q2.setId(UUID.randomUUID().toString()); q2.setQname("How are you?"); q2.setQcontent("I'm ok."); q2.setQorder(6); q2.setUpdateTime(new Date()); list.add(q2); questionDao.saveAll(list); sqlSession.commit(); List<Question> questions = questionDao.findAll(); for (Question question : questions) { System.out.println(question.getQname() + " \tReply: "+question.getQcontent()); } sqlSession.close(); } }