配置文件管理:
在SqlMapConfig.xml文件的相关配置
<?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> <!--IBatis的一些相关配置--> <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="80" maxSessions="80" maxTransactions="80" useStatementNamespaces="true" /> <sqlMap resource="Bean/Student.xml" /> </sqlMapConfig>
struts 配置文件的配置
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- 配置交给Spring管理 视图主题 --> <constant name="struts.ui.theme" value="simple" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.devMode" value="false" /> <constant name="struts.multipart.maxSize" value="10485760" /> <constant name="struts.convention.default.parent.package" value="defaultpackage" /> <constant name="struts.convention.package.locators" value="action" /> <!-- struts动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="test" extends="struts-default"> <action name="addAction" class="Action.AddAction"> <result name="ok">/success.jsp</result> </action> <action name="findAllAction" class="Action.FindAllStudentAction"> <result name="ok">/test.jsp</result> </action> </package> </struts>
与Spring结合所需要的配置 applicationContext.xml
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" > <!--配置数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation"> <value>WEB-INF/app-config/ibatis-config/SqlMapConfig.xml</value> </property> </bean> <!--配置sqlMapClient的模板类sqlMapClientTemplate--> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- ====事务管理 ==== --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>*Service*</value> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> <bean id="studentDao" class="DaoImpl.StudentDaoImpl"> <property name="sqlMapClientTemplate"> <ref bean="sqlMapClientTemplate" /> </property> </bean> <bean id="studentService" class="ServiceImpl.StudentServiceImpl"> <property name="studentDao" ref="studentDao" /> </bean> </beans>
扫描二维码关注公众号,回复:
650440 查看本文章
ibatis实体与表关系映射
实体类
public class Student { private String sno; private String sname; private String sex; private String birthday; private String classString; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getClassString() { return classString; } public void setClassString(String classString) { this.classString = classString; } /*public String toString() { StringBuilder sb = new StringBuilder(); sb.append("sno:").append(getSno()); sb.append("sname:").append(getSname()); sb.append("sex:").append(getSex()); sb.append("birthday:").append(getBirthday()); sb.append("class:").append(getClassString()); return sb.toString(); }*/ }
映射文件Student.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="Student"> <typeAlias alias="Student" type="Bean.Student"/> <insert id="insertStudent" parameterClass="Student"> INSERT INTO student( SNO, SNAME, SSEX, SBIRTHDAY, CLASS ) values ( #sno:VARCHAR:NULL#, #sname:VARCHAR:NULL#, #sex:VARCHAR:NULL#, #birthday:DATETIME#, #classString:VARCHAR:NULL# ) </insert> <select id="selectAllStudent" resultClass="Student"> <![CDATA[ select * from student ]]> </select> </sqlMap>
映射范例
<?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="UserBean"> <typeAlias alias="UserBean" type="com.ysm.user.dao.bean.UserBean" /> <!--javaType="java.lang.String" jdbcType="CHAR"--> <resultMap id="UserBeanRes" class="UserBean"> <result property="userID" column="USERID" /> <result property="userName" column="USERNAME" /> <result property="password" column="PASSWORD" /> <result property="mobile" column="MOBILE" /> <result property="minID" column="MINID" /> <result property="nickName" column="NICKNAME" /> <result property="realName" column="REALNAME" /> <result property="hlr" column="HLR" /> <result property="hlrText" column="HLRTEXT" /> <result property="address" column="ADDRESS" /> <result property="sex" column="SEX" javaType="java.lang.String" jdbcType="CHAR" /> <result property="birthDay" column="BIRTHDAY" /> <result property="constallation" column="CONSTALLATION" /> <result property="bloodType" column="BLOODTYPE" /> <result property="email" column="EMAIL" /> <result property="signature" column="SIGNATURE" /> <result property="profession" column="PROFESSION" /> <result property="companyName" column="COMPANYNAME" /> <result property="compAddress" column="COMPADDRESS" /> <result property="compLocation" column="COMPLOCATION" /> <result property="headImageID" column="HEADIMAGEID" /> <result property="headImageURL" column="HEADIMAGEURL" /> <result property="lastLoginTime" column="LASTLOGINTIME" /> <result property="state" column="STATE" javaType="java.lang.String" jdbcType="CHAR" /> <result property="fancnt" column="FANCNT" /> <result property="attencnt" column="ATTENCNT" /> <result property="visitedcnt" column="VISITEDCNT" /> <result property="commcnt" column="COMMCNT" /> <result property="createTime" column="CREATETIME" /> <result property="oraderState" column="ORADERSTATE" /> <result property="spaceTotal" column="SPACETOTAL" /> <result property="spaceUsed" column="SPACEUSED" /> <result property="getFlag" column="GETFLAG" /> <result property="imei" column="IMEI" /> </resultMap> <select id="countFriend" resultClass="java.lang.Integer" parameterClass="java.lang.Integer" > select count(FRIENDID) from FHPP_FRIEND where USERID = #value# </select> <select id="countPublicShare" resultClass="java.lang.Integer" parameterClass="java.lang.Integer" > select count(SHAREID) from FHPP_PUBLIC_SHARE where USERID = #value# </select> <select id="SelectUserFromUserID" resultMap="UserBeanRes" parameterClass="java.lang.Integer" > select USERID, USERNAME, PASSWORD, MOBILE, MINID, NICKNAME, REALNAME, HLR, HLRTEXT, ADDRESS, SEX, to_char(BIRTHDAY,'YYYY-MM-DD') as BIRTHDAY, CONSTALLATION, BLOODTYPE, EMAIL , SIGNATURE, PROFESSION, COMPANYNAME, COMPADDRESS, COMPLOCATION, HEADIMAGEID, HEADIMAGEURL, to_char(LASTLOGINTIME,'YYYY-MM-DD HH:MM:SS') as LASTLOGINTIME, STATE, FANCNT, ATTENCNT, VISITEDCNT, COMMCNT, to_char(CREATETIME,'YYYY-MM-DD HH:MM:SS') as CREATETIME, ORADERSTATE, SPACETOTAL, SPACEUSED,IMEI, GETFLAG from FHPP_USER_INFO where USERID = #value# </select> <select id="SelectUserFromClass" resultMap="UserBeanRes" parameterClass="java.lang.Integer" > select USERID, USERNAME, PASSWORD, MOBILE, MINID, NICKNAME, REALNAME, HLR, HLRTEXT, ADDRESS, SEX, to_char(BIRTHDAY,'YYYY-MM-DD') as BIRTHDAY, CONSTALLATION, BLOODTYPE, EMAIL , SIGNATURE, PROFESSION, COMPANYNAME, COMPADDRESS, COMPLOCATION, HEADIMAGEID, HEADIMAGEURL, to_char(LASTLOGINTIME,'YYYY-MM-DD HH:MM:SS') as LASTLOGINTIME, STATE, FANCNT, ATTENCNT, VISITEDCNT, COMMCNT, to_char(CREATETIME,'YYYY-MM-DD HH:MM:SS') as CREATETIME, ORADERSTATE, SPACETOTAL, SPACEUSED, IMEI, GETFLAG from FHPP_USER_INFO where USERID in (Select FRIENDID from FHPP_FRIEND where CLASSID = #value#) </select> <select id="SelectUserFromMobile" resultMap="UserBeanRes" parameterClass="java.lang.String" > select USERID, USERNAME, PASSWORD, MOBILE, MINID, NICKNAME, REALNAME, HLR, HLRTEXT, ADDRESS, SEX, to_char(BIRTHDAY,'YYYY-MM-DD') as BIRTHDAY, CONSTALLATION, BLOODTYPE, EMAIL , SIGNATURE, PROFESSION, COMPANYNAME, COMPADDRESS, COMPLOCATION, HEADIMAGEID, HEADIMAGEURL, to_char(LASTLOGINTIME,'YYYY-MM-DD HH:MM:SS') as LASTLOGINTIME, STATE, FANCNT, ATTENCNT, VISITEDCNT, COMMCNT, to_char(CREATETIME,'YYYY-MM-DD HH:MM:SS') as CREATETIME, ORADERSTATE, SPACETOTAL, SPACEUSED, IMEI, GETFLAG from FHPP_USER_INFO where MOBILE = #value# </select> <select id="registerUser" parameterClass="UserBean" resultClass="java.lang.Integer" > select createDefaultData(#minID#,#mobile#,#imei#,#getFlag#) as a from dual </select> <insert id="insertUser" parameterClass="UserBean"> <selectKey resultClass="java.lang.Integer" keyProperty="userID"> SELECT SEQ_USER_ID.Nextval AS USERID FROM DUAL </selectKey> <![CDATA[ INSERT INTO FHPP_USER_INFO ( USERID, USERNAME, PASSWORD, MOBILE, MINID, NICKNAME, REALNAME, HLR, HLRTEXT, ADDRESS, SEX, BIRTHDAY, CONSTALLATION, BLOODTYPE, EMAIL , SIGNATURE, PROFESSION, COMPANYNAME, COMPADDRESS, COMPLOCATION, HEADIMAGEID, HEADIMAGEURL, LASTLOGINTIME, STATE, FANCNT, ATTENCNT, VISITEDCNT, COMMCNT, CREATETIME, ORADERSTATE, SPACETOTAL, SPACEUSED, IMEI, GETFLAG) VALUES( #userID#, #userName#, #password#, #mobile:VARCHAR:NULL#, #minID:VARCHAR:NULL#, #nickName:VARCHAR:NULL#, #realName:VARCHAR:NULL#, #hlr:VARCHAR:NULL#, #hlrText:VARCHAR:NULL#, #address:VARCHAR:NULL#, #sex:VARCHAR:NULL#, #birthDay:TIMESTAMP:NULL#, #constallation:VARCHAR:NULL#, #bloodType:VARCHAR:NULL#, #email:VARCHAR:NULL#, #signature:VARCHAR:NULL#, #profession:VARCHAR:NULL#, #companyName:VARCHAR:NULL#, #compAddress:VARCHAR:NULL#, #compLocation:VARCHAR:NULL#, #headImageID:VARCHAR:NULL#, #headImageID:VARCHAR:NULL#, sysdate, #state#, #fancnt:DECIMAL:NULL#, #attencnt:DECIMAL:NULL#, #visitedcnt:DECIMAL:NULL#, #commcnt:DECIMAL:NULL#, sysdate, #oraderState:VARCHA:NULL# #spaceTotal:DECIMAL:NULL#, #spaceUsed:VARCHAR:NULL#, #imei:VARCHAR:NULL# #getFlag:VARCHAR:NULL# ) ]]> </insert> <update id="updateUserByGetFlag" parameterClass="UserBean"> <![CDATA[ update FHPP_USER_INFO set USERNAME = #userName# ,MINID = #minID# ,GETFLAG = #getFlag# ,IMEI = #imei# where USERID = #userID# ]]> </update> <update id="updateUserByUserID" parameterClass="UserBean"> <![CDATA[ update FHPP_USER_INFO ]]> <dynamic prepend="set" > <isNotNull prepend="," property="userName" > USERNAME = #userName# </isNotNull> <!-- <isNotNull prepend="," property="password" > PASSWORD = #password# </isNotNull> --> <isNotNull prepend="," property="mobile" > MOBILE = #mobile# </isNotNull> <isNotNull prepend="," property="minID" > MINID = #minID# </isNotNull> <isNotNull prepend="," property="nickName" > NICKNAME = #nickName# </isNotNull> <isNotNull prepend="," property="realName" > REALNAME = #realName# </isNotNull> <isNotNull prepend="," property="hlr" > HLR = #hlr# </isNotNull> <isNotNull prepend="," property="hlrText" > HLRTEXT = #hlrText# </isNotNull> <isNotNull prepend="," property="address" > ADDRESS = #address# </isNotNull> <isNotNull prepend="," property="sex" > SEX = #sex# </isNotNull> <isNotNull prepend="," property="birthDay" > BIRTHDAY = to_date(#birthDay#,'YYYY-MM-DD') </isNotNull> <isNotNull prepend="," property="constallation" > CONSTALLATION = #constallation# </isNotNull> <isNotNull prepend="," property="bloodType" > BLOODTYPE = #bloodType# </isNotNull> <isNotNull prepend="," property="email" > EMAIL = #email# </isNotNull> <isNotNull prepend="," property="signature" > SIGNATURE = #signature# </isNotNull> <isNotNull prepend="," property="profession" > PROFESSION = #profession# </isNotNull> <isNotNull prepend="," property="companyName" > COMPANYNAME = #companyName# </isNotNull> <isNotNull prepend="," property="compAddress" > COMPADDRESS = #compAddress# </isNotNull> <isNotNull prepend="," property="compLocation" > COMPLOCATION = #compLocation# </isNotNull> <isNotNull prepend="," property="headImageID" > HEADIMAGEID = #headImageID# </isNotNull> <isNotNull prepend="," property="headImageURL" > HEADIMAGEURL = #headImageURL# </isNotNull> <isNotNull prepend="," property="lastLoginTime" > LASTLOGINTIME = to_date(#lastLoginTime#,'YYYY-MM-DD HH:MM:SS') </isNotNull> <isNotNull prepend="," property="state" > STATE = #state# </isNotNull> <isNotNull prepend="," property="fancnt" > FANCNT = #fancnt# </isNotNull> <isNotNull prepend="," property="attencnt" > ATTENCNT = #attencnt# </isNotNull> <isNotNull prepend="," property="visitedcnt" > VISITEDCNT = #visitedcnt# </isNotNull> <isNotNull prepend="," property="commcnt" > COMMCNT = #commcnt# </isNotNull> <isNotNull prepend="," property="createTime" > CREATETIME = to_date(#createTime#,'YYYY-MM-DD HH:MM:SS') </isNotNull> <isNotNull prepend="," property="getFlag" > GETFLAG = #getFlag# </isNotNull> <isNotNull prepend="," property="imei" > IMEI = #imei# </isNotNull> </dynamic> <![CDATA[ where USERID = #userID# ]]> </update> <update id="updateHeardImage" parameterClass="UserBean"> <![CDATA[ update FHPP_USER_INFO set HEADIMAGEID=#headImageID#,HEADIMAGEURL=#headImageURL# where USERID=#userID# ]]> </update> <!-- Óû§¶¨¹ºÌײÍÐÞ¸Ä --> <update id="updateOrderState" parameterClass="UserBean"> <![CDATA[ update FHPP_USER_INFO set ORADERSTATE=#oraderState#, SCRIBESERVICEID = #scribeServiceId#, SPACETOTAL=#spaceTotal# where MOBILE=#mobile# ]]> </update> </sqlMap>
简单的方法调用
import java.util.List; import org.springframework.orm.ibatis.SqlMapClientTemplate; import Bean.Student; import Dao.IStudentDao; public class StudentDaoImpl implements IStudentDao{ private SqlMapClientTemplate sqlMapClientTemplate; public SqlMapClientTemplate getSqlMapClientTemplate() { return sqlMapClientTemplate; } public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) { this.sqlMapClientTemplate = sqlMapClientTemplate; } @Override public List<Student> findAll() { return (List<Student>)sqlMapClientTemplate.queryForList("selectAllStudent"); } @Override public void addStudent(Student student) { sqlMapClientTemplate.insert("Student.insertStudent",student); } }