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();

}

}

}

最新文章

  1. Python中下划线的使用方法
  2. iOS CoreData技术学习资源汇总
  3. PL/SQL Developer 连接新数据库
  4. 电脑设置固定ip
  5. ios开发多线程--GCD
  6. IO多路复用的几种实现机制的分析
  7. Delphi 函数参数修饰中的var 、out和const
  8. PHP unlink() 函数
  9. Protocol Buffer和JSON性能比较
  10. Ef+T4模板实现代码快速生成器
  11. 详解 Node + Redux + MongoDB 实现 Todolist
  12. 使用DotNetty编写跨平台网络通信程序
  13. hihoCoder #1015 : KMP算法【KMP裸题,板子】
  14. SpringCloud Config客户端
  15. servlet篇 之 servlet概念及其功能实现
  16. Navicat for MySQL用ssh功能连接远程数据库
  17. Jamie and Tree CodeForces - 916E (换根)
  18. 团队作业——Alpha冲刺 1/12
  19. ruby -检查json数据类型
  20. 16. orcle中replace的用法及例子

热门文章

  1. hdu 1402 FFT(模板)
  2. bzoj省选十连测推广赛
  3. 勤拂拭软件Android开发之旅(1) 之 Android 开发环境搭建
  4. 《java技术》第二次作业
  5. PostgreSQL 查看单表大小
  6. Failed to connect to GitHub to update the CocoaPods/Specs specs repo - Please check if you are offline, or that GitHub is down
  7. ubuntu远程桌面连接命令rdesktop连接windows远程桌面详解
  8. Python中求1到20平方的两种方法
  9. [self init]
  10. 将luarocks整合进openresty