笔记要点
出错分析与总结
工程组织
1.定义接口
public interface EmployeeMapper { //多条记录封装到一个map中: Map<Integer,Employee>,键是这条记录的主键,值时记录封装后的JavaBean @MapKey("id") //告诉Mybatis 封装Map的时候,使用哪个属性作为map的key,当然要对应你的下一行的Integer public Map<Integer,Employee> getEmpsByLastNameLikeRetMap( String lastName); //select 的返回值类型是Map ,返回一条记录的Map,key是列名,值就是对应的值; public Map<String,Object> getEmpsByIdReturnMap(Integer id); //select 的返回值类型是List public List<Employee> getEmpsByLastNameLike( String lastName); //多个参数处理,传入map public Employee getEmpByMap(Map<String,Object> map); }
2.定义XML映射文件 EmployeeMapper.xml
<?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.dao.EmployeeMapper"><!--namespace: 名称空间;id: 唯一标识; resultType:返回值类型; --> <!--public Map<Integer,Employee> getEmpsByLastNameLikeRetMap( String lastName); 如果返回的是一个集合,resultType要写Map中值 元素的类型:Employee --> <select id="getEmpsByLastNameLikeRetMap" resultType="com.bean.Employee" databaseId="mysql"> select * from tbl_employee where last_name like #{lastName} </select> <!--public Map<String,Object> getEmpsByIdReturnMap(Integer id); 记得更改,resultType="map" --> <select id="getEmpsByIdReturnMap" resultType="map" databaseId="mysql"> select * from tbl_employee where id =#{id} </select> <!--public List<Employee> getEmpsByLastNameLike( String lastName); 如果返回的是一个集合,resultType要写集合中元素的类型; --> <select id="getEmpsByLastNameLike" resultType="com.bean.Employee" databaseId="mysql"> select * from tbl_employee where last_name like #{lastName} </select> <!--public Employee getEmpByMap(Map<String,Object> map);--> <select id="getEmpByMap" resultType="com.bean.Employee" databaseId="mysql"> select * from ${table_name} where id = ${id} and last_name = #{lastName} </select>
3.编写测试代码
public class test_tp26 { public SqlSessionFactory getSqlSessionFactory() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream=Resources.getResourceAsStream(resource); return new SqlSessionFactoryBuilder().build(inputStream); } @Test public void test06() throws Exception{ //默认是不自动提交数据的,需要我们自己手动提交 SqlSession openSession = getSqlSessionFactory().openSession(); try { EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); //1.select 返回List列表 List<Employee> like = mapper.getEmpsByLastNameLike("%e%"); for(Employee e:like){ System.out.println(e); } System.out.println("*********************"); //2.select 记录封装Map,返回单条记录 Map<String, Object> emps = mapper.getEmpsByIdReturnMap(1); System.out.println(emps); //{gender=1, last_name=jerry, id=1, email=jerry@163.com} System.out.println("*********************"); //3.select 记录封装Map,返回多组记录 Map<Integer, Employee> emps1 = mapper.getEmpsByLastNameLikeRetMap("%e%"); for(Map.Entry m: emps1.entrySet()){ System.out.println(m); } //{gender=1, last_name=jerry, id=1, email=jerry@163.com} System.out.println("*********************"); //最后手动提交 openSession.commit(); }finally { openSession.close(); } } }
测试结果
DEBUG 11-29 16:10:26,474 ==> Preparing: select * from tbl_employee where last_name like ? (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,493 ==> Parameters: %e%(String) (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,505 <== Total: 2 (BaseJdbcLogger.java:145) Employee{id=1, lastname='jerry', email='jerry@163.com', gender='1'} Employee{id=5, lastname='葫芦娃e', email='qwq@qq.com', gender='1'} ********************* DEBUG 11-29 16:10:26,506 ==> Preparing: select * from tbl_employee where id =? (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,506 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,507 <== Total: 1 (BaseJdbcLogger.java:145) {gender=1, last_name=jerry, id=1, email=jerry@163.com} ********************* DEBUG 11-29 16:10:26,508 ==> Preparing: select * from tbl_employee where last_name like ? (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,508 ==> Parameters: %e%(String) (BaseJdbcLogger.java:145) DEBUG 11-29 16:10:26,509 <== Total: 2 (BaseJdbcLogger.java:145) 1=Employee{id=1, lastname='jerry', email='jerry@163.com', gender='1'} 5=Employee{id=5, lastname='葫芦娃e', email='qwq@qq.com', gender='1'} *********************