原文链接:http://blog.csdn.net/tototuzuo ... 01099
1 PersonTestMapper.xml中的内容如下:
2 PersonMapper.xml的配置内容如下(resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询map传递参数,模糊查询,插入,更新,删除,where条件查询,动态修改,in查询foreach迭代,批量插入foreach,批量删除,一对多查询,extends:resultMap的继承,多对多查询,延迟加载):
PERSON_ID, NAME, GENDER, PERSON_ADDR, BIRTHDAY
select LAST_INSERT_ID()
insert into person (person_id, name, gender, person_addr, birthday)
values(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday})
update person p set p.name = #{name},
p.gender = #{gender},
p.person_addr = #{personAddr},
p.birthday = #{birthday}
where p.person_id = #{personId}
delete from person where person_id = #{personId}
update person t
t.name = #{name},
t.gender = #{gender},
t.person_addr = #{personAddr},
t.birthday = #{birthday}
where t.person_id = #{personId}
select LAST_INSERT_ID()
insert into person (person_id, name, gender, person_addr, birthday)
values
(#{person.personId}, #{person.name}, #{person.gender}, #{person.personAddr}, #{person.birthday})
delete from person where person_id in
#{personId}
3 配置sqlMapConfig.xml中的配置:
4 测试代码如下:
MybatisTest.java
package com.rl.test;
import java.io.InputStream;
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.model1.Person;
import com.rl.model1.QueryCondition;
/**
* mybatis的简单查询
*/
public class MybatisTest {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
}
/**
* 通过id查询数据
*/
@Test
public void selectPersonById() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
com.rl.model.Person person = session.selectOne(
"com.rl.mapper.PersonTestMapper.selectPersonById", 1);
System.out.println(person);
} finally{
session.close();
}
}
/**
* 使用resultMap来查询
*/
@Test
public void selectPersonById1() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
Person person = session.selectOne("com.rl.mapper.PersonMapper.selectPersonById", 1);
System.out.println(person);
} finally{
session.close();
}
}
/**
* 查询表的记录数
*/
@Test
public void selectPersonCount() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Integer count = session.selectOne("com.rl.mapper.PersonMapper.selectPersonCount");
System.out.println(count);
} finally{
session.close();
}
}
/**
* 查询所有Person
*/
@Test
public void selectPersonAll() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//查询集合的时候需要使用selectList
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonAll");
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
/**
* 使用多个参数用Map方式来查询
*/
@Test
public void selectPersonByParams() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Map map = new HashMap();
map.put("gender",0);
map.put("birthday", new Date());
//查询集合的时候需要使用selectList
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByParams",map);
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
/**
* 使用多个参数查询数据,使用查询对象的方式
*/
@Test
public void selectPersonByParams1() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
QueryCondition qc = new QueryCondition();
qc.setGender("0");
qc.setBirthday(new Date());
//查询集合的时候需要使用selectList
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByParams1",qc);
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
/**
* 模糊查询
*/
@Test
public void selectPersonByLike() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
QueryCondition qc = new QueryCondition();
qc.setName("武");
//查询集合的时候需要使用selectList
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByLike",qc);
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
}
MybatisTest1.java的内容如下:
package com.rl.test;
import java.io.InputStream;
import java.util.Date;
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.model1.Person;
/**
* mybatis的数据库表的变更
*/
public class MybatisTest1 {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
}
/**
* 数据插入主键返回
* public void saveOrder(Orders order, List detailList){
orderDao.save(order);
for(OrderDetail detail : detailList){
detail.setOrderId(order.getOrderId());
detailDao.save(detail)
}
}
*/
@Test
public void insert(){
SqlSession session = sessionFactory.openSession();
Person p = new Person();
//p.setPersonId(3);
p.setName("武松");
p.setGender("0");
p.setPersonAddr("阳谷县");
p.setBirthday(new Date());
try {
session.insert("com.rl.mapper.PersonMapper.insert", p);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
/**
* 修改,将id是3的记录改成
*/
@Test
public void update(){
SqlSession session = sessionFactory.openSession();
Person p = new Person();
p.setPersonId(3);
p.setName("陆虞候");
p.setGender("0");
p.setPersonAddr("阳谷县");
p.setBirthday(new Date());
try {
session.update("com.rl.mapper.PersonMapper.update", p);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
/**
* 删除
*/
@Test
public void delete(){
SqlSession session = sessionFactory.openSession();
try {
session.delete("com.rl.mapper.PersonMapper.delete", 4);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
}
MybatisTest2.java
package com.rl.test;
import java.io.InputStream;
import java.util.ArrayList;
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.model1.Person;
/**
* mybatis的动态sql
*/
public class MybatisTest2 {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void selectPersonByCondition() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Map map = new HashMap();
map.put("name", "安");
map.put("gender", "0");
//map.put("personAddr", "东京");
//map.put("birthday", new Date());
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByCondition", map);
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
@Test
public void dynamicUpdate(){
SqlSession session = sessionFactory.openSession();
Person p = new Person();
p.setPersonId(3);
p.setName("陆虞候");
p.setGender("0");
//p.setPersonAddr("阳谷县");
//p.setBirthday(new Date());
try {
session.update("com.rl.mapper.PersonMapper.dynamicUpdate", p);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
/**
* foreach的用法
*/
@Test
public void selectPersonByIn() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Map map = new HashMap();
/*List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);*/
String [] list = {"1","2","3"};
map.put("ids", list);
List pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByIn", map);
for(Person p : pList){
System.out.println(p);
}
} finally{
session.close();
}
}
/**
* 批量插入
*/
@Test
public void insertBatch(){
SqlSession session = sessionFactory.openSession();
Map map = new HashMap();
List pList= new ArrayList();
try {
for(int i = 0; i < 1000009; i++){
Person p = new Person();
p.setName("武松"+i);
p.setGender("0");
p.setPersonAddr("阳谷县");
p.setBirthday(new Date());
pList.add(p);
if(i0 == 0){
map.put("pList", pList);
session.insert("com.rl.mapper.PersonMapper.insertBatch", map);
pList.clear();
}
}
map.put("pList", pList);
session.insert("com.rl.mapper.PersonMapper.insertBatch", map);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
@Test
public void deleteBatch(){
SqlSession session = sessionFactory.openSession();
Map map = new HashMap();
List ids= new ArrayList();
try {
for(int i = 106; i < 1000115; i++){
ids.add(i);
if(i0 == 0){
map.put("ids", ids);
session.delete("com.rl.mapper.PersonMapper.deleteBatch", map);
ids.clear();
}
}
map.put("ids", ids);
session.insert("com.rl.mapper.PersonMapper.deleteBatch", map);
//库表的变更都需要提交
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
session.close();
}
}
}
MybatisTest3.java的内容如下:
package com.rl.test;
import java.io.InputStream;
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.model1.Orders;
import com.rl.model1.Person;
import com.rl.model1.Role;
/**
* mybatis的关联查询
*/
public class MybatisTest3 {
SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(in);
}
/**
* 一对多关联查询
*/
@Test
public void selectPersonAndOrderByPId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
Person person = session.selectOne("com.rl.mapper.PersonMapper.selectPersonAndOrderByPId", 1);
System.out.println(person);
} finally{
session.close();
}
}
/**
* 查询Person下的所有订单和订单下的明细
*/
@Test
public void selectPersonOrderAndDetailByPId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
Person person = session.selectOne("com.rl.mapper.PersonMapper.selectPersonOrderAndDetailByPId", 1);
System.out.println(person);
} finally{
session.close();
}
}
/**
* 多对多查询从Person端出发
*/
@Test
public void selectPersonAndRoleByPId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
Person person = session.selectOne("com.rl.mapper.PersonMapper.selectPersonAndRoleByPId", 1);
System.out.println(person);
} finally{
session.close();
}
}
/**
* 多对多查询从角色端来看
*/
@Test
public void selectRoleAndPersonByRId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数
Role role = session.selectOne("com.rl.mapper.RoleMapper.selectRoleAndPersonByRId", 1);
System.out.println(role);
} finally{
session.close();
}
}
/**
* 多对一的关联查询
*这里的mapper配置文件在后续的博文中定义。
*/
@Test
public void selectPersonByOrderId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Orders order = session.selectOne("com.rl.mapper.OrdersMapper.selectPersonByOrderId", 1);
System.out.println(order);
} finally{
session.close();
}
}
/**
* 多对一和一对多混合查询
* 这里的mapper配置文件在后续的博文中定义。
*/
@Test
public void selectPersonAndDetailByOrderId() {
//创建session对象
SqlSession session = sessionFactory.openSession();
try {
Orders order = session.selectOne("com.rl.mapper.OrdersMapper.selectPersonAndDetailByOrderId", 1);
System.out.println(order);
} finally{
session.close();
}
}
}