mybatis01
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
mybatis示例
1.porm.xml
<dependencies>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis
</artifactId>
<version>3.4.5
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>5.1.6
</version>
</dependency>
<dependency>
<groupId>log4j
</groupId>
<artifactId>log4j
</artifactId>
<version>1.2.12
</version>
</dependency>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>4.12
</version>
</dependency>
<dependency>
<groupId>org.projectlombok
</groupId>
<artifactId>lombok
</artifactId>
<version>1.16.20
</version>
<scope>provided
</scope>
</dependency>
</dependencies>
2.创建实体类和dao接口
3.创建SqlMapConfig
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cc/mybatis/dao/UserDao.xml"></mapper>
</mappers>
</configuration>
4.创建映射配置文件UserDao.xml
<mapper namespace="com.cc.mybatis.dao.UserDao">
<select id="findAll" resultType="com.cc.mybatis.domain.User">
select * from user
</select>
</mapper>
5.操作数据库
InputStream in
= Resources
.getResourceAsStream("SqlMapConfig");
SqlSessionFactoryBuilder builder
= new SqlSessionFactoryBuilder();
SqlSessionFactory factory
= builder
.build(in
);
SqlSession session
= factory
.openSession();
UserDao userDao
= session
.getMapper(UserDao
.class);
List
<User> users
= userDao
.findAll();
for (User user
: users
) {
System
.out
.println(user
);
}
session
.close();
in
.close();
查询结果
mybatis注解方式
删除UserDao.xml
在UserDao添加
@Select("select * from user")
修改SqlMapperConfig
将
<mappers>
<mapper resource="com/cc/mybatis/dao/UserDao.xml"></mapper>
</mappers>
替换为
<mappers>
<mapper class="com.cc.mybatis.dao.UserDao"></mapper>
</mappers>
遇到的问题
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.cc.mybatis.dao.UserDao.findAll'. It's likely that neither a Result Type nor a Result Map was specified.
### The error may exist in com/cc/mybatis/dao/UserDao.xml
### The error may involve com.cc.mybatis.dao.UserDao.findAll
### The error occurred while handling results
### SQL: select * from user
### Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.cc.mybatis.dao.UserDao.findAll'. It's likely that neither a Result Type nor a Result Map was specified.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy2.findAll(Unknown Source)
at com.cc.mybatis.test.MyBatisTest.main(MyBatisTest.java:33)
Caused by: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.cc.mybatis.dao.UserDao.findAll'. It's likely that neither a Result Type nor a Result Map was specified.
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.validateResultMapsCount(DefaultResultSetHandler.java:287)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:189)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 6 more
Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement ‘com.cc.mybatis.dao.UserDao.findAll’. It’s likely that neither a Result Type nor a Result Map was specified.
没有找到查询封装的对象的返回类型
UserDao.xml添加resultType=“com.cc.mybatis.domain.User”