测试ibatis3连接数据
User.java文件:
package com.bsonline.test.ibatisTest;
public class User {
private Integer id;
private String username;
private String password;
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User_SqlMap.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="User">
<select id="getUser" parameterType="java.lang.Integer" resultType="user">
select * from system where id=#{id}
</select>
</mapper>
SqlMapConfig.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<properties resource="com/bsonline/test/ibatisTest/jdbc.properties" />
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="lazyLoadingEnabled" value="true" />
<setting name="multipleResultSetsEnabled" value="false" />
<setting name="useColumnLabel" value="true" />
<setting name="defaultExecutorType" value="SIMPLE" />
</settings>
<typeAliases>
<typeAlias type="com.bsonline.test.ibatisTest.User" alias="user"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bsonline/test/ibatisTest/User_SqlMap.xml"/>
</mappers>
</configuration>
jdbc.properties文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/bsonline
username=root
password=123456
IbatisTest.java文件:
package com.bsonline.test.ibatisTest;
imp
imp
imp
imp
imp
imp
public class IbatisTest {
/**
*@author: zhangjp
*@功能:testIBATIS
*@公用:false
*@date:Apr 30, 2010 6:56:30 PM
**/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String resource ="com/bsonline/test/ibatisTest/SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
User user = (User)session.selectOne("getUser", 1);
System.out.println(user.getId()+" "+user.getUsername()+" "+user.getPassword());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}