传入POJO对象
QueryVo.java
public class QueryVo implements Serializable{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
SQL映射文件
测试类
@Test
public void func9() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2.创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
// -------------------------------------------
// 4.SqlSEssion帮我生成一个实现类(给接口的字节码文件)
UserDao mapper = sqlSession.getMapper(UserDao.class);
// 创建一个POJO对象
QueryVo vo = new QueryVo();
User u = new User();
u.setId(2);
vo.setUser(u);
User userByPOJO = mapper.findUserByPOJO(vo);
System.out.println(userByPOJO);
// -------------------------------------------
}
resultMap(手动指定字段)
数据库表
Order实体文件
public class Order {
// 订单id
private int id;
// 用户id
private Integer userId;
// 订单号
private String number;
// 订单创建时间
private Date createtime;
// 备注
private String note;
get/set。。。
}
SQL映射文件
由于实体和SQL中的userid字段不同,所以要使用resultMap
OrderDao.java
public interface OrderDao {
Order findOrderById(int i);
}
测试类
public void func() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2.创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
// -------------------------------------------
// 4.执行相应的SQL语句
OrderDao mapper = sqlSession.getMapper(OrderDao.class);
Order orderById = mapper.findOrderById(1);
System.out.println(orderById);
// -------------------------------------------
}
一.动态SQL
1)if标签&&where标签
2)foreach标签
a.直接传入数组
UserDao.java
public interface UserDao {
List selectUserByIds(int[] ids);
}
配置文件
测试类
@Test
public void func3() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2.创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
// -------------------------------------------
// 方法一:直接传入一个数组
int[] ids = {1,2,3};
UserDao mapper = sqlSession.getMapper(UserDao.class);
List list = mapper.selectUserByIds(ids);
System.out.println(list);
// -------------------------------------------
}
b.直接传入集合
测试类
// 方法二:直接传入一个集合
List ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
UserDao mapper = sqlSession.getMapper(UserDao.class);
List list = mapper.selectUserByIds(ids);
System.out.println(list);
c.传入一个POJO,里面有集合或者数组
测试类
// 方法三:传入一个POJO
//List ids = new ArrayList<>();
//
//ids.add(1);
//ids.add(2);
//ids.add(3);
int[] ids = {1,2,3};
QueryVo vo = new QueryVo();
vo.setIds(ids);
UserDao mapper = sqlSession.getMapper(UserDao.class);
List list = mapper.selectUserByIds(vo);
System.out.println(list);
二.关联查询
1、一对一查询
1)在Order中添加一个user字段并提供get,set方法
2)书写接口
List findOrderList();
3)书写配置文件
4)测试类
public void func4() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2.创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
// -------------------------------------------
OrderDao mapper = sqlSession.getMapper(OrderDao.class);
List orderList = mapper.findOrderList();
System.out.println(orderList);
// -------------------------------------------
}
2、一对多查询
1)在User中添加一个集合,保存Order对象
2)书写接口
ListfindUserAllOrder();
3)SQL映射文件
测试类
@Test
public void func5() throws IOException {
// 1.加载配置文件
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
// 2.创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 3.获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
// -------------------------------------------
OrderDao mapper = sqlSession.getMapper(OrderDao.class);
List userAllOrderList = mapper.findUserAllOrder();
System.out.println(userAllOrderList);
// -------------------------------------------
}
三.与Spring整合
方式一:原始Dao方式
1)Spring配置文件
2)UserDao.java
// 继承SqlSessionDaoSupport,调用父类的getSqlSession方法
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
public void main(String[] args) {
super.getSqlSession().selectList("xxx.xxx","参数");
}
}
方式二:mapper代理模式
1)Spring配置文件
2)Mybatis主配置文件
3)SQL映射关系配置文件
4)UserMapper接口
public interface UserMapper {
User findUserById(Integer id);
}
5)测试类
public void func(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("spring-config.xml");
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
User userById = userMapper.findUserById(1);
System.out.println(userById);
}
方式三:动态扫描
上面的会有一个弊端,如果Mapper太多,配置文件就变得很多。所以引入扫描
获取mapper方法
UserMapper userMapper = ac.getBean(UserMapper.class);