生活随笔
收集整理的這篇文章主要介紹了
mybatis的环境搭建及如何和搭配spring使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本次博客主要介紹mybatis的環境搭建及如何和搭配spring使用,關于動態sql的部分可能會放在后面找一個專題來寫。建議要有一定的ibatis的基礎
1maven組織結構所需要的jar包
?
| 02 | ????????????<groupId>org.mybatis</groupId> |
| 03 | ????????????<artifactId>mybatis</artifactId> |
| 04 | ????????????<version>3.2.0</version> |
| 05 | ????????????<classifier>sources</classifier> |
| 08 | ????????????<groupId>org.mybatis</groupId> |
| 09 | ????????????<artifactId>mybatis</artifactId> |
| 10 | ????????????<version>3.2.0</version> |
| 14 | ????????????<groupId>log4j</groupId> |
| 15 | ????????????<artifactId>log4j</artifactId> |
| 16 | ????????????<version>1.2.15</version> |
| 19 | ????????????<groupId>mysql</groupId> |
| 20 | ????????????<artifactId>mysql-connector-java</artifactId> |
| 21 | ????????????<version>5.1.16</version> |
2 mybatis的配置文件
?
?
| 02 | ???????<properties?resource="mysql.properties"></properties> |
| 03 | ????<environments?default="development"> |
| 04 | ????????<environment?id="development"> |
| 05 | ????????????<transactionManager?type="JDBC"?/> |
| 06 | ????????????<dataSource?type="POOLED"> |
| 07 | ????????????????<property?name="driver"?value="${driver}"?/> |
| 08 | ????????????????<property?name="url"?value="${url}"?/> |
| 09 | ????????????????<property?name="username"?value="${username}"?/> |
| 10 | ????????????????<property?name="password"?value="${password}"?/> |
| 11 | ????????????</dataSource> |
| 15 | ????????<mapper?resource="org/mybatis/example/BlogMapper.xml"?/> |
其中configuration是根節點,其中properties節點引用了配置文件,setting節點用來做一些性能配置,environment節點用來配置數據庫的環境,其中每一個環境對應了一個sqlsessionfactory,mapper節點主要對應著項目的mybatis的mapper文件
?
enviroments可以配置事務管理器,主要有兩種類型,一種是JDBC,另一種是managed,www.1111kp.info, www.6699ysk.info, www.aaafaipiao.com,?
datasource用來配置數據源,其有三種類型,unpooled(每次查詢都打開關閉連接),pooled(連接池),JNDI。
接下來是mybatis的mapper文件
?
| 1 | <mapper?namespace="org.mybatis.example.BlogMapper"> |
| 2 | ??<select?id="selectBlog"?parameterType="int"?resultType="org.mybatis.example.Blog"> |
| 3 | ????select * from Blog where id = #{id} |
其中幾個主要的元素有select,update,delete,insert,sql,cache,resultmap
?
?
| 01 | public?static?void?main(String[] args)?throws?IOException { |
| 02 | ????????String resource =?"org/mybatis/example/mybatis-config.xml"; |
| 03 | ????????InputStream inputStream = Resources.getResourceAsStream(resource); |
| 04 | ????????SqlSessionFactory sqlSessionFactory =?newSqlSessionFactoryBuilder().build(inputStream); |
| 05 | ????????SqlSession session = sqlSessionFactory.openSession(); |
| 07 | ??????????Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog",?1); |
| 08 | ??????????System.out.println("blog.name="+blog.getName()); |
| 10 | ??????????session.close(); |
java的中調用如上面的代碼,首先獲取到mybatis的配置文件,然后獲取到其中的sqlsessionfactory,通過sqlsessionfactory獲取到sqlsession,接下來用sqlsession來執行查詢語句。www.bbbkp123.info, www.fp1111.info, www.fp1234.info, www.fpfuzhou.com
?
另外mybatis和spring也有很好的結合,如果在mybatis中使用spring的話,需要在maven中額外的引入jar包
?
| 02 | ????????????<groupId>org.mybatis</groupId> |
| 03 | ????????????<artifactId>mybatis-spring</artifactId> |
| 04 | ????????????<version>1.1.1</version> |
| 05 | ????????????<classifier>sources</classifier> |
| 08 | ????????????<groupId>org.mybatis</groupId> |
| 09 | ????????????<artifactId>mybatis-spring</artifactId> |
| 10 | ????????????<version>1.1.1</version> |
?
spring的配置文件如下:
?
| 02 | ????<context:property-placeholder?location="classpath:mysql.properties"?/> |
| 04 | ????<bean?id="dataSource"?class="org.springframework.jdbc.datasource.DriverManagerDataSource"> |
| 05 | ????????<property?name="driverClassName"?value="${driver}"?/> |
| 06 | ????????<property?name="url"?value="${url1}"?/> |
| 09 | ????<bean?id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| 10 | ????????<property?name="dataSource"?ref="dataSource"?/> |
| 13 | ????<bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean"> |
| 14 | ????????<property?name="configLocation"?value="classpath:org/mybatisspring/test/mybatis-config.xml"?/> |
| 15 | ????????<property?name="dataSource"?ref="dataSource"?/> |
這里配置了數據源,事務管理器和mybatis的sqlsessionfactory
?
接下來是如何配置mapper文件
?
| 1 | <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">--> |
| 2 | ????????<!--<property name="annotationClass" value="org.springframework.stereotype.Repository"/>--> |
| 3 | ????????<!--<property name="basePackage" value="org.mybatisspring.test"/>--> |
| 4 | ????????<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>--> |
| 7 | ????<bean?id="studentMapper"?class="org.mybatis.spring.mapper.MapperFactoryBean"> |
| 8 | ????????<property?name="mapperInterface"?value="org.mybatisspring.test.StudentMapper"?/> |
| 9 | ????????<property?name="sqlSessionFactory"?ref="sqlSessionFactory"?/> |
采用第二種的配置方法的話,可以把mapper文件映射成一個java接口。接口中定義了mapper中的實現方法
?
?
| 3 | public?interface?StudentMapper { |
| 4 | ????public?StudentEntity getStudent(String studentID); |
具體是測試實現方法如下
?
?
| 1 | public?static?void?main(String args[]){ |
| 2 | ????????ApplicationContext ac=new?FileSystemXmlApplicationContext("applicationcontext.xml"); |
| 3 | ????????StudentMapper studentMapper=? ac.getBean(StudentMapper.class); |
| 4 | ????????StudentEntity entity = studentMapper.getStudent("123123"); |
| 5 | ????????System.out.println("name:"?+ entity.getStudentName()); |
首先獲得spring的配置文件,然后拿到mapper類,并表用mapper類的對應方法
?
總結一下。本文主要介紹了mybatis的使用及如何和spring配合使用。關于mybasit的底層實現和動態sql我準備以后單獨在準備一個博客來寫。
?
?
?
http://my.oschina.net/u/947963/blog
?
?
轉載于:https://www.cnblogs.com/linuxsuperstart/archive/2013/03/05/2945161.html
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的mybatis的环境搭建及如何和搭配spring使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。