1.项目相关路径
2.Mybatis的配置文件
3.Mybatis的工具类
package com.util;
import java.io.IOException;
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;
public class Util {
private static SqlSessionFactory sqlSessionFactory = null;
/**
* 初始化Session工厂
* @throws IOException
*/
private static void initialFactory() throws IOException {
String resource = "Mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 获取Session
* @return
*/
public static SqlSession getSession() {
if(sqlSessionFactory == null) {
try {
initialFactory();
} catch (IOException e) {
e.printStackTrace();
}
}
return sqlSessionFactory.openSession();
}
}
4.创建实体类以及对应的Mapping.xml文件
package com.model;
public class Users {
private Integer id;
private String name;
private int age;
public Integer getId()
{ return id; }
public void setId(Integer id)
{ this.id = id; }
public String getName()
{ return name; }
public void setName(String name)
{ this.name = name; }
public int getAge()
{ return age; }
public void setAge(int age)
{ this.age = age; }
@Override
public String toString()
{ return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
insert into Users (id,name,age) values (#{id},#{name},#{age})
insert into Users (id,name,age)
SELECT
#{item.id} id,
#{item.name} name,
#{item.age} age
FROM dual
insert into Users (id,name,age)
SELECT
#{item.id} id,
#{item.name} name,
#{item.age} age
FROM dual
insert into Users (id,name,age)
SELECT
#{item.id} id,
#{item.name} name,
#{item.age} age
FROM dual
delete from Users where id=#{id}
delete from Users where id in (
select
#{item.id}
from dual
)
update Users set name=#{name} where id=#{id}
5.测试类
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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.After;
import org.junit.Before;
import org.junit.Test;
import com.model.Users;
import com.util.Util;
public class TestMybatis{
private SqlSession session = null;
@Before
public void before() {
session = Util.getSession();
}
@After
public void after() {
session.commit();
session.close();
}
//1.查询(数字对应Mapping.xml的数字方法)
@SuppressWarnings("unchecked")
@Test
public void testSelectUser() throws IOException {
List userList = session.selectList("com.mapping.UserMapping.getUser");
for(Users each : userList) {
System.out.println("each->" + each);
}
}
//2.插入
@Test
public void testInsert() {
Users user = new Users();
user.setId(200);
user.setName("路飞");
user.setAge(23);
session.insert("com.mapping.UserMapping.insertUser" , user);
}
//7.根据id查询数据
@Test
public void testSelectUserById() throws IOException {
Users user = (Users) session.selectOne("com.mapping.UserMapping.getUserById",1);
System.out.println("each->" + user.toString());
}
//8.根据id删除数据
@Test
public void testDeleteUserById() throws IOException {
session.delete("com.mapping.UserMapping.deleteUserById",1);
System.out.println("删除成功!");
}
// 10.更新数据
@Test
public void testUpdateUserById() throws IOException {
Users user = (Users) session.selectOne("com.mapping.UserMapping.getUserById",1);
user.setName("11111111");
session.update("com.mapping.UserMapping.updateUserById",user);
System.out.println("更新成功!");
}
//3.批量插入list
@Test
public void testInsertUserList() throws IOException {
Users user = new Users();
user.setId(100);
user.setName("路飞");
user.setAge(23);
Users user1 = new Users();
user1.setId(101);
user1.setName("路飞");
user1.setAge(23);
List userList = new ArrayList();
userList.add(user);
userList.add(user1);
session.insert("com.mapping.UserMapping.insertUserList" , userList);
System.out.println("插入成功!");
}
//4.批量插入map
@Test
public void testInsertUserMap() throws IOException {
Users user = new Users();
user.setId(106);
user.setName("路飞");
user.setAge(23);
Users user1 = new Users();
user1.setId(104);
user1.setName("路飞");
user1.setAge(23);
List list =new ArrayList();
list.add(user1);
list.add(user);
Map> map = new HashMap>();
map.put("map",list);//map存放的key值要与mapper.xml文件中的的collection名称一致
session.insert("com.mapping.UserMapping.insertUserMap" , map);
System.out.println("插入成功!");
}
// 5.批量插入List
foreach知识总结
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map