这里我们先说最常见的insert 、update 、delete 、select
bean public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department department; public Employee() { super(); // TODO Auto-generated constructor stub } public Employee(Integer id, String lastName, String email, String gender, Department department) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department=" + department + "]"; } } Dao接口 在使用mybatis时我们不用为Dao层写实现类,通过配置文件来实现类中的方法。 public interface EmployeeMapper { /** * 通过id查询单个员工 * @param id */ public Employee queryEmpById(int id); /** * 查询所有员工 */ public List<Employee> queryEmpAll(); /** * 添加员工 * @param id */ public void addEmp(Employee emp); /** * 修改员工信息 * @param id */ public void updateEmp(Employee emp); /** * 通过id删除员工 * @param id */ public void delEmp(int id); } 配置文件 mybatis_config.xml 核心配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引入外部文件 --> <properties resource="db.properties"></properties> <settings> <!-- 配置驼峰命名法(将数据库中的_命名方式改为驼峰命名法) --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 为该包下每一个类创建一个默认的别名,就是简单类名小写--> <typeAliases> <package name="com.atguigu.mybatis.bean"/> </typeAliases> <!-- 数据库连接环境的配置 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </dataSource> </environment> </environments> <!-- 引入SQL映射文件,Mapper映射文件 --> <mappers> <!-- 这里我们使用class,前提是dao层的配置文件必须有dao层接口文件名相同,并在同一个包下--> <mapper class="com.atguigu.mybatis.dao.EmployeeMapper" /> </mappers> </configuration> EmployeeMapper.xml SQL映射文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 命名空间(全类名) --> <mapper namespace="com.atguigu.mybatis.dao.EmployeeMapper"> <!-- id的值为dao层接口的方法名,resultType是返回值类型 --> <select id="queryEmpById" resultType="Employee"> select * from employee where id = #{id} </select> <!-- public List<Employee> queryEmpAll(); --> <select id="queryEmpAll" resultType="Employee"> select * from employee </select> <!-- public void addEmp(Employee emp); --> <insert id="addEmp"> insert into employee values(null,#{lastName},#{email},#{gender}) </insert> <!-- public void updateEmp(int id); --> <update id="updateEmp"> update employee set last_name=#{lastName},gender=#{gender} where id=#{id} </update> <!-- public void delEmp(int id); --> <delete id="delEmp"> delete from employee where id=#{id} </delete> </mapper> 测试类 增删改必须进行提交,使用session.commit();或factory.openSession(true); public class Main { static SqlSessionFactory factory; static { try { InputStream inputStream = Resources.getResourceAsStream("mybatis_config.xml"); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); //读取配置文件 factory = factoryBuilder.build(inputStream); } catch (Exception e) { e.printStackTrace(); } } @Test public void select() { //创建session工厂 SqlSession session = factory.openSession(); //获取代理对象 EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); Employee employee = employeeMapper.queryEmpById(1); System.out.println(employee); List<Employee> queryEmpAll = employeeMapper.queryEmpAll(); System.out.println(queryEmpAll); } @Test public void update() { //增删改必须进行提交,使用session.commit();或factory.openSession(true); SqlSession session = factory.openSession(); EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class); //增 /*Employee employee = new Employee(null,"王五","147@163.com","男"); employeeMapper.addEmp(employee); session.commit();*/ //删 employeeMapper.delEmp(1); session.commit(); //改 /*Employee employee = new Employee(1,"张三",null,"男"); employeeMapper.updateEmp(employee); session.commit();*/ } }指定返回值类型resultType
结果会自动封装为 resultType中的类型 <select id="queryMaps" resultType="java.util.HashMap"> select * from employee </select>此时我们更改bean,使bean的结构边复杂
public class Employee { private Integer id; private String lastName; private String email; private String gender; private Department department; public Employee() { super(); // TODO Auto-generated constructor stub } public Employee(Integer id, String lastName, String email, String gender, Department department) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department=" + department + "]"; } } public class Department { private Integer did; private String departmentName; public Department() { super(); // TODO Auto-generated constructor stub } public Department(Integer did, String departmentName) { super(); this.did = did; this.departmentName = departmentName; } public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [did=" + did + ", departmentName=" + departmentName + "]"; } } 自定义映射 此时查询employee信息并查出employee对应的department信息,放入employee对象中 <resultMap type="Employee" id="resultMap"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <association property="department" javaType="Department"> <id column="did" property="did"/> <result column="department_name" property="departmentName"/> </association> </resultMap> <select id="getEmpById" resultMap="resultMap"> select e.*,d.* from employee e,department d where e.did=d.did and e.id=#{id} </select>虽然上述的employee类中包含了department类,但是有时候我们可能只需要employee信息,暂时不需要对应的department信息,我们想节约资源,等我们需要对应的department信息时在查询department
按照以前的思维,我们会将一条查询语句拆为两条查询语句,当我们需要department时,在通过employee的信息去查询department,但这样的话我们需要分开写两次调用查询的语句,mybatis中为我们提供了更好的方法:分布查询 select * from employee where id=#{id} select * from department where did=#{did}