05_MyBatis基于注解的开发


  1. 要想开发基于注解的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基于注解的开发)