想要学会一个框架,仅仅只有足够的理论知识一定是不够的,实践才是唯一的道理,下面就来看看ibatis框架该怎么测试使用吧。
首先当然是准备我们的基础配置文件啦,缺什么都不能缺配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <properties resource="jdbc.properties" /> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${jdbc.driverClassName}" /> <property name="JDBC.ConnectionURL" value="${jdbc.url}" /> <property name="JDBC.Username" value="${jdbc.username}" /> <property name="JDBC.Password" value="${jdbc.password}" /> </dataSource> </transactionManager> <sqlMap resource="ibatis/resources/User.xml" /> </sqlMapConfig>
再来就是对应实体pojo类的配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap> <typeAlias alias="User" type="ibatis.model.User" /> <select id="getAllUsers" resultClass="User"> select * from pf_customer where is_delete='0' </select> <select id="findUsers" resultClass="User"> select * from pf_customer where is_delete='0' and name like '$keyword$' </select> <update id="deleteUser"> update pf_customer set is_delete=1 where id=#id# </update> </sqlMap>
最后编写java代码测试
package ibatis; import ibatis.model.User; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class IBatisDemo { public static void main(String[] args) throws IOException, SQLException { String config = "SqlMapConfig.xml"; Reader reader = Resources.getResourceAsReader(config); SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); //String id="1"; //sqlMap.update("deleteUser", id); List < User > list = sqlMap.queryForList("getAllUsers"); for (User user: list) { System.out.println(user); } } }
运行成功,ibatis成功连接数据库。
以上就是关于ibatis框架测试使用的所有内容,更多java架构师相关信息务必记得关注我们了解详情。
推荐阅读: