maven 项目 查询部分关心的字段
<?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> <!--节点是有顺序的--> <!--先是jdbc,log4j--> <properties resource="jdbc.properties"></properties> <!-- <settings> <setting name="logImpl" value="LOG4J"/> </settings>--> <settings> <setting name="autoMappingBehavior" value="NONE"/> </settings> <typeAliases>
//别名 使用别名后 pageage name="cn.kitty.bean "后可自动生成与bean 同名类的别名 <package name="cn.kitty.bean"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="mapper/IUserDao.xml"/>-->
//别名 <package name="cn.kitty.dao"></package> </mappers> </configuration>
settings 发现autoMappingBehavior的value的值有NONE,PARTIAL,FULL
NONE : 禁止自动配置
PARTIAL:(默认):自动配置所有的,有内部嵌套(association,collection)的除外。
FULL: 自动配置所有
在小配置-------IUserDao.xml中添加 resultMap 节点
在 select 节点 中 唯一添加标识符 resultMap="定义一个喜欢的名字" 此处与<resultMap id="XXXX "/>相同
工具类 MybatisUtil
public class MybatisUtil { private static InputStream is; public static SqlSessionFactory sqlSessionFactory; static { try { is=Resources.getResourceAsStream("mybaits1-config.xml"); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory =new SqlSessionFactoryBuilder().build(is) ; } private MybatisUtil(){} public static SqlSession getSession(){ return sqlSessionFactory.openSession(); }
}
测试类 UserTest
public class UserTest { @Test public void findAll() throws IOException {//查询 SqlSession session = MybatisUtil.getSession(); List<User> list = session.getMapper(IUserDao.class).findAll(); for (User user : list) { System.out.println(user); } }