项目截图:
MyBatis的映射文件中支持在基础SQL上添加一些逻辑操作,并动态拼接成完整的SQL之后再执行,以达到SQL复用、简化编程的效果。
我们根据实体类的不同取值,使用不同的SQL语句来进行查询。比如在id如果不为空时可以根据 id查询,如果username不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。
mapper接口:
public interface UserDao {
//复杂条件查询
public List findByUser(User user);
}
mapper映射文件:
User类代码:
package com.by.pojo;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private String password;
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}
测试:
package test;
import com.by.mapper.UserMapper;
import com.by.pojo.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MyBatisTest {
private SqlSession sqlSession;
private InputStream inputStream;
@Before
public void init() throws IOException {
//加载mybatis-config.xml
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建sqlSession
sqlSession = sessionFactory.openSession();
}
@Test
public void testFindAll(){
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setSex("男");
user.setAddress("香港");
List userList = userDao.findByUser(user);
for(User u : userList){
System.out.println(u);
}
}
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
}
为了简化上面where 1=1的条件拼装,我们可以使用where标签将if标签代码块包起来,将1=1条件去掉。若查询条件的开头为 “AND” 或 “OR”,where标签会将他们去除
Mapper文件:
package com.by.mapper;
import com.by.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
List selectUser(User userParam);
}
Mapper映射文件:
测试类代码:
package test;
import com.by.mapper.UserMapper;
import com.by.pojo.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MyBatisTest {
private SqlSession sqlSession;
private InputStream inputStream;
@Before
public void init() throws IOException {
//加载mybatis-config.xml
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建sqlSession
sqlSession = sessionFactory.openSession();
}
@Test
public void testIfAndWhere(){
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User userParam = new User();
//userParam.setId(41);
userParam.setUsername("张三丰");
//userParam.setBirthday(new Date());
//userParam.setSex("男");
List userList = userMapper.selectUser(userParam);
for (User user : userList) {
System.out.println(user);
}
}
@After
public void close() throws IOException {
sqlSession.close();
inputStream.close();
}
}
set标签用于动态包含需要更新的列,并会删掉额外的逗号。
Mapper:
public void updateByUser(User user);
update user
username=#{username},
birthday=#{birthday},
sex=#{sex},
address=#{address},
where id=#{id}
测试类:
@Test
public void testUpdateByUser(){
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setId(50);
user.setBirthday(new Date());
user.setAddress("加拿大");
userDao.updateByUser(user);
}
trim标签可以代替where标签、set标签。
mapper:
//修改
public void updateByUser2(User user);
update user
birthday=#{birthday},
sex=#{sex},
address=#{address},
where id=#{id}
foreach标签的常见使用场景是集合进行遍历。
mapper:
//批量删除
public void deleteUserByIds(@Param("ids") List ids);
//批量添加
public void insertUsers(@Param("userList") List userList);
delete from user where id in
#{id}
INSERT INTO user(username,password,birthday,sex,address)
VALUES
(#{user.username},#{user.password},#{user.birthday},
#{user.sex},#{user.address})
测试类代码:
@Test
public void testDeleteUserByIds(){
UserDao userDao = sqlSession.getMapper(UserDao.class);
List ids = new ArrayList();
ids.add(50);
ids.add(64);
ids.add(67);
userDao.deleteUserByIds(ids);
}
@Test
public void testInsertUsers(){
UserDao userDao = sqlSession.getMapper(UserDao.class);
long start = System.currentTimeMillis();
List userList = new ArrayList<>();
for(int i = 0 ;i < 10000; i++) {
User user = new User();
user.setUsername("刘德华");
user.setPassword("111");
user.setBirthday(new Date());
user.setSex("男");
user.setAddress("香港");
//userDao.insertUser(user);
userList.add(user);
}
userDao.insertUsers(userList);
long end = System.currentTimeMillis();
System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
sqlSession.commit();
}
sql元素标签用来定义可重复使用的SQL代码片段,使用时只需要用include元素标签引用即可。
mapper:
//复杂条件查询
public List findByUser3(User user);
and username=#{username}
and birthday=#{birthday}
and sex=#{sex}
and address=#{address}
测试类代码:
@Test
public void testFindAll3(){
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setAddress("香港");
user.setUsername("刘德华");
List userList = userDao.findByUser3(user);
for(User u : userList){
System.out.println(u);
}
}