mybatis入门

8 篇文章 0 订阅

http://my.oschina.net/zimingforever/blog/111907

这个博客早就想写了。最近在搞mybatis的mapper解析器,所以特意学习并研究了下mybatis,其实mybatis的使用和ibatis大同小异,之前搞了些ibatis的博客,也可以参考一下。这里一个mybatis官方参考文档,是我学习的主要参考:

http://mybatis.github.com/mybatis-3/zh/index.html

http://mybatis.github.com/spring/zh/index.html

本次博客主要介绍mybatis的环境搭建及如何和搭配spring使用,关于动态sql的部分可能会放在后面找一个专题来写。建议要有一定的ibatis的基础

1maven组织结构所需要的jar包

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependency>
             <groupId>org.mybatis< /groupId >
             <artifactId>mybatis< /artifactId >
             <version>3.2.0< /version >
             <classifier>sources< /classifier >
         < /dependency >
         <dependency>
             <groupId>org.mybatis< /groupId >
             <artifactId>mybatis< /artifactId >
             <version>3.2.0< /version >
         < /dependency >
 
         <dependency>
             <groupId>log4j< /groupId >
             <artifactId>log4j< /artifactId >
             <version>1.2.15< /version >
         < /dependency >
         <dependency>
             <groupId>mysql< /groupId >
             <artifactId>mysql-connector-java< /artifactId >
             <version>5.1.16< /version >
         < /dependency >
2 mybatis的配置文件


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< configuration >
        < properties resource = "mysql.properties" ></ properties >
     < 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 = "org/mybatis/example/BlogMapper.xml" />
     </ mappers >
</ configuration >
其中configuration是根节点,其中properties节点引用了配置文件,setting节点用来做一些性能配置,environment节点用来配置数据库的环境,其中每一个环境对应了一个sqlsessionfactory,mapper节点主要对应着项目的mybatis的mapper文件


enviroments可以配置事务管理器,主要有两种类型,一种是JDBC,另一种是managed

datasource用来配置数据源,其有三种类型,unpooled(每次查询都打开关闭连接),pooled(连接池),JNDI。

接下来是mybatis的mapper文件


?
1
2
3
4
5
< mapper namespace = "org.mybatis.example.BlogMapper" >
   < select id = "selectBlog" parameterType = "int" resultType = "org.mybatis.example.Blog" >
     select * from Blog where id = #{id}
   </ select >
</ mapper >
其中几个主要的元素有select,update,delete,insert,sql,cache,resultmap



?
1
2
3
4
5
6
7
8
9
10
11
12
public static void main(String[] args) throws IOException {
         String resource = "org/mybatis/example/mybatis-config.xml" ;
         InputStream inputStream = Resources.getResourceAsStream(resource);
         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         SqlSession session = sqlSessionFactory.openSession();
         try {
           Blog blog = (Blog) session.selectOne( "org.mybatis.example.BlogMapper.selectBlog" , 1 );
           System.out.println( "blog.name=" +blog.getName());
         } finally {
           session.close();
         }
     }
java的中调用如上面的代码,首先获取到mybatis的配置文件,然后获取到其中的sqlsessionfactory,通过sqlsessionfactory获取到sqlsession,接下来用sqlsession来执行查询语句。


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


?
1
2
3
4
5
6
7
8
9
10
11
< dependency >
             < groupId >org.mybatis</ groupId >
             < artifactId >mybatis-spring</ artifactId >
             < version >1.1.1</ version >
             < classifier >sources</ classifier >
         </ dependency >
         < dependency >
             < groupId >org.mybatis</ groupId >
             < artifactId >mybatis-spring</ artifactId >
             < version >1.1.1</ version >
         </ dependency >


spring的配置文件如下:


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- 导入属性配置文件 -->
     < context:property-placeholder location = "classpath:mysql.properties" />
 
     < bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource" >
         < property name = "driverClassName" value = "${driver}" />
         < property name = "url" value = "${url1}" />
     </ bean >
 
     < bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         < property name = "dataSource" ref = "dataSource" />
     </ bean >
 
     < bean id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
         < property name = "configLocation" value = "classpath:org/mybatisspring/test/mybatis-config.xml" />
         < property name = "dataSource" ref = "dataSource" />
     </ bean >
这里配置了数据源,事务管理器和mybatis的sqlsessionfactory


接下来是如何配置mapper文件


?
1
2
3
4
5
6
7
8
9
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">-->
         <!--<property name="annotationClass" value="org.springframework.stereotype.Repository"/>-->
         <!--<property name="basePackage" value="org.mybatisspring.test"/>-->
         <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
     <!--</bean>-->
 
     < bean id = "studentMapper" class = "org.mybatis.spring.mapper.MapperFactoryBean" >
         < property name = "mapperInterface" value = "org.mybatisspring.test.StudentMapper" />
         < property name = "sqlSessionFactory" ref = "sqlSessionFactory" />
采用第二种的配置方法的话,可以把mapper文件映射成一个java接口。接口中定义了mapper中的实现方法



?
1
2
3
4
5
@Repository
@Transactional
public interface StudentMapper {
     public StudentEntity getStudent(String studentID);
}
具体是测试实现方法如下



?
1
2
3
4
5
6
public static void main(String args[]){
         ApplicationContext ac= new FileSystemXmlApplicationContext( "applicationcontext.xml" );
         StudentMapper studentMapper=  ac.getBean(StudentMapper. class );
         StudentEntity entity = studentMapper.getStudent( "123123" );
         System.out.println( "name:" + entity.getStudentName());
     }
首先获得spring的配置文件,然后拿到mapper类,并表用mapper类的对应方法


参考文档:

http://sunfish.iteye.com/blog/1472693

http://limingnihao.iteye.com/blog/781671

总结一下。本文主要介绍了mybatis的使用及如何和spring配合使用。关于mybasit的底层实现和动态sql我准备以后单独在准备一个博客来写。


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值