单表的crud操作【字段多一些】 employees表
条件查询
public List findByConds1(String firstname,String lastname,int minsalary,int maxlary);
public List findByConds2(Map map);
public List findByConds3(@Param(“firstname”)String firstname,@Param(“lastname”)String lastname,int minsalary,int maxlary);
public List findByConds4(EmployeesVo vo,int start,int pageSize);
动手能力强的同学,可以将之前的项目中的dao层更改为mybatis的实现。
结构:
Employees:
import lombok.Data;
import java.sql.Date;
@Data
public class Employees {
private int employeeId;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String jobId;
private Double salary;
private Double commissionPct;
private Integer managerId;
private Integer departmentId;
private Date hiredate;
}
Employeesvo:
import lombok.Data;
@Data
public class Employeesvo {
private String firstName;
private String lastName;
private Integer minSalary;
private Integer maxSalary;
}
mybatis-config.xml:
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<setting name="mapUnderscoreToCamelCase" value="true" />
settings>
<typeAliases>
<package name="com.cdcas.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///jspzzx?useSSl=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true"/>
<property name="username" value="root"/>
<property name="password" value="redhat"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.cdcas.mapper"/>
mappers>
configuration>
EmployeesMapper接口:
import com.cdcas.pojo.Employees;
import com.cdcas.pojo.Employeesvo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface EmployeesMapper {
List<Employees> findByConds1(String firstName, String lastName, int minSalary, int maxSalary);
List<Employees> findByConds2(Map<String,Object> map);
List<Employees> findByConds3(@Param("firstName") String firstName, @Param("lastName") String lastName, int minSalary, int maxSalary);
List<Employees> findByConds4(Employeesvo vo, int start, int pageSize);
}
EmployeesMapper.xml:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cdcas.mapper.EmployeesMapper">
<select id="findByConds1" resultType="com.cdcas.pojo.Employees">
select * from employees where first_name like #{param1} and last_name like #{param2}
and salary>=#{param3} and salary <=#{param4}
select>
<select id="findByConds2" resultType="com.cdcas.pojo.Employees">
select * from employees where first_name like #{firstName} and last_name like #{lastName}
and salary>=#{minSalary} and salary <=#{maxSalary}
select>
<select id="findByConds3" resultType="com.cdcas.pojo.Employees">
select * from employees where first_name like #{firstName} and last_name like #{lastName}
and salary>=#{param3} and salary <=#{param4}
select>
<select id="findByConds4" resultType="com.cdcas.pojo.Employees">
select * from employees where first_name like #{param1.firstName} and last_name like #{param1.lastName}
and salary>=#{param1.minSalary} and salary <=#{param1.maxSalary} limit #{param2}, #{param3}
select>
mapper>
之前的:
dao层和pojo单独抽成一个模块:
通过maven模块可以相互调用。和导入jar一样