user.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="user"> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias="User" type="****.beans.User"/> <!-- Result maps describe the mapping between the columns returned from a query, and the class properties. A result map isn't necessary if the columns (or aliases) match to the properties exactly. --> <resultMap id="UserResult" class="User"> <result property="userName" column="name"/> <result property="password" column="password"/> </resultMap> <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllUser" resultMap="UserResult"> select * from User </select> <!-- A simpler select example without the result map. Note the aliases to match the properties of the target result class. --> <select id="selectUserByName" parameterClass="java.lang.String" resultClass="User"> select name as userName, password as password from user where name = #userName# </select> <!-- Insert example, using the User parameter class--> <insert id="insertUser" parameterClass="User"> insert into User ( name, password ) values (#userName#,#password#) </insert> </sqlMap>
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> <!-- The properties (name=value) in the file specified here can be used placeholders in this config file (e.g. “${driver}”. The file is relative to the classpath and is completely optional. <properties resource="db.properties" /> --> <!-- These settings control SqlMapClient configuration details, primarily to do with transaction management. They are all optional (more detail later in this document). <settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false"/> --> <!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> <property name="JDBC.DefaultAutoCommit" value="true" /> <property name="Pool.MaximumActiveConnections" value="10"/> <property name="Pool.MaximumIdleConnections" value="5"/> <property name="Pool.MaximumCheckoutTime" value="120000"/> <property name="Pool.TimeToWait" value="500"/> <property name="Pool.PingQuery" value="select 1 from User"/> <property name="Pool.PingEnabled" value="false"/> <property name="Pool.PingConnectionsOlderThan" value="1"/> <property name="Pool.PingConnectionsNotUsedFor" value="1"/> </dataSource> </transactionManager> --> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <!-- List more here... <sqlMap resource="com/mydomain/data/Order.xml"/> <sqlMap resource="com/mydomain/data/Documents.xml"/> --> <sqlMap resource="******/beans/User.xml"/> </sqlMapConfig>
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:property-placeholder location="classpath:db.properties"/> <context:annotation-config/> <context:component-scan base-package="com.***"/> <!--此bean用来告诉Spring去何处找数据库信息,有此Bean才会有下面dataSource中用${}标记来取变量的语句 <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:db.properties</value> </property> </bean> --> <!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${driver}</value> </property> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value> </property> <property name="password"> <value>${password}</value> </property> </bean> <!--根据dataSource和configLocation创建一个SqlMapClient --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <!-- sql语句配置xml文件 --> <value>classpath:SqlMapConfig.xml</value> </list> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!--根据sqlMapClien创建一个sqlMapClientTemplate模版类 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean name="userServiceSpring" class="*****.service.UserServiceSpring"> <property name="sqlMapClientTemplate"> <ref bean="sqlMapClientTemplate"/> </property> </bean> <bean id="user" class="****.beans.User"/> </beans>
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version="1.2"> <!--此配置把jsf bean 交给spring管理--> <application> <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> </application> <!-- <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>*****.beans.User</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> --> <navigation-rule> <from-view-id>/index.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/sucess.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>
//jsf 中获取spring实例的对象
ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext(); ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);