package com.badao.test;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.badao.beans.Employee;
import com.badao.mapper.EmployeeMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
public class TestMp {
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeeMapper employeeMapper =ioc.getBean("employeeMapper",EmployeeMapper.class);
@Test
public void testDataSource() throws Exception{
DataSource ds = ioc.getBean("dataSource",DataSource.class);
System.out.println("******************************************"+ds);
Connection connection = ds.getConnection();
System.out.println("******************************************"+connection);
}
/***
* 测试通用插入操作
*/
@Test
public void testCommonInsert() {
Employee employee = new Employee();
employee.setName("霸道插入测试");
employee.setAge(23);
employee.setEmail("123@163.com");
employee.setGender(1);
int result = employeeMapper.insert(employee);
System.out.println("************************"+result);
}
/***
* 测试插入数据获取主键
*/
@Test
public void testInsertReturnId() {
Employee employee = new Employee();
employee.setName("霸道获取主键");
employee.setAge(23);
employee.setEmail("123@163.com");
employee.setGender(1);
int result = employeeMapper.insert(employee);
System.out.println("************************"+result);
Integer id = employee.getId();
System.out.println("*********************"+id);
}
/***
* 测试insert
*/
@Test
public void testInsert() {
Employee employee = new Employee();
employee.setName("insert测试");
employee.setAge(23);
int result = employeeMapper.insert(employee);
System.out.println("************************"+result);
Integer id = employee.getId();
System.out.println("*********************"+id);
}
/***
* 测试insertAllColumn
*/
@Test
public void testInsertAllColumn() {
Employee employee = new Employee();
employee.setName("insertAllColumn测试");
employee.setAge(23);
int result = employeeMapper.insertAllColumn(employee);
System.out.println("************************"+result);
Integer id = employee.getId();
System.out.println("*********************"+id);
}
/***
* 通用更新操作
*/
@Test
public void testCommomUpdate() {
//
Employee employee = new Employee();
employee.setId(1);
employee.setName("更新测试成功");
int result=employeeMapper.updateById(employee);
System.out.println("*******************"+result);
}
/***
* updateAllColumnById操作
*/
@Test
public void testCommomUpdateAllColumnById() {
Employee employee = new Employee();
employee.setId(2);
employee.setName("更新测试成功");
int result=employeeMapper.updateAllColumnById(employee);
System.out.println("*******************"+result);
}
/***
* 通用查询操作 通过ID查询
*/
@Test
public void testCommomSelectById() {
Employee employee=employeeMapper.selectById(1);
System.out.println("*******************"+employee);
}
/***
* 通用查询操作 selectOne 最多只能查到一个
*/
@Test
public void testCommomSelectOne() {
Employee employee = new Employee();
employee.setId(3);
employee.setName("气质");
Employee employeeresult=employeeMapper.selectOne(employee);
System.out.println("*******************"+employeeresult);
}
/***
* 通用查询操作 selectBatchIds 通过多个ID进行查询
*/
@Test
public void testCommomSelectBatchIds() {
List<Integer> idList = new ArrayList<Integer>();
idList.add(1);
idList.add(2);
idList.add(3);
List<Employee>employeeList=employeeMapper.selectBatchIds(idList);
System.out.println("*******************"+employeeList);
for (Employee employee : employeeList) {
System.out.println(employee.getName());
}
}
/***
* 通用查询操作 selectByMap map要写列名条件 不能是实体属性名
*/
@Test
public void testCommomSelectByMap() {
Map<String, Object> columnMap = new HashMap<String, Object>();
columnMap.put("name", "气质");
columnMap.put("gender", "0");
List<Employee> employeeList=employeeMapper.selectByMap(columnMap);
System.out.println("*******************"+employeeList);
for (Employee employee : employeeList) {
System.out.println(employee.getName());
}
}
/***
* 通用查询操作 selectByPage
*/
@Test
public void testCommomSelectByPage() {
List<Employee> employeeList=employeeMapper.selectPage(new Page<Object>(2, 2), null);
System.out.println("*******************"+employeeList);
for (Employee employee : employeeList) {
System.out.println(employee.getName());
}
}
/***
* 通用删除操作 通过ID删除
*/
@Test
public void testCommomDeleteById() {
int result=employeeMapper.deleteById(8);
System.out.println("*******************"+result);
}
/***
* 通用删除操作 deleteByMap map要写列名条件 不能是实体属性名
*/
@Test
public void testCommomDeleteByMap() {
Map<String, Object> columnMap = new HashMap<String, Object>();
columnMap.put("name", "更新测试成功");
columnMap.put("gender", "1");
int result=employeeMapper.deleteByMap(columnMap);
System.out.println("*******************"+result);
}
/***
* 通用查询操作 deleteBatchIds 通过多个ID进行删除
*/
@Test
public void testCommomDeleteBatchIds() {
List<Integer> idList = new ArrayList<Integer>();
idList.add(5);
idList.add(6);
int result=employeeMapper.deleteBatchIds(idList);
System.out.println("*******************"+result);
}
/***
*条件构造器 查询操作selectPage
*/
@Test
public void testEntitymapperSelect() {
//分页查询数据库,年龄字段在某个区间内,并且性别为男(1),并且名字为霸道的记录
List<Employee> employeeList=employeeMapper.selectPage(new Page<Object>(1, 2), new EntityWrapper<Employee>()
.between("age", 20, 30)
.eq("gender",1)
.eq("name", "霸道")
);
System.out.println("*******************"+employeeList);
for (Employee employee : employeeList) {
System.out.println(employee.getName());
}
}
/***
*条件构造器 查询操作SelectList
*/
@Test
public void testEntitymapperSelectList() {
//查询数据库,性别为男(1),并且名字中带有霸的记录或者email中带有123的记录。
List<Employee> employeeList=employeeMapper.selectList(new EntityWrapper<Employee>()
.eq("gender",1)
.like("name", "霸")
//.or()//SELECT id AS id,`name`,email,gender,age FROM employee WHERE (gender = ? AND name LIKE ? OR email LIKE ?)
.orNew()//SELECT id AS id,`name`,email,gender,age FROM employee WHERE (gender = ? AND name LIKE ?) OR (email LIKE ?)
.like("email", "123")
);
System.out.println("*******************"+employeeList);
for (Employee employee : employeeList) {
System.out.println(employee.getName());
}
}
/***
*条件构造器 更新操作
*/
@Test
public void testEntitymapperUpdate() {
Employee employee =new Employee();
employee.setEmail("456@163.com");
employeeMapper.update(employee,new EntityWrapper<Employee>()
.eq("gender",1)
.like("name", "霸")
);
}
/***
*条件构造器 删除操作
*/
@Test
public void testEntitymapperDelete() {
employeeMapper.delete(new EntityWrapper<Employee>()
.