1.基础的增删改查
EmployeeMapper.java
public interface EmployeeMapper {
public Employee getEmployeeID(Integer id);
public boolean insertEmployee(Employee employee);
public boolean updateEmployee(Employee employee);
public boolean deleteEmployeeID(Integer id);
}
EmployeeMapper.xml中的mapper部分
<mapper namespace="dao.EmployeeMapper">
<select id="getEmployeeID" resultType="bean.Employee" databaseId="mysql">
select * from employee where id = #{id}
</select>
<insert id="insertEmployee">
INSERT INTO employee(name,sex,email) VALUES (#{name},#{sex},#{email})
</insert>
<update id="updateEmployee">
UPDATE employee SET name=#{name},sex=#{sex},email=#{email} WHERE id=#{id}
</update>
<delete id="deleteEmployeeID">
DELETE FROM employee WHERE id=#{id}
</delete>
</mapper>
test函数
/**
* MyBatis允许增删改直接定义Integer、Long、Boolean、void类型的返回值
* @throws IOException
*/
@Test
public void test2() throws IOException {
//1.获取sqlSession
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//openSession(true):设置自动提交
SqlSession sqlSession = sqlSessionFactory.openSession();
//2.获取Mapper
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
//3.执行函数
Employee employee1= new Employee(null,"zzw","男","250@qq.com");
Boolean b1 = employeeMapper.insertEmployee(employee1);
// Employee employee2 = new Employee(1,"litao","女","2125@qq.com");
// Boolean b2 = employeeMapper.updateEmployee(employee2);
//
// Boolean b3 = employeeMapper.deleteEmployeeID(3);
//
// Employee employee4 = employeeMapper.getEmployeeID(1);
//4.手动提交
sqlSession.commit();
//5.关闭资源
sqlSession.close();
}
2.获取自增主键的值
EmployeeMapper.xml中的mapper的insert部分
<!--
useGeneratedKeys="true":使用自增主键获取主键值策略
keyProperty:mybatis获取到主键值封装给javaBean的哪个属性
-->
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="id">
INSERT INTO employee(name,sex,email) VALUES (#{name},#{sex},#{email})
</insert>
test函数的部分
Employee employee1= new Employee(null,"zzz","男","251@qq.com");
Boolean b1 = employeeMapper.insertEmployee(employee1);
System.out.println(employee1); //将主键值直接返回到了id属性中
3.查询返回值为List类型
Mapper接口函数
public List<Employee> getEmployeeName(String name);
Mapper.xml(resultType依然等于Employee,不是List)
<select id="getEmployeeName" resultType="bean.Employee" databaseId="mysql">
select * from employee where name LIKE #{name}
</select>
test函数部分
List<Employee> list = employeeMapper.getEmployeeName("%z%");
System.out.println(list);
4.查询返回值为Map类型
4.1返回值为默认的Map
Mapper接口函数
public Map<String,Object> getEmployeeMap1(Integer id);
Mapper.xml(resultType等于map)
<select id="getEmployeeMap1" resultType="map" databaseId="mysql">
select * from employee where id = #{id}
</select>
test函数部分
Map<String,Object> map = employeeMapper.getEmployeeMap1(1);
System.out.println(map);
4.2返回值为自定义的Map
Mapper接口函数(@MapKey(“id”)指明返回的Map中key的类型)
@MapKey("id")
public Map<Integer,Employee> getEmployeeMap2(String name);
Mapper.xml(resultType等于Employee)
<select id="getEmployeeMap2" resultType="bean.Employee" databaseId="mysql">
select * from employee where name LIKE #{name}
</select>
test函数部分
Map<Integer,Employee> map = employeeMapper.getEmployeeMap2("%z%");
System.out.println(map);