spring orm + ibatis + mysql 入门教程,ormibatis,ibatis 需要手动编
spring orm + ibatis + mysql 入门教程,ormibatis,ibatis 需要手动编
ibatis 需要手动编写sql语句,这在麻烦的同时也为性能优化sql提供了很大的便利。本文 我们看下spring + ibatis的开发步骤。
首先需要创建一个maven项目,然后按照如下方式配置pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.outofmemory</groupId> <artifactId>hellspring.transaction</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hellspring.transaction</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-version>3.0.0.RC2</spring-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <!-- spring orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring-version}</version> </dependency> <!-- dbcp 连接池--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.13</version> </dependency> <!-- ibatis --> <dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.ibatis-sqlmap</artifactId> <version>2.3.4.726_4</version> </dependency> </dependencies></project>
如你所见在pom中需要添加多个依赖的jar包,首先是spring的核心jar包spring-context和spring-orm,以及dbcp连接池jar,mysql和ibatis的jar包。
第二步需要配置spring相关的配置文件。
首先要在application.properties文件中配置数据库访问的相关参数,如下:
#mysql driverjdbc.driver=com.mysql.jdbc.Driver#mysql userName and passwordjdbc.username=rootjdbc.password=root#数据库Ajdbc.customerUrl=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull#数据库B#jdbc.centerUrl = jdbc:mysql://127.0.0.1:3306/db_test_b?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull#dbcp settingsdbcp.maxIdle=5dbcp.maxActive=10
然后配置spring的bean,配置内容和配置的注释如下:
<?xml version="1.0" encoding="utf-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- properties config inject --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <!-- standard config --> <value>classpath*:application.properties</value> </list> </property> </bean> <!--datasource config and connection pool --> <bean id="dataSource4A" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.customerUrl}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="true" /> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <!-- iBatis --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource" ref="dataSource4A" /> <property name="configLocation"> <value>classpath:sqlMapConfigA.xml</value> </property> </bean> <!-- bean annotation driven --> <context:annotation-config /> <context:component-scan base-package="cn.outofmemory.hellspring.transaction" > </context:component-scan></beans>
在上面的配置文件中我们配置了dataSource和sqlMapClient,dataSource在sqlMapClient中使用,而sqlMapClient会自动注入到Dao层的类中。
假定我们要为Customer写一个dao层的类,customer有三个字段分别为id 自增长的int,name为varchar,birthday为date类型,其ddl sql如下:
CREATE TABLE `customer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `birthday` date NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
如下是CustomerDao的实现:
package cn.outofmemory.hellspring.transaction;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;import org.springframework.stereotype.Repository;import com.ibatis.sqlmap.client.SqlMapClient;@Repositorypublic class CustomerDao extends SqlMapClientDaoSupport { @Autowired public CustomerDao(SqlMapClient sqlMapClient) { super.setSqlMapClient(sqlMapClient); } public Integer insertCustomer(Customer customer) { return (Integer)this.getSqlMapClientTemplate().insert("Customer.insert", customer); } public Customer getCustomer(Integer id) { return (Customer) this.getSqlMapClientTemplate().queryForObject("Customer.selectById", id); }}
这个类从SqlMapClientdaoSupport继承,以便提供dao orm的支持,类上有Repository的注解,并且在其构造函数上有@Autowired的注解,自动将sqlMapClient类注入给构造函数。
在insertCustomer的方法中调用了this.getSqlMapClientTemplate().insert("Customer.insert", customer);
这一行调用了父类的sqlMapClientTemplate实例的insert方法,insert方法的第一个参数是ibatis配置文件中配置的命名空间 + sqlId,第二个参数是传递给ibatis的参数。
这里涉及到ibatis的配置文件,我们需要在src/main/resources目录下创建一个名称为sqlMapConfigA.xml的xml文件,这个文件在spring的配置文件中定义了。此文件的内容如下:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig> <!-- Settings各节点的说明: cacheModelsEnable :是否启用SqlMapClient上的缓存机制,建议设为true; enhancementEnabled:是否针对POJO启用字节码增加机制以提升getter/setter的调用效能,避免使用Java reflect所带来的性能开销, 同时也为Lazy Loading带来了极大的性能提升,建议设置为true; lazyLoadingEnabled:是否启用延迟加载机制,建议设为"true"; errorTracingEnabled:是否启用错误日志,在开发期间建议设为"true" 以方便调试; maxRequests:最大并发请求数(Statement并发数); maxSessions:最大Session 数。即当前最大允许的并发SqlMapClient数。maxSessions设定必须介于maxTransactions和maxRequests之间, 即maxTransactions<maxSessions=<maxRequests; maxTransactions:最大并发事务数; useStatementNamespaces:是否使用Statement命名空间。这里的命名空间指的是映射文件中,sqlMap节点的namespace属性,如针对users表的映射文件sqlMap节点: <sqlMap namespace="Users">这里,指定了此sqlMap节点下定义的操作均从属于"Users"命名空间。在useStatementNamespaces="true"的情况 下,Statement调用需追加命名空间,如:sqlMap.update("Users.updateUser",user);否则直接通过Statement名称调用即可,如: sqlMap.update("updateUser",user);但请注意此时需要保证所有映射文件中,Statement定义无重名。 --> <settings cacheModelsEnabled="false" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="false" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="true"/> <sqlMap resource="cn/outofmemory/hellospring/dao/Customer.xml"/></sqlMapConfig>
这个文件中的settings节点配置了ibatis的一些配置,sqlMap节点配置了引入的sql配置文件的xml。这里我们只是引入了一个xml文件,在这些引入的xml文件中会配置sql语句,我们看下Customer.xml文件的内容。
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap namespace="Customer"> <typeAlias alias="Customer" type="cn.outofmemory.hellspring.transaction.Customer" /> <insert id="insert" parameterClass="Customer"> insert into `customer` (`name`,`birthday`) values (#name#,#birthday#) <selectKey keyProperty="id" resultClass="Integer"> select LAST_INSERT_ID() </selectKey> </insert> <select id="selectById" parameterClass="Integer" resultClass="Customer"> select * from `customer` where id = #id# </select></sqlMap>
这个文件的根节点是sqlMap,在这个节点上指定了其所在的命名空间即namespace属性;typeAlias节点指定了类的别名,显然这是为了避免每次都要写上完整的package;然后是sql的定义:在ibatis中有四种sql分别为select , insert,update,和delete,在我们这个例子中第一个sql是一个insert的xml节点,这个节点中首先指定了id,然后指定了parameterClass参数类型,在节点的内部首先是插入的sql语句,用#varname#,来做sql参数的展位符,varname必须是parameterClass的有效属性名, 对于insert来说还需要selectKey节点,用来返回插入后的自增长字段的值。
select语句也需要指定id和参数名以及返回值的类型。
ibatics的sql写法有很多技巧,请参考:
http://byrx.net/code-snippet/80/16-ibatis-changyong-SQL-statement-code
您可以下载本文的源码
用户点评