05_MyBatis基于注解的开发
要想開發基于注解的MyBatis應用。需要先寫一個帶有注解的接口。
PersonDao.java的寫法如下:
| package com.rl.dao; ? import java.util.List; import java.util.Map; ? import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.SelectKey; import org.apache.ibatis.annotations.SelectProvider; import org.apache.ibatis.annotations.Update; ? import com.rl.model1.Person; import com.rl.util.SqlHelper; ? public interface PersonDao { ???????? ???????? @Select("select * from person t where t.person_id = #{personId}") ???????? @Results(value={ ?????????????????? @Result(column="person_id", property="personId", id=true), ?????????????????? @Result(column="name", property="name"), ?????????????????? @Result(column="gender", property="gender"), ?????????????????? @Result(column="person_addr", property="personAddr"), ?????????????????? @Result(column="birthday", property="birthday") ???????? }) ???????? public Person selectPersonById(Integer personId); ???????? ???????? @Select("select * from person") ???????? @Results(value={ ?????????????????? @Result(column="person_id", property="personId", id=true), ?????????????????? @Result(column="name", property="name"), ?????????????????? @Result(column="gender", property="gender"), ?????????????????? @Result(column="person_addr", property="personAddr"), ?????????????????? @Result(column="birthday", property="birthday") ???????? }) ???????? public List<Person> selectPersonAll(); ???????? ???????? @Select("select * from person t where t.gender = #{gender} and t.birthday < #{birthday}") ???????? @Results(value={ ?????????????????? @Result(column="person_id", property="personId", id=true), ?????????????????? @Result(column="name", property="name"), ?????????????????? @Result(column="gender", property="gender"), ?????????????????? @Result(column="person_addr", property="personAddr"), ?????????????????? @Result(column="birthday", property="birthday") ???????? }) ???????? public List<Person> selectPersonByParams(Map<String,Object> map); ???????? ???????? ???????? @Select("select * from person t where t.name like '%${name}%'") ???????? @Results(value={ ??????????????????????????? @Result(column="person_id", property="personId", id=true), ??????????????????????????? @Result(column="name", property="name"), ??????????????????????????? @Result(column="gender", property="gender"), ??????????????????????????? @Result(column="person_addr", property="personAddr"), ??????????????????????????? @Result(column="birthday", property="birthday") ???????? }) ???????? public List<Person> selectPersonByLike(Map<String,Object> map); ???????? ? ???????? @Insert("insert into person (person_id, name, gender, person_addr, birthday) " + ??????????????????????????? "values(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday})") ???????? @SelectKey(before = false, keyProperty = "personId", resultType = java.lang.Integer.class, statement = { "select LAST_INSERT_ID()" }) ???????? public void insert(Person person); ???????? ???????? @Update("update person p set p.name = #{name}," + ??????????????????????????? "p.gender = #{gender}," + ??????????????????????????? "p.person_addr = #{personAddr}," + ??????????????????????????? "p.birthday = #{birthday} " + ??????????????????????????? "where p.person_id = #{personId}") ???????? public void update(Person person); ???????? ???????? @Delete("delete from person where person_id = #{personId}") ???????? public void delete(Integer personId); ???????? ???????? @SelectProvider(type=SqlHelper.class, method="getSql") ???????? @Results(value={ ??????????????????????????? @Result(column="person_id", property="personId", id=true), ??????????????????????????? @Result(column="name", property="name"), ??????????????????????????? @Result(column="gender", property="gender"), ??????????????????????????? @Result(column="person_addr", property="personAddr"), ??????????????????????????? @Result(column="birthday", property="birthday") ???????? }) ???????? public List<Person> selectPersonByCondition(Map<String,Object> map); ???????? ???????? @Select("select * from person p, orders o where p.person_id = o.person_id and p.person_id = #{personId}") ???????? @ResultMap(value="com.rl.mapper.PersonMapper.selectPersonAndOrderByPIdRM") ???????? public Person selectOrdersByPersonId(Integer personId); } |
測試類:
| package com.rl.test; ? import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; ? import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; ? import com.rl.dao.PersonDao; import com.rl.model1.Person; ? /** ?* mybatis的注解開發 ?*/ public class MybatisTest6 { ? ???????? SqlSessionFactory sessionFactory; ???????? ???????? @Before ???????? public void setUp() throws Exception { ?????????????????? InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml"); ?????????????????? sessionFactory = new SqlSessionFactoryBuilder().build(in); ?????????????????? //注冊接口 ?????????????????? sessionFactory.getConfiguration().addMapper(PersonDao.class); ???????? } ? ???????? @Test ???????? public void selectPersonById(){ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? Person person = personDao.selectPersonById(1); ??????????????????????????? System.out.println(person); ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void selectPersonAll(){ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? List<Person> pList = personDao.selectPersonAll(); ??????????????????????????? for(Person p : pList){ ???????????????????????????????????? System.out.println(p); ??????????????????????????? } ?????????????????? } finally { ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void selectPersonByParams() throws Exception { ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? Map<String,Object> map = new HashMap<String,Object>(); ?????????????????? map.put("gender", 0); ?????????????????? map.put("birthday", new SimpleDateFormat("yyyy-MM-dd").parse("2014-08-08")); ?????????????????? try { ??????????????????????????? List<Person> pList = personDao.selectPersonByParams(map); ??????????????????????????? for(Person p : pList){ ???????????????????????????????????? System.out.println(p); ??????????????????????????? } ?????????????????? } finally { ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void selectPersonByLike() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? Map<String,Object> map = new HashMap<String,Object>(); ?????????????????? map.put("name", "安"); ?????????????????? try { ??????????????????????????? List<Person> pList = personDao.selectPersonByLike(map); ??????????????????????????? for(Person p : pList){ ???????????????????????????????????? System.out.println(p); ??????????????????????????? }?????????????????????????? ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void insert() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? Person p = new Person(); ??????????????????????????? p.setName("西門慶"); ??????????????????????????? p.setGender("0"); ??????????????????????????? p.setPersonAddr("陽谷縣"); ??????????????????????????? p.setBirthday(new Date()); ??????????????????????????? personDao.insert(p); ??????????????????????????? session.commit(); ?????????????????? }catch(Exception ex){ ??????????????????????????? ex.printStackTrace(); ??????????????????????????? session.rollback(); ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void update() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? Person p = new Person(); ??????????????????????????? p.setPersonId(5); ??????????????????????????? p.setName("大官人"); ??????????????????????????? p.setGender("0"); ??????????????????????????? p.setPersonAddr("陽谷縣"); ??????????????????????????? p.setBirthday(new Date()); ??????????????????????????? personDao.update(p); ??????????????????????????? session.commit(); ?????????????????? }catch(Exception ex){ ??????????????????????????? ex.printStackTrace(); ??????????????????????????? session.rollback(); ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? @Test ???????? public void delete() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? personDao.delete(5); ??????????????????????????? session.commit(); ?????????????????? }catch(Exception ex){ ??????????????????????????? ex.printStackTrace(); ??????????????????????????? session.rollback(); ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? ???????? /** ???????? ?* 動態條件組合查詢 ???????? ?*/ ???????? @Test ???????? public void selectPersonByCondition() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? Map<String,Object> map = new HashMap<String,Object>(); ?????????????????? //map.put("name", "安"); ?????????????????? //map.put("gender", "0"); ?????????????????? //map.put("personAddr", "府"); ?????????????????? //map.put("birthday", new Date()); ?????????????????? try { ??????????????????????????? List<Person> pList = personDao.selectPersonByCondition(map); ??????????????????????????? for(Person p : pList){ ???????????????????????????????????? System.out.println(p); ??????????????????????????? } ?????????????????? } finally { ??????????????????????????? session.close(); ?????????????????? } ???????? } ???????? /** ???????? ?* 管理查詢 ???????? ?*/ ???????? @Test ???????? public void selectOrdersByPersonId() throws Exception{ ?????????????????? SqlSession session = sessionFactory.openSession(); ?????????????????? //獲得接口的實現類 ?????????????????? PersonDao personDao = session.getMapper(PersonDao.class); ?????????????????? try { ??????????????????????????? Person person = personDao.selectOrdersByPersonId(1); ??????????????????????????? System.out.println(person); ?????????????????? }finally{ ??????????????????????????? session.close(); ?????????????????? } ???????? } } |
?
總結
以上是生活随笔為你收集整理的05_MyBatis基于注解的开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工行节节高1号和2号的区别
- 下一篇: 控制流结构