总结:ibatis对derby数据库操作,以及与spring的集成(ibatis's hello world)
转载请注明出处:http://renjie120.iteye.com/用过ibatis之后,感觉hibernate太复杂.
使用过derby数据库之后,感觉mysql也很大.
helloWorld工程很重要,学习新东西完成helloWorld就心里有底了.
http://dl.iteye.com/upload/attachment/235543/ba146a4f-bae5-352e-b213-463b083d688b.png
闲来无聊,看了一下这些东西,发现derby好好玩.花了几天时间学习了一下基本的ibatis,derby的知识,然后集合了spring框架.
遇到了几个小小的问题,都一一解决了,这里好好总结一下以后再次遇到就不用浪费时间百度了.
1.使用jdbc连接derby数据库的基本语句如下,注意有用户名,密码 的情况,要使用properties文件包含用户名和密码,然后通过properties读值:
private static Properties properties = null;static {// getResourceAsStream的参数"/application.properties"表示以当前类的包的根路径为基准路径 InputStream inputStream = DerbyUtil.class.getResourceAsStream("/derby.properties");properties = new Properties();try {properties.load(inputStream);inputStream.close();} catch (IOException e) {System.out.println("获取系统属性文件异常:");}}/** * 取得数据库连接 * @return */public static Connection getCon() {Connection con = null;String driver = "org.apache.derby.jdbc.EmbeddedDriver";String connectionURL = "jdbc:derby:testIbatis;create=true;";// 装载数据库驱动.try {Class.forName(driver);System.out.println(driver + " loaded. ");} catch (java.lang.ClassNotFoundException e) {System.err.print("ClassNotFoundException: ");System.out.println("缺少derby相关jar包!");}try {con = DriverManager.getConnection(connectionURL, properties);} catch (SQLException e) {System.out.println("出现异常.");errorPrint(e);}return con;} 经常遇到的一个错误是:
--- Cause: java.sql.SQLException: Schema '.......' does not exist
原因就是因为数据库连接的用户名,密码有误!
上面的/derby.properties内容如下:注意用户名,密码的key!开始我写成了use,password(因为直接连接derby的话,配置用户名密码就是用user=;password=),结果找了半天!!
JDBC.Username=adminJDBC.Password=admin
2.使用ibatis 的 sqlMapClient连接数据库的基本方式如下:不论什么数据库都是一样,derby,mysql,oracle都这样写.
public List getDises(Dis argVo) {String resource = "ibatis/SqlMapConfig.xml";SqlMapClient sqlMap = null;List ans = null;try {Reader reader = Resources.getResourceAsReader(resource);sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);} catch (Exception e) {e.printStackTrace();}// sqlMap系统初始化完毕,开始执行getAllUser操作try {sqlMap.startTransaction();ans =sqlMap.queryForList("getDises", argVo);sqlMap.commitTransaction();} catch (SQLException e) {System.out.println(e.getMessage());} finally {try {sqlMap.endTransaction();} catch (SQLException e) {e.printStackTrace();}}return ans;}
3.上面的写法很多都是重复的部分,所以可以使用模板类进行简化书写,要继承SqlMapDaoTemplate这个类,然后还要在ibatis基础配置文件上,再多添加一个配置文件DaoConfig.xml,一般还会要多添加一个类叫做DaoConfig.java.
DaoConfig.xml配置文件一般如下所示:
<daoConfig><context><transactionManager type="SQLMAP"><property name="SqlMapConfigResource"value="ibatis/SqlMapConfig.xml" /></transactionManager><dao interface="ibatis.newTest.IDistrict"implementation="ibatis.dao.NewTestDistrict" /></context></daoConfig>
实现的方式网上很多,但是我遇到了一个很郁闷的问题,找了很久,错误如下:
Exception in thread "main" java.lang.ClassCastException: $Proxy1
at ibatis.dao.NewTestDistrict.main(NewTestDistrict.java:24)
找了半天,原来在写测试类的时候,
DaoManager daoManager = DaoConfig.getDaoManager();
NewTestDistrict test= ( NewTestDistrict ) daoManager.getDao(IDistrict.class);
上面应该是接口的类型,不应该写具体类!!
改成下面就好了:
IDistrict test= (IDistrict ) daoManager.getDao(IDistrict.class);
4.spring和ibatis的集成,网上代码一大把...在demo中也有...
5.demo工程中的内容:
derby文件夹:DerbyUtil:包含derby数据库初始化的方法
ibatis文件夹:
dao文件夹:NewTestDistrict.java 使用ibatis中的模板工具类进行持久化操作
newTest文件夹:TestDistrict.java 使用基本的ibatis语句进行持久化操作
springIbatis文件夹:DistrictSpring.java spring和Ibatis集成测试类
sql文件夹:数据库脚本
6.derby数据库
Derby数据库文件夹:testIbatis文件夹,在第一次运行工程之后,derby数据文件建立在src同级目录.
页:
[1]