mybatis sql最多能查询多少数据

2020-04-29 科技 78阅读
搭建MyBatis开发环境,实现用户表记录数查询
1、在MyEclipse中创建工程,导入MyBatis的jar包
2、创建MyBatis配置文件mybatis-config.xml配置数据库信息
mybatis-config.xml文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">



</typeAliases>



</transactionManager>






</dataSource>
</environment>
</environments>



</mappers>
</configuration>
jdcb.properties文件(com.mysql.jdbc.Driver 一定要大写)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/testdb
username=root
password=bdqn
mybatis-config.xml的头文件:去dbf的文件开始位置找
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
a、复制mybatis-3.2.2\org\apache\ibatis\builder\xml下的2个文件(如下)放到你想要的地方(我直接放工程里去)
b、然后在window-->prefenence-->xml-->xml catalog-->add-->config.dtd这个文件,然后key写-//mybatis.org//DTD Config 3.0//EN-->确认后就可以正常使用这个文件了
3、编写实体类User
package cn.bdqn.pojo;
public class User {
private Integer id;
private String username;
private String userCode;
private String userPassword;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
测试类:
public class UserTest {
private Logger logger = Logger.getLogger(UserTest.class);
@Test
public void countTest() {
String resource = "mybatis-config.xml";
int count = 0;
SqlSession sqlSession = null;
try {
//1、读取配置文件(mybatis-config.xml)的输入流
//引入包,是ibatis的包
InputStream is = Resources.getResourceAsStream(resource);
System.out.println("is======"+is);
//2、创建sqlSessionFactory对象,完成对配置文件的读取(application作用域,创建完就没必要销毁)--一个就够了(单例)
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
System.out.println("factory======"+factory);
//3、创建sqlSession对象(request作用域,用完后要关闭)
sqlSession = factory.openSession();
System.out.println("sqlSession====="+sqlSession);
System.out.println( sqlSession.selectOne("cn.bdqn.dao.UserMapper.count"));
//4、调用mapper文件进行数据操作(调用之前必须要把mapper文件加入到mybatis-config.xml)
count = sqlSession.selectOne("cn.bdqn.dao.UserMapper.count");
System.out.println("count===="+count);
//打日志用log4j.properties
logger.debug("countTest--->"+count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
sqlSession.close();
}
Assert.assertEquals(6, count);
}
4、配置映射文件UserMapper.xml
a、头文件 :搜索:sqlsession就可以找到探索映射sql语句下的头文件
b、然后在window-->prefenence-->xml-->xml catalog-->add-->mapper.dtd这个文件,然后key写-//mybatis.org//DTD Mapper 3.0//EN-->确认后就可以正常使用这个文件了

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">