本次博客主要介绍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}" /> |
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 = new SqlSessionFactoryBuilder().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()); |
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文件
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