日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

05_MyBatis基于注解的开发

發布時間:2024/9/27 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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基于注解的开发的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。