日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis的环境搭建及如何和搭配spring使用

發布時間:2025/3/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis的环境搭建及如何和搭配spring使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本次博客主要介紹mybatis的環境搭建及如何和搭配spring使用,關于動態sql的部分可能會放在后面找一個專題來寫。建議要有一定的ibatis的基礎

1maven組織結構所需要的jar包

?

01<dependency>
02????????????<groupId>org.mybatis</groupId>
03????????????<artifactId>mybatis</artifactId>
04????????????<version>3.2.0</version>
05????????????<classifier>sources</classifier>
06????????</dependency>
07????????<dependency>
08????????????<groupId>org.mybatis</groupId>
09????????????<artifactId>mybatis</artifactId>
10????????????<version>3.2.0</version>
11????????</dependency>
12?
13????????<dependency>
14????????????<groupId>log4j</groupId>
15????????????<artifactId>log4j</artifactId>
16????????????<version>1.2.15</version>
17????????</dependency>
18????????<dependency>
19????????????<groupId>mysql</groupId>
20????????????<artifactId>mysql-connector-java</artifactId>
21????????????<version>5.1.16</version>
22????????</dependency>

2 mybatis的配置文件

?

?

01<configuration>
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>
12????????</environment>
13????</environments>
14????<mappers>
15????????<mapper?resource="org/mybatis/example/BlogMapper.xml"?/>
16????</mappers>
17</configuration>

其中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}
4??</select>
5</mapper>

其中幾個主要的元素有select,update,delete,insert,sql,cache,resultmap

?

?

01public?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();
06????????try?{
07??????????Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog",?1);
08??????????System.out.println("blog.name="+blog.getName());
09????????}?finally?{
10??????????session.close();
11????????}
12????}

java的中調用如上面的代碼,首先獲取到mybatis的配置文件,然后獲取到其中的sqlsessionfactory,通過sqlsessionfactory獲取到sqlsession,接下來用sqlsession來執行查詢語句。www.bbbkp123.info, www.fp1111.info, www.fp1234.info, www.fpfuzhou.com

?

另外mybatis和spring也有很好的結合,如果在mybatis中使用spring的話,需要在maven中額外的引入jar包

?

01<dependency>
02????????????<groupId>org.mybatis</groupId>
03????????????<artifactId>mybatis-spring</artifactId>
04????????????<version>1.1.1</version>
05????????????<classifier>sources</classifier>
06????????</dependency>
07????????<dependency>
08????????????<groupId>org.mybatis</groupId>
09????????????<artifactId>mybatis-spring</artifactId>
10????????????<version>1.1.1</version>
11????????</dependency>

?

spring的配置文件如下:

?

01<!-- 導入屬性配置文件 -->
02????<context:property-placeholder?location="classpath:mysql.properties"?/>
03?
04????<bean?id="dataSource"?class="org.springframework.jdbc.datasource.DriverManagerDataSource">
05????????<property?name="driverClassName"?value="${driver}"?/>
06????????<property?name="url"?value="${url1}"?/>
07????</bean>
08?
09????<bean?id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
10????????<property?name="dataSource"?ref="dataSource"?/>
11????</bean>
12?
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"?/>
16????</bean>

這里配置了數據源,事務管理器和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"/>-->
5????<!--</bean>-->
6?
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中的實現方法

?

?

1@Repository
2@Transactional
3public?interface?StudentMapper {
4????public?StudentEntity getStudent(String studentID);
5}

具體是測試實現方法如下

?

?

1public?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());
6????}

首先獲得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使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。