https://gitee.com/free https://gitee.com/free/Mapper/wikis/1.1-java?parent=1.integration
CREATE TABLE `tabple_emp` ( `emp_id` int NOT NULL AUTO_INCREMENT , `emp_name` varchar(500) NULL , `emp_salary` double(15,5) NULL , `emp_age` int NULL , PRIMARY KEY (`emp_id`) ); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('tom', '1254.37', '27'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('jerry', '6635.42', '38'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('bob', '5560.11', '40'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('kate', '2209.11', '22'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('justin', '4203.15', '30');
public class Employee { private Integer empId; private String empName; private Double empSalary; private Integer empAge; public Employee() { } public Employee(Integer empId, String empName, Double empSalary, Integer empAge) { super(); this.empId = empId; this.empName = empName; this.empSalary = empSalary; this.empAge = empAge; } @Override public String toString() { return "Employee [empId=" + empId + ", empName=" + empName + ", empSalary=" + empSalary + ", empAge=" + empAge + "]"; } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Double getEmpSalary() { return empSalary; } public void setEmpSalary(Double empSalary) { this.empSalary = empSalary; } public Integer getEmpAge() { return empAge; } public void setEmpAge(Integer empAge) { this.empAge = empAge; } }
tk.mybatis
mapper
4.0.0-beta3
/ * 具体操作数据库的Mapper 接口, 需要继承通用Mapper 提供的核心接口: Mapper
* 泛型类型就是实体类的类型 * @author Lenovo */ public interface EmployeeMapper extends Mapper
{ }
SELECT emp_id,emp_name,emp_salary_apple,emp_age FROM tabple_emp WHERE emp_id = ? AND emp_name = ? AND emp_salary_apple = ? AND emp_age = ?
@Transient private String otherThings; //非数据库表中字段
//目标:WHERE (emp_salary>? AND emp_age
?) //1.创建Example 对象 Example example = new Example(Employee.class); //* //i.设置排序信息 example.orderBy("empSalary").asc().orderBy("empAge").desc(); //ii.设置“去重” example.setDistinct(true); //iii.设置select 字段 example.selectProperties("empName","empSalary"); //* //2.通过Example 对象创建Criteria 对象 Criteria criteria01 = example.createCriteria(); Criteria criteria02 = example.createCriteria(); //3.在两个Criteria 对象中分别设置查询条件 //property 参数:实体类的属性名 //value 参数:实体类的属性值 criteria01.andGreaterThan("empSalary", 3000) .andLessThan("empAge", 25); criteria02.andLessThan("empSalary", 5000) .andGreaterThan("empAge", 30); //4.使用OR 关键词组装两个Criteria 对象 example.or(criteria02); //5.执行查询 List
empList = employeeService.getEmpListByExample(example); for (Employee employee : empList) { System.out.println(employee); }
mappers=com.atguigu.mapper.mine_mappers.MyMapper
tk.mybatis.mapper.additional.insert.InsertListMapper
tk.mybatis.mapper.additional.insert.InsertListProvider
UPDATE tabple_emp SET emp_name=?,emp_age=?,emp_salary=? where emp_id=? ; UPDATE tabple_emp SET emp_name=?,emp_age=?,emp_salary=? where emp_id=? ; UPDATE tabple_emp SET emp_name=?,emp_age=?,emp_salary=? where emp_id=? ; ……
UPDATE tabple_emp SET emp_name=#{record.empName}, emp_age=#{record.empAge}, emp_salary=#{record.empSalary} where emp_id=#{record.empId}
@CacheNamespace public interface EmployeeMapper extends MyMapper
{ }
public interface TypeHandler{ //将parameter 设置到ps 对象中,位置是i //在这个方法中将parameter 转换为字符串 void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; //根据列名从ResultSet 中获取数据,通常是字符串形式 //将字符串还原为Java 对象,以T 类型返回 T getResult(ResultSet rs, String columnName) throws SQLException; T getResult(ResultSet rs, int columnIndex) throws SQLException; T getResult(CallableStatement cs, int columnIndex) throws SQLException; } 10.2.3 继承树 10.2.4 BaseTypeHandler 类中的抽象方法说明 //将parameter 对象转换为字符串存入到ps 对象的i 位置 public abstract void setNonNullParameter( PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException; //从结果集中获取数据库对应查询结果 //将字符串还原为原始的T 类型对象 public abstract T getNullableResult( ResultSet rs, String columnName) throws SQLException; public abstract T getNullableResult( ResultSet rs, int columnIndex) throws SQLException; public abstract T getNullableResult( CallableStatement cs, int columnIndex) throws SQLException; 10.2.5 自定义类型转换器类 public class AddressTypeHandler extends BaseTypeHandler { ……
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207223.html原文链接:https://javaforall.net
